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?

Robbie Guilfoyle
  • 3,363
  • 1
  • 18
  • 18

1 Answers1

3

If you are saying that you want the action behind your link to be able to update its data with:

@property.update(property_params)

Where the strong parameters are something like:

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

Then I think you could define your link as:

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

And that would line up the parameters in the typical way. Is that what you're after?

cschroed
  • 6,304
  • 6
  • 42
  • 56