2016-04-28 6 views
0

私のプロジェクトでは、Ryan Biggの "Multitenancy with Rails"のアイデアを使用しています。各アカウントには多数のユーザーがおり、そのアカウントの所有者は、送信された電子メールの"Accept invitation"をクリックできるユーザーを招待できます。すでにユーザが/サイトに登録ログインしていない場合は、部分的に以下のフォームが表示されます。Rails: "form_for ... as::user"にフィールドを追加するときの未定義メソッドエラー

_anonymous_accept.html.erb:提出

<%= form_for(@invitation, url: accepted_invitation_path(params[:slug], @invitation), as: :user) do |f| %> 

    <%= f.label :email %> 
    <%= f.email_field :email, value: @invitation.email, class: 'form-control' %> 

    <%= f.label :password %> 
    <%= f.password_field :password, class: 'form-control' %> 

    <%= f.label :password_confirmation, "Confirmation" %> 
    <%= f.password_field :password_confirmation, class: 'form-control' %> 

    <%= f.submit "Accept invitation", class: "button" %> 

<% end %> 

で、これは受け入れによって処理されます招待状のコントローラ内のアクション: invitations_controller.rb

def accept 
    store_location_for(:user, request.fullpath) 
    @invitation = Invitation.find_by!(token: params[:id]) 
end 

def accepted 
    @invitation = Invitation.find_by!(token: params[:id]) 

    if user_signed_in? 
    user = current_user 
    else 
    user_params = params[:user].permit(
     :email, 
     :password, 
     :password_confirmation 
    ) 

    user = User.create!(user_params) 
    sign_in(user) 
    end 

    current_account.users << user 

    flash[:success] = "Welcome! You have joined the #{current_account.name} account!" 
    redirect_to dashboard_path(params[:slug]) 
end 

これはフォームにフィールドを追加するまで完全に動作します。私は、ユーザーが自分のフルネームを入力しなくても登録できるようにしたくないので、私は上記のコードサンプルに次の行を追加しよう:

# added to the above sample from _anonymous_accept.html.erb 

<%= f.label :firstname %> 
<%= f.text_field :firstname, class: 'form-control' %> 

<%= f.label :lastname %> 
<%= f.text_field :lastname, class: 'form-control' %> 

と、この:

# added to the permitted user params in the accepted action in invitations_controller.rb 

:firstname, 
:lastname, 

これらの変更コードを破壊し、私は、このエラーを提供しています:

NoMethodError in Accounts::InvitationsController#accept 
undefined method `firstname' for #<Invitation:0x0000000478cb58> 

彼らはすべてのときのメールアドレスとパスワードのparamsは、姓と名の受け入れとされていないので、私は、このエラーの背後にあるロジックを見ることができないのです同じユーザーモデルから

誰にも提案はありますか?

EDIT:サーバーのログから

、要求通り:

NoMethodError (undefined method `firstname' for #<Invitation:0x00000005a32208>): 
    app/views/accounts/invitations/_anonymous_accept.html.erb:12:in `block in _app_views_accounts_invitations__anonymous_accept_html_erb___2610584697742424634_41597720' 
    app/views/accounts/invitations/_anonymous_accept.html.erb:9:in `_app_views_accounts_invitations__anonymous_accept_html_erb___2610584697742424634_41597720' 
    app/views/accounts/invitations/accept.html.erb:9:in `_app_views_accounts_invitations_accept_html_erb__4463442190025129055_39118520' 

EDIT 2:

# from db/schema.rb 

create_table "invitations", force: :cascade do |t| 
    t.string "email" 
    t.integer "account_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.string "token" 
end 

# from models/invitation.rb 

class Invitation < ActiveRecord::Base 
    belongs_to :account 
    before_create :generate_token 

    validates :email, presence: true 

    def to_param 
    token 
    end 

    private 

    def generate_token 
    self.token = SecureRandom.hex(16) 
    end 
end 
+1

uは、テーブルに 'ファーストネーム、lastname'列を持っているんであれば私に教えてください? – 7urkm3n

+0

はい、それらはすべてusersテーブルにあります。プロフィールの編集機能があるので、ユーザーは登録後に自分の名前を追加することができますが、招待を受け入れて登録するときに自分のフルネームを入力するようにしたいと考えています。 – GreyEyes

+0

名義と氏名がなくても機能しますか? – 7urkm3n

答えて

0

フォームはいないfirstnamelastname属性を持つ@invitationインスタンスオブジェクトのためであります。 招待状にユーザーモデルとの関連がある場合は、招待状にfields_forを使用するか、招待モデルにfirstname & lastnameフィールドを追加してください。

+0

ありがとう!しかし、なぜ 'password'と' password_confirmation'が '@ invitation'の属性でもないので、それはなぜ説明しません。私はちょうど 'as::user'がこれを世話すると考えました。しかし、私は 'fields_for'でそれらを入れ子にしようとする可能性があります – GreyEyes

+0

そうですね!その奇妙な –

+0

あなたはユーザーのテーブルスキーマを投稿できますか? –

0

おそらく、問題はdeviseから発生します。あなたのinvitation.rbファイルにこれを追加

class ApplicationController < ActionController::Base 
    before_action :configure_permitted_parameters, if: :devise_controller? 

    protected 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) << [:firstname, :lastname] 
    end 
end 

...この方法を試してみてください、と私に知らせてください。

更新日:

attr_accessor :firstname, :lastname 
+0

ありがとうございますが、残念ながらそれを解決しませんでした。同じエラーが発生します。 – GreyEyes

0

オールライト、私はそれを働かせました。しかし、これは安価な回避策かもしれません。

私はこれらの行に置き換え:FirstNameおよび:で姓だから私は、私はちょうど未定義のメソッドへの呼び出しをバイパスしてると思います

<%= label_tag("user[firstname]", "Your first name:") %> 
<%= text_field_tag("user[firstname]") %> 

<%= label_tag("user[lastname]", "Your last name:") %> 
<%= text_field_tag("user[lastname]") %> 

:これらの行で...

# from _anonymous_accept.html.erb 

<%= f.label :firstname %> 
<%= f.text_field :firstname, class: 'form-control' %> 

<%= f.label :lastname %> 
<%= f.text_field :lastname, class: 'form-control' %> 

をフォームヘルパー。

これは本当に悪い:-)

関連する問題