【问题标题】:Rails 4: How to pass nested attributes with strong parameters into a modelRails 4:如何将具有强参数的嵌套属性传递给模型
【发布时间】:2014-02-20 04:26:25
【问题描述】:

我在这里使用了两个嵌套资源。藏匿处和接收器。

routes.rb

resources :stashes do
  resources :receivers
end

我有一个表单,我想在提交后同时创建一个 Stash 和一个 Receiver。

<%= form_for(@stash) do |f| %>
  <h2>Who is this Stash for?</h2>
  <div class="reciever-wrap">
    <%= f.fields_for @receiver do |builder| %>
      <div><%= builder.label :first_name %><br />
      <%= builder.text_field :first_name, :autofocus => true %></div>

      <div><%= builder.label :last_name %><br />
      <%= builder.text_field :last_name %></div>

    <% end %>
   <div class="self-check">
      <%= check_box_tag(:self) %>
      <%= label_tag(:self, "This stash is for me") %></div>
  </div>

  <div><%= f.label :occasion, "Is this stash for an occassion?" %><br />
  <%= f.text_field :occasion %></div>

  <div><%= f.label :initial_amount, "How much would you like to spend?" %><br />
  <%= f.text_field :initial_amount %></div>

  <div><%= f.label :length, "How long would you like this Stash to last?" %><br />
  <%= select_tag(:length, options_for_select([['3 Months', 1],['6 Months', 2],['1 Year']])) %>    </div>

  <div><%= f.submit "Start adding gifts to this Stash!", :class => "button" %></div>
<% end %>

我认为首先创建 Receiver,然后创建 Stash 是最有意义的,因此我可以将 Stash 与 Receiver 相关联。所以在 Stashes 控制器中(因为 Stash 在这种形式中是主要的)我在创建操作中有这个:

stashes_controller.rb

def create
  receiver = Receiver.create_receiver(current_user, stash_params)
  if receiver.id
    @stash = Stash.create_stash(current_user, stash_params, receiver)
    if @stash.id
      flash[:success] = "Stash Started!"
      redirect_to stashes_url(@stash), :notice => "Stash Started!"
    else
      render :action => "new", :notice => "Error, try that again"
    end
  end
end

我还在底部定义了 stash_params:

def stash_params
  params.require(:stash).permit(
    :intitial_amount,
    :length,
    :user_id,
    :first_name,
    :last_name,
    :id,
    :receivers_attributes => [:id, :first_name, :last_name])
end

我能够创建 Stash 没问题。但是当我去创建接收器时,我遇到了麻烦。系统正在创建新的 Receiver 记录,但没有存储表单中的任何数据。我猜这与无法访问嵌套的强参数数据有关。

这是 Receiver 模型中的 create_receiver 方法,它应该保存数据,但由于某种原因不是。我能够保存 current_user 数据,这很好。只有强属性中的数据没有被保存:

receiver.rb

def self.create_receiver(current_user, stash_params)
  Receiver.create!(
    :first_name => stash_params[:first_name],
    :last_name => stash_params[:last_name],
    :user_id => current_user
  )
end

我是否需要遍历 stash_params 才能访问嵌套的 receiver_params 属性?我不确定我应该如何访问模型中的嵌套属性。任何帮助是极大的赞赏!

我还尝试将这个 create 方法移动到 Stash 模型中,看看我是否可以从那里访问属性,但我也没有运气。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 nested-attributes strong-parameters


    【解决方案1】:

    您的模型中可能缺少accepts_nested_attributes_for

    http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

    更新 #1

    既然你说你已经包含了accepts_nested_attributes_for,我建议将你的控制器逻辑整合到类似的东西

    def create
      @stash = Stash.new(stash_params)
      @stash.user = current_user
      @stash.receivers.each { |r| r.user = current_user }
    
      if @stash.save
          flash[:success] = "Stash Started!"
          redirect_to stashes_url(@stash), :notice => "Stash Started!"
        else
          render :action => "new", :notice => "Error, try that again"
        end
      end
    end
    

    这样您就不必检查单个记录是否正在保存并且强参数是否应该正确运行

    【讨论】:

    • 谢谢,凯尔。我在 Receiver 和 Stash 模型中都有accepts_nested_attributes_for。这是我检查的第一件事!
    • 我在此处的 @stash.receivers.each 方法上遇到错误。我相信这是因为尚未创建接收器。我遇到的这个问题是访问 Receiver 模型中的 receivers_params。
    【解决方案2】:

    由于您使用的是嵌套属性,因此您应该在 create 操作中尝试此操作:

    def create
      stash = Stash.new(stash_params)
      if stash.save
        flash[:success] = "Stash Started!"
        redirect_to stashes_url(@stash), :notice => "Stash Started!"
      else
        render :action => "new", :notice => "Error, try that again"
      end
    end
    

    这完成了保存存储和接收器所需的所有工作。 accepts_nested_attribute_for 保证所属对象将被保存,并在保存父对象的同时从父对象接收正确的 id。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多