本文介绍了Rails 4中的多对多嵌套属性(具有强大的参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来我一直在试图解决这个问题.我正在使用Rails 4(具有更新的质量分配技术),并尝试使用具有多对多关系的嵌套属性.我的记录正在保存到数据库中,但一切都为零,并且我的日志中出现不允许的参数:学校,校友,潜在客户"错误.

I have been trying to figure this one out for a few days now. I am using Rails 4 (with the updated mass assignment technique) and trying to use nested attributes with a many-to-many relationship. My record is saving to the DB but everything is nil and I'm getting an "Unpermitted parameters: school, alumnis, prospects" error in my logs.

这就是我所拥有的:

referral.rb

class Referral < ActiveRecord::Base
 belongs_to :school
 belongs_to :alumni
 belongs_to :prospect
end

alumni.rb

class Alumni < ActiveRecord::Base
  has_many :referrals
  has_many :prospects, through: :referrals

  accepts_nested_attributes_for :referrals
end

school.rb

class School < ActiveRecord::Base
  has_many :referrals
  has_many :prospects, through: :referrals
  has_many :alumnis, through: :referrals

  accepts_nested_attributes_for :referrals
end

prospect.rb

class Prospect < ActiveRecord::Base
  has_many :referrals
  has_many :alumnis, through: :referrals

  accepts_nested_attributes_for :referrals
end

referrals_controller.rb

def create
  @referral = Referral.create(referral_params)

  respond_to do |format|
    if @referral.save
      # ReferralMailer.referrer_email(@referral).deliver
      # ReferralMailer.referral_email(@referral).deliver
      format.html { redirect_to @referral, notice: 'Referral was successfully created.' }
      format.json { render action: 'show', status: :created, location: @referral }
    else
      format.html { render action: 'new' }
      format.json { render json: @referral.errors, status: :unprocessable_entity }
    end
  end
end

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

  # Never trust parameters from the scary internet, only allow the white list through.
  def referral_params
    params.require(:referral).permit(prospects_attributes: [:first_name, :last_name, :email], alumnis_attributes: [:first_name, :last_name, :email], schools_attributes: [:name])

  end

_form.html.erb

<%= form_for(@referral) do |f| %>
  <% if @referral.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@referral.errors.count, "error") %> prohibited this referral from being saved:</h2>

      <ul>
      <% @referral.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.fields_for :school do |builder| %>
    <%= builder.label :name, "School Name" %>
    <%= builder.text_field :name %>
  <% end %>

  <%= f.fields_for :alumnis do |builder| %>
    <%= builder.label :first_name, "First Name" %>
    <%= builder.text_field :first_name %>

    <%= builder.label :last_name, "Last Name" %>
    <%= builder.text_field :last_name %>

    <%= builder.label :email, "Email" %>
    <%= builder.text_field :email %>
  <% end %>

  <%= f.fields_for :prospects do |builder| %>
    <%= builder.label :first_name, "First Name" %>
    <%= builder.text_field :first_name %>

    <%= builder.label :last_name, "Last Name" %>
    <%= builder.text_field :last_name %>

    <%= builder.label :email, "Email" %>
    <%= builder.text_field :email %>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

服务器日志输出

Processing by ReferralsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ee+rREUU/0wGzNFTEaMxr8oRStaA53X9fmDrlVRyrD8=", "referral"=>{"school"=>{"name"=>"asdf"}, "alumnis"=>{"first_name"=>"asdf", "last_name"=>"asfd", "email"=>"asdf"}, "prospects"=>{"first_name"=>"asdf", "last_name"=>"asdf", "email"=>"asdf"}}, "commit"=>"Create Referral"}
Unpermitted parameters: school, alumnis, prospects
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "referrals" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", Fri, 12 Jul 2013 03:49:06 UTC +00:00], ["updated_at", Fri, 12 Jul 2013 03:49:06 UTC +00:00]]
   (0.6ms)  commit transaction
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Redirected to http://localhost:3000/referrals/68

引荐记录

=> #<Referral id: 68, created_at: "2013-07-12 03:49:06", updated_at: "2013-07-12 03:49:06", school_id: nil, alumni_id: nil, prospect_id: nil>

推荐答案

您不会像预期的强参数那样将参数传递给控制器​​.

You parameters are not being past to the controller as strong parameters is expecting.

从您的服务器日志中:

"referral" => {
  "school"  => { 
     "name" => "asdf" }, 
  "alumnis" => { 
    "first_name" => "asdf", 
    "last_name"  => "asfd", 
    "email"      => "asdf" 
  }, 
  "prospects" => {
    "first_name" => "asdf", 
    "last_name"  => "asdf", 
    "email"      => "asdf" 
  }
 }

强大的参数要求使用prospects_attributesalumnis_attributesschools_attributes,因此prospectsalumnisschool会被阻止,并且将创建没有任何属性的对象.

Strong parameters is expecting prospects_attributes, alumnis_attributes and schools_attributes so prospects, alumnis and school are getting blocked and the objects are getting created without any attributes.

这篇关于Rails 4中的多对多嵌套属性(具有强大的参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:09