0

I am wanting to be able to click a link and update an attribute is_public in rails4 with strong parameters. Here is my current code:

<small><%= link_to "make private", property_url(@property,is_public: !@property.is_public), method: "patch" %></small>

The issue I have with this, is in the controller the strong parameters will receive a No Method for Nil Class for the .permit method because it doesn't create params[property][:is_public]. It just creates params[:is_public] and the ID of the property.

Is there a rails way of making it nest the parameters in the object or will I have to hard code (I don't even like hard coding my name) this?

4

1 回答 1

3

如果您说您希望链接背后的操作能够更新其数据:

@property.update(property_params)

强参数类似于:

def property_params
  params.require(:property).permit(:is_public)
end

然后我认为您可以将链接定义为:

<small><%= link_to "make private", property_url(@property, property: { is_public: !@property.is_public }), method: "patch" %></small>

这将以典型的方式排列参数。这就是你所追求的吗?

于 2013-11-12T20:44:11.137 回答