错误-在分配属性时,必须将哈希作为参数传递



我有一个资金表,下面是它的管理资金显示页面。我想添加一个功能,这样管理员就可以直接从这个页面添加支票截止日期,并在末尾添加提交按钮。

<%= form_tag add_cheque_date_path, :method => 'patch' do %>
<tbody>
<% @fundings.each do |funding| %>
<tr> 
<td><%= funding.child.parent.parent_1_firstname %></td>
<td><%= funding.child.parent.email %></td>
<td><%= funding.activity_start_date %></td>
<td><%= funding.date_submitted %></td>
<td><%= funding.status %></td>
<td><%= date_field_tag "funding.cheque_cut_date", funding.cheque_cut_date %></td>
<td><%= link_to "View", parent_child_funding_path(funding.child.parent, funding.child, funding) %></td>   
</tr>
<% end %>
</tbody>   
</table>
<%= submit_tag "Submit" %>
<% end %>

parents_controller.rb

def add_cheque_date
@fundings= Funding.all
fundings = params[:fundings]
@fundings.each do |funding|
funding.update_attributes(:cheque_cut_date)
end
end
def funding_params
params.require(:funding).permit(:cheque_cut_date)
end

routes.rb

patch 'admin/application-status', to: 'parents#add_cheque_date', as: 'add_cheque_date'

当我点击下面的提交是我得到的错误。请帮我修一下。

When assigning attributes, you must pass a hash as an argument.

update_attributes更新传入的所有属性散列并保存记录。如果对象无效,则保存将fail和false将被返回。

但在funding.update_attributes(:cheque_cut_date),您只放入密钥:cheque_cut_date,没有任何值。尝试下一个:

funding.update_attributes(funding_params)

此外,还有不止一个问题。date_field_tag "funding.cheque_cut_date"创建名称为funding.cheque_cut_date的字段,但params.require(:funding).permit(:cheque_cut_date)不允许密钥funding.cheque_cut_date。也更改字段名称:

<td><%= date_field_tag "funding[cheque_cut_date]", funding.cheque_cut_date %></td>
#or the same fields helpers type as other from this form
<td><%= f.date_field :cheque_cut_date %></td>

form.html.erb

<td><%= date_field_tag "cheque_cut_date[#{funding.id}]", funding.cheque_cut_date, min: Date.today %></td>

控制器rb

def add_cheque_date
fundings = params[:cheque_cut_date]
fundings.select do |k, v|
if v.present?
funding = Funding.find(k)
funding.update_attributes({cheque_cut_date: v})
end
end
end

最新更新