2016-04-14 4 views
0

ここが問題です。私はユーザーとイベントを持っており、それらの間に招待状を持っていたい。つまり、ユーザーは他のユーザーをイベントに招待することができます。イベントとユーザーのためのレール招待コントローラ

私はInvitationsコントローラとモデルを作成しました。モデルはこの

user_id: (user being invited), event_id:(event_to_attend), inviter: (sent_the_invite) 

私は新しい/招待状/へ行くことによって、コントローラからの私の招待状を構築し、フォームを送信が、私は彼らが仕事を得ることができないんだように見えます。それらは前に作成されましたが、私はいくつかの変更を加えて作成しません。単に「招待状が送られました」と言っていますが、コンソールなどに招待状は作成されません。この場合には、私は招待状や缶を見ることができます

コンソールで、私は

a=User.first 
a.invitations.build(event_id: 1, inviter:2) 
a.save 

を行う。そして、私は招待状とUSER_IDは招待状を作成したものである見ることができるので、私は、彼らが仕事を知っています、イベントの.inviteesメソッドを呼び出してユーザーを取得します。だから協会が働く。しかし、私はフォームを通じてそれを作成することはできません。

は、ここに私の招待状コントローラ

クラスInvitationsController < ApplicationControllerに が デフ新しい ApplicationHelperを含んだ、私はUX

ここ

ためのユーザーイベントの説明の名前のみを使用して、フォーム

<%= form_for(@invitation) do |f| %> 

<% userArray=User.all.select{|u| u.name unless u==current_user }%> 
<% names=userArray.map{|u| u.name} %> 
<% events=current_user.attended_events.map{|u| u.description} %> 

<%= f.label :event %> 
<%= f.select :event, events %> 

<%= f.label :user %> 
<%= f.select :user, names %> 

<%= f.submit "Send" %> 

<% end %> 

です@招待状=招待状。新着 終了

def create #同じユーザーが同じ名前のイベント、重複したユーザー、およびイベントを考慮しません。

#user being invited 
@user=User.find_by(name: params[:invitation][:user]) 


#event being invited to 
@event=Event.find_by(description: params[:invitation][:event]) 


#Inviter is current user, invitee is @user 
@user.invitations.build(event_id: @event.id, inviter: current_user.id) 

#Event not in user.attended_events 
if !(@user.attended_events.where(id: @event.id) || \ 
    @user.invitations.where("[email protected] AND [email protected]")) 

    flash.now[:danger]="User is already going to the event" 
    render 'foo' 
    #render 'new' 

elsif @user.save! 
    flash[:success]="Invitation was SENT!" 
    redirect_to new_invitation_path 
else 
    flash.now[:danger]="Please select both options" 
    render 'foobar' 
    #render 'new' 
end 
    end 

答えて

0

あなたが必要以上にあなた自身をかなり難しくしています。

フォームをクリーンアップするrails collection helpersを使用して起動することができます。

<%= form_for(@invitation) do |f| %> 
    <div class="row"> 
    <%= f.label :event_id %> 
    <%= f.collection_select :event_id, Event.all, :id, :name, prompt: true %> 
    </div> 

    <div class="row"> 
    <%= f.label :user_id %> 
    <%= f.collection_select :user_id, User.where.not(id: @invitation.inviter.id), :id, :name, prompt: true %> 
    </div> 

    <%= f.submit %> 
<% end %> 

これは、あなたがcurrent_userメソッドを持っていることを前提とし、あなたのイベントとユーザーがラベルに使用する名前属性を持っていること(任意の属性を使用できます)。使用される実際の値は、関連付けられたレコードのIDです。

ここでは、パラメータキーevent_iduser_idを使用しています。 eventを使用すると、setterがIDだけでなく実際のEventインスタンスを必要とするため、エラーが発生します。

そうのようなあなたのコントローラでこれを処理します:

class InvitationsController < ActiveRecord::Base 
    def new 
    @invitation = current_user.invitations.new 
    end 

    def create 
    @invitation = current_user.invitations.new(invitation_params) 
    @invitation.save 
    respond_with(@invitation) 
    end 

    private 
    def invitation_params 
     params.require(:invitation).permit(:event_id, :user_id) 
    end 
end 

これはを通じてまだ重複の招待状を作成するための問題を処理しません。

class Invitation < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :invitation 
    belongs_to :inviter, class_name: 'User' 
    validates_uniqueness_of :user_id, 
    scope: 'invitation_id', 
    message: 'is already going to the event' 
end 

これは、ユーザーが既に招待されて、再びnewビューをレンダリングしている場合@invitation.saveが失敗する原因になります。これを扱うRailsの方法は、モデルに一意性の検証を追加することです。

Due to the possibility of race conditionsデータベースインデックスを使用して検証をバックする必要があります。

# generate with 
# $ rails g migration AddUniqueIndexToInvitation 
class AddUniqueIndexToInvitation < ActiveRecord::Migration 
    def change 
    add_index :invitations, [:user_id, :event_id], unique: true 
    end 
end 
関連する問題