Reputation: 21471
I've made a little test with Rails scaffolding, and I tried to generate a controller and a view for an already existing model.
I'm trying to edit a value for my model.
In the controller all the requires methods are there, and there's even :
# Never trust parameters from the scary internet, only allow the white list through.
def tag_params
params[:tag]
end
The only value I tried to update from my model (named Tag) was :name, so I edited the tag_params
method to look like this:
# Never trust parameters from the scary internet, only allow the white list through.
def tag_params
params[:tag].require(:tag).permit(:name)
end
as I usually do, but now I get a param not found: tag
exception.
However, if I write it like this
# Never trust parameters from the scary internet, only allow the white list through.
def tag_params
params[:tag].permit(:name)
end
it works alright. Why is that?
The form_for declaration in the view looks like this:
<%= form_for(@tag) do |f| %>
For those who don't know how scaffolding method look like in the controller, here's a part of it :
# GET /tags/1/edit
def edit
end
# PATCH/PUT /tags/1
# PATCH/PUT /tags/1.json
def update
respond_to do |format|
if @tag.update(tag_params)
format.html { redirect_to tags_path, notice: 'Tag was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @tag.errors, status: :unprocessable_entity }
end
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def tag_params
params[:tag]
end
Upvotes: 0
Views: 123
Reputation: 25761
params[:tag].require(:tag)
will require a param tag
inside the params[:tag]
hash. What you want to do is params.require(:tag).permit(:name)
.
Upvotes: 1
Reputation: 2347
It should be:
def tag_params
params.require(:tag).permit(:name)
end
Upvotes: 1