nil类rails的未定义方法'[]'

时间:2015-03-11 18:13:40

标签: ruby-on-rails ruby-on-rails-4 params

为什么这是一个未定义的方法?它说错误就在这一行:

  @member.room.update(rent: params[:member][:room_attributes][:rent], room_name: params[:member][:room_attributes][:room_name])

members_controller.rb

def edit
    @member = Member.find(params[:id])
end

def update
    @member = Member.find(params[:id])
    @member.room.update(rent: params[:member][:room_attributes][:rent], room_name: params[:member][:room_attributes][:room_name])
end


def member_params
    params.require(:member).permit(:name, :room_id,
                                    room_attributes: [:rent, :room_name, :member_id],
                                    purchase_attributes: [:description, :cost, :member_id])
end

edit.html.erb

<h3> Member Name </h3><br>
<%= @member.name %><br>
  <%= form_for @member.room, url: member_path(@member) do |f| %>
    <%= f.label :room_name %><br>
    <%= f.text_area :room_name %><br>
    <%= f.label :rent %><br>
    <%= f.text_area :rent %><br>


    <%= f.submit "Submit" %>

  <% end %>

2 个答案:

答案 0 :(得分:1)

错误表示未设置其中一个参数键。输出表单(在 edit.html.erb 中)时,每个表单字段的名称和/或ID是什么。

从它的外观来看,我似乎需要room[room_name] member[room_attributes][room_name]

答案 1 :(得分:0)

由于您使用的是强参数,请尝试使用:

@member.room.update(rent: member_params[:member][:room_attributes][:rent], room_name: member_params[:member][:room_attributes][:room_name])

member_params代替params