2016-07-25 9 views
0

私は私のユーザーのためのフォームをサインアップ提出するときに私はエラーUnpermitted parameter: organizationを取得しています。私は、「スクラッチからの認証」を使用しています。ここに私のコードです:Railsの5許可されていないパラメータ:組織

user.rb

class User < ApplicationRecord 
    belongs_to :organization 
    has_secure_password 
end 

organization.rb

class Organization < ApplicationRecord 
    has_many :users 
    has_many :tasks 
    accepts_nested_attributes_for :users 
end 

users_controller.rb

class UsersController < ApplicationController 
    def new 
    @user = User.new 
    @organization = Organization.new 
    end 

    def create 
    @user = User.new(user_params) 
    @user.build_organization(user_params[:organization_attributes]) 
    if @user.save 
     session[:user_id] = @user.id 
     redirect_to root_url, notice: "Thank you for signing up!" 
    else 
     render "new" 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_user 
     @user = User.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def user_params 
     params.require(:user).permit(:email, :password, :password_confirmation, :admin, 
     organization_attributes: :name) 
    end 
end 

new.html.erb

<h1>Sign Up</h1> 

<%= form_for @user do |f| %> 
    <% if @user.errors.any? %> 
    <div class="error_messages"> 
     <h2>Form is invalid</h2> 
     <ul> 
     <% @user.errors.full_messages.each do |message| %> 
      <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </div> 
    <div class="field"> 
    <%= f.fields_for :organization do |org| %> 
    <%= 'Organization or Company Name' %><br /> 
    <%= org.text_field :name %> 
    <% end %> 
    </div> 
    <div class="field"> 
    <%= f.label :password %><br /> 
    <%= f.password_field :password %> 
    </div> 
    <div class="field"> 
    <%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %> 
    </div> 
    <div class="field"> 
    <%= f.label :admin %><br /> 
    <%= f.check_box :admin %> 
    </div> 
    <div class="actions"><%= f.submit "Sign Up" %></div> 
<% end %> 
ここで

Processing by UsersController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"lhzxsTF43PiGKwMXly/fufGoVNEMUgqymwtMkhCkNtmolArIqbUjuo/qxYUVpFxIfaB4qVV2sumDqa5O2ggLbA==", "user"=>{"email"=>"[email protected]", "organization"=>{"name"=>"myOrg"}, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "admin"=>"0"}, "commit"=>"Sign Up"} 
Unpermitted parameter: organization 
Unpermitted parameter: organization 
    (0.1ms) begin transaction 
    SQL (0.3ms) INSERT INTO "organizations" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2016-07-25 15:39:56 UTC], ["updated_at", 2016-07-25 15:39:56 UTC]] 
    SQL (0.1ms) INSERT INTO "users" ("email", "password_digest", "organization_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["email", "[email protected]"], ["password_digest", "$2a$10$MEEXO6bU9FGwMv3WOvdYheL.1iGhx4eeDVo67qp.OPmh1BJHs0z0G"], ["organization_id", 10], ["created_at", 2016-07-25 15:39:56 UTC], ["updated_at", 2016-07-25 15:39:56 UTC]] 
    (0.7ms) commit transaction 
Redirected to http://localhost:3000/ 
Completed 302 Found in 64ms (ActiveRecord: 1.1ms) 

...提出時にコンソールでのぞき見ている私は、問題の根は、パラメータが送信されたときorganization"=>{"name"=>"myOrg"}が、それは代わりにorganization_attributesあるべきと考えていますか?

答えて

1

あなたの推測は正しいですが、夫婦、他の問題があります。

  1. 前述のとおり、strong_paramsオプションをorganization_attributesに変更します。
  2. あなたはaccepts_nested_attributesを後方に持っています。 user_paramsというユーザーを作成しているため、Userモデルにはaccepts_nested_attributes :organizationが必要ですが、組織では必要ありません(他の場所で使用しない限り)。
  3. モデルを微調整した後、あなたは明示的に@user.build_organization(user_params[:organization_attributes])を経由して、もはや組織を構築する必要はありません。その行は単に削除することができます。

最後に、セキュリティリスクの可能性があるため、adminフラグを通過させたくないという点を指摘したいだけです。明らかにあなたのアプリを知らないが、ただそれを言いたい。

+0

ありがとうございました。モデルのaccepts_nested_attributesを変更し、f.fields_for:組織をf.fields_for:organization_attributesに変更しました。 – Lumbee

関連する問題