Reputation: 69
I am trying the update the record for the columns IsValid and Comments in rails, but doesn't get updated in backend or any error:
Below is my parameters result:
Started PATCH "/metrics/1" for
Processing by MetricsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"1oC/1UdAaTPUepy1zIjO1x6n67Th/pdcnvYJH95cB63tZts9d135JIK4MzQD2/pdPoRKnUKXIc0ZI9MQZkjfNQ==",
"metrics_controller"=>{"IsValid"=>"False", "Comments"=>"1"}, "commit"=>"Save", "id"=>"1"}
Metric Load (75.1ms) SELECT `Threshold`.* FROM `Threshold` WHERE `Threshold`.`ID` = 1 LIMIT 1
Unpermitted parameters: utf8, _method, authenticity_token, metrics_controller, commit, id
(75.7ms) BEGIN
(75.1ms) COMMIT
update method in controller:
def update
@metric = Metric.find(params[:id])
#if(@metric.update(post_params))
if(@metric.update_attributes(post_params))
redirect_to metrics_path
else
render 'edit'
end
end
private def post_params
params.permit(:Metric, :WI, :Value, :UT, :Score, :IsValid, :UserName, :Comments)
end
If I update post_params method with the below code it is throwing error:
private def post_params
params.require(:metric).permit(:Metric, :WI, :Value, :UT, :Score, :IsValid, :UserName, :Comments)
end
Upvotes: 0
Views: 140
Reputation: 1012
The params that are coming from the request (from the logs) are :
{
"utf8"=>"✓",
"authenticity_token"=>"1oC/1UdAaTPUepy1zIjO1x6n67Th..",
"metrics_controller"=> {
"IsValid"=>"False",
"Comments"=>"1"
},
"commit"=>"Save",
"id"=>"1"
}
So the key isValid
is under the key metrics_controller
.
Then, with this kind of coming data, in your controller, you should call
params
.require(:metrics_controller)
.permit(:IsValid, :Comments, :id)
You should take care of the naming convention (snake_case instead of CamelCase), but the most important thing is that the form that is responsible of this request should name inputs accordingly to what's your controller expect.
Then, you should name form elements with something like (according to your screenshot)
<select name="metric[is_valid]">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
With that kind of naming, data will be scoped in key metric
instead of metric_controller
, and it'll go through strong params.
Upvotes: 1
Reputation: 1288
As @Tom Lord mentioned, the case of your parameters looks suspicious and might be causing your problem. If you could provide your schema for the metrics
table that will help us help you.
Rails will always work at its best when you follow the naming conventions. This most importantly includes using the correct case and singular/plural names.
This is how I'd expect your code to look if it were written the Rails way:
def update
@metric = Metric.find(params[:id])
if @metric.update(post_params)
redirect_to metrics_path
else
render 'edit'
end
end
private
def post_params
params.permit(:metric, :wi, :value, :ut, :score, :is_valid, :user_name, :comments)
end
Upvotes: 0
Reputation: 28305
By convention in ruby, and rails, you should almost always use snake_case
for methods and variables; not CamelCase
.
You haven't quite provided enough information for me to say this with 100% certainty (in particular, what is the database schema for the metrics
table?), but that is almost certainly the cause of the problem here.
Your Metric
class likely has attributes such as value
, score
, is_valid
, etc. But you are trying to update attributes named Comments
and IsValid
. These attributes do not exist, so nothing gets updated.
Change your view to use snake_case
, and permit variables in snake_case
; then it should work. It would be possible to make it work using CamelCase
like you've done here, but that's against conventions - so would require more effort.
Upvotes: 1