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

时间:2018-07-25 19:46:49

标签: ruby-on-rails ruby ruby-on-rails-5

我有一个资助表,下面是其管理资助显示页面。我想添加一个功能,以便管理员可以直接从此页面添加截止日期,并在最后添加一个提交按钮。

File "C:/Users/bwerner/Documents/3pm_reporter.py", line 34, in <module>
    print(vt_result_check(path))
  File "C:/Users/bwerner/Documents/3pm_reporter.py", line 11, in vt_result_check
    vt_data = json.load(vt_result_file)
  File "C:\Users\bwerner\AppData\Local\Continuum\anaconda3\Lib\json\__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\bwerner\AppData\Local\Continuum\anaconda3\Lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\bwerner\AppData\Local\Continuum\anaconda3\Lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\bwerner\AppData\Local\Continuum\anaconda3\Lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

parents_controller.rb

<%= 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 %>

routes.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

当我单击下面的提交时,我得到了错误。请帮助我修复它。

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

2 个答案:

答案 0 :(得分:1)

  

update_attributes更新传入的所有属性   哈希并保存记录。如果对象无效,则保存将   失败并返回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>

答案 1 :(得分:0)

form.html.erb

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

controller.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