Ruby on rails Rails 4:如何将具有强参数的嵌套属性传递到模型中
我这里有两个嵌套的资源。储藏和接收器 routes.rbRuby on rails Rails 4:如何将具有强参数的嵌套属性传递到模型中,ruby-on-rails,ruby-on-rails-4,nested-attributes,strong-parameters,Ruby On Rails,Ruby On Rails 4,Nested Attributes,Strong Parameters,我这里有两个嵌套的资源。储藏和接收器 routes.rb resources :stashes do resources :receivers end def create receiver = Receiver.create_receiver(current_user, stash_params) if receiver.id @stash = Stash.create_stash(current_user, stash_params, receiver) if
resources :stashes do
resources :receivers
end
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
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
我有一个表单,我想在提交后创建一个隐藏和一个接收者
<%= 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 %>
我还在底部定义了隐藏参数:
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
我可以创建一个储藏室,没问题。但是,当我去创建接收器时,我遇到了麻烦。系统正在创建新的接收方记录,但表单中的任何数据都没有被存储。我猜这与无法访问嵌套的强参数数据有关
这是接收器模型中的create_receiver方法,它应该保存数据,但由于某些原因没有保存数据。我能够保存当前的用户数据,所以这很好。只有来自强属性的数据未被保存:
接收器.rb
resources :stashes do
resources :receivers
end
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
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_参数来访问嵌套的receiver_参数属性?我不确定应该如何访问模型中的嵌套属性。非常感谢您的帮助
我还尝试将此创建方法移动到隐藏模型中,以查看是否可以从那里访问属性,但也没有成功。您的模型中可能缺少
接受嵌套的属性
更新#1
既然您已经声明为
包含了接受\u嵌套的\u属性,\u,我建议您将控制器逻辑整合为
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
通过这种方式,您不必检查是否保存了单个记录,并且强参数应该正常运行因为您使用的是嵌套属性,所以您应该在创建
操作中尝试以下操作:
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
这就完成了保存存储和接收器所需的所有工作<代码>接受
的_嵌套_属性_确保所属对象将被保存,并在保存父对象的同时从父对象接收正确的id。谢谢,Kyle。在接收器和存储模型中,我确实为提供了接受\u嵌套的\u属性。这是我检查的第一件事!我在@stash.receivers上得到一个错误。这里的每个方法。我相信这是因为接收器还没有被创造出来。我遇到的这个问题是访问接收器模型中的接收器参数。