2016-07-25 3 views
0

내 사용자에게 가입 양식을 제출할 때 오류 Unpermitted parameter: organization가 표시됩니다. 나는 처음부터 'auth from scratch'변형을 사용하고 있습니다. 여기 내 코드입니다 :레일즈 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으로 사용자를 생성하므로 사용자 모델에 accepts_nested_attributes :organization이 필요하지만 조직에서는 필요하지 않습니다 (다른 곳에서 사용하지 않는 한).
  3. 모델을 조정 한 후에는 @user.build_organization(user_params[:organization_attributes])을 통해 더 이상 명시 적으로 조직을 구축 할 필요가 없습니다. 그 줄은 그냥 지울 수 있습니다.

마지막으로 보안 위험이 될 수 있으므로 admin 플래그가 통과되도록 허용하고 싶지 않을 수 있습니다. 분명히 귀하의 애플 리케이션을 모르지만, 단지 그것을 언급하고 싶었을뿐입니다.

+0

감사합니다 phoffer, '대부분'입니다. 모델의 accepts_nested_attributes를 전환하고 f.fields_for : organization을 f.fields_for : organization_attributes로 변경했습니다. – Lumbee

관련 문제