Unable to save/update the record into db in rails
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
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4
|
show 4 more comments
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
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4
You need to leave the post_params method in as it permits the parameters. Can you posy what error message occurs when you try and update please?
– Mark
Nov 14 '18 at 13:18
I'm not sure, but it looks like your header's params are send as body. How do you call update method? Ajax async call? Html/Rails form? could you update your answer?
– Pietro Allievi
Nov 14 '18 at 15:15
Hi @Mark , I don't get any error but the data doesn't get saved in db
– Mallela SriPrakash
Nov 14 '18 at 15:20
1
@MallelaSriPrakash replace if(@metric.update_attributes(post_params)) with if(@metric.update_attributes!(post_params)) please, the server will then throw an error and explain what is going wrong
– Mark
Nov 14 '18 at 15:21
@Mark As you have said, I have replaced it. But still I don't get any error while updating. But, when I changed the text as this if(@metric.update_attributes(!post_params)) then I get this error: When assigning attributes, you must pass a hash as an argument.
– Mallela SriPrakash
Nov 14 '18 at 15:45
|
show 4 more comments
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
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4
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
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4
asked Nov 14 '18 at 13:16
Mallela SriPrakashMallela SriPrakash
295
295
You need to leave the post_params method in as it permits the parameters. Can you posy what error message occurs when you try and update please?
– Mark
Nov 14 '18 at 13:18
I'm not sure, but it looks like your header's params are send as body. How do you call update method? Ajax async call? Html/Rails form? could you update your answer?
– Pietro Allievi
Nov 14 '18 at 15:15
Hi @Mark , I don't get any error but the data doesn't get saved in db
– Mallela SriPrakash
Nov 14 '18 at 15:20
1
@MallelaSriPrakash replace if(@metric.update_attributes(post_params)) with if(@metric.update_attributes!(post_params)) please, the server will then throw an error and explain what is going wrong
– Mark
Nov 14 '18 at 15:21
@Mark As you have said, I have replaced it. But still I don't get any error while updating. But, when I changed the text as this if(@metric.update_attributes(!post_params)) then I get this error: When assigning attributes, you must pass a hash as an argument.
– Mallela SriPrakash
Nov 14 '18 at 15:45
|
show 4 more comments
You need to leave the post_params method in as it permits the parameters. Can you posy what error message occurs when you try and update please?
– Mark
Nov 14 '18 at 13:18
I'm not sure, but it looks like your header's params are send as body. How do you call update method? Ajax async call? Html/Rails form? could you update your answer?
– Pietro Allievi
Nov 14 '18 at 15:15
Hi @Mark , I don't get any error but the data doesn't get saved in db
– Mallela SriPrakash
Nov 14 '18 at 15:20
1
@MallelaSriPrakash replace if(@metric.update_attributes(post_params)) with if(@metric.update_attributes!(post_params)) please, the server will then throw an error and explain what is going wrong
– Mark
Nov 14 '18 at 15:21
@Mark As you have said, I have replaced it. But still I don't get any error while updating. But, when I changed the text as this if(@metric.update_attributes(!post_params)) then I get this error: When assigning attributes, you must pass a hash as an argument.
– Mallela SriPrakash
Nov 14 '18 at 15:45
You need to leave the post_params method in as it permits the parameters. Can you posy what error message occurs when you try and update please?
– Mark
Nov 14 '18 at 13:18
You need to leave the post_params method in as it permits the parameters. Can you posy what error message occurs when you try and update please?
– Mark
Nov 14 '18 at 13:18
I'm not sure, but it looks like your header's params are send as body. How do you call update method? Ajax async call? Html/Rails form? could you update your answer?
– Pietro Allievi
Nov 14 '18 at 15:15
I'm not sure, but it looks like your header's params are send as body. How do you call update method? Ajax async call? Html/Rails form? could you update your answer?
– Pietro Allievi
Nov 14 '18 at 15:15
Hi @Mark , I don't get any error but the data doesn't get saved in db
– Mallela SriPrakash
Nov 14 '18 at 15:20
Hi @Mark , I don't get any error but the data doesn't get saved in db
– Mallela SriPrakash
Nov 14 '18 at 15:20
1
1
@MallelaSriPrakash replace if(@metric.update_attributes(post_params)) with if(@metric.update_attributes!(post_params)) please, the server will then throw an error and explain what is going wrong
– Mark
Nov 14 '18 at 15:21
@MallelaSriPrakash replace if(@metric.update_attributes(post_params)) with if(@metric.update_attributes!(post_params)) please, the server will then throw an error and explain what is going wrong
– Mark
Nov 14 '18 at 15:21
@Mark As you have said, I have replaced it. But still I don't get any error while updating. But, when I changed the text as this if(@metric.update_attributes(!post_params)) then I get this error: When assigning attributes, you must pass a hash as an argument.
– Mallela SriPrakash
Nov 14 '18 at 15:45
@Mark As you have said, I have replaced it. But still I don't get any error while updating. But, when I changed the text as this if(@metric.update_attributes(!post_params)) then I get this error: When assigning attributes, you must pass a hash as an argument.
– Mallela SriPrakash
Nov 14 '18 at 15:45
|
show 4 more comments
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
I agree that the first step is to get the naming conventions fixed in this code. Hopefully whatever is calling this update method is not passing the params as CamelCase. If so that should be fixed as well. This is the second question I've seen this week where not following Rails' naming convention has caused an app to not work.
– Dan
Nov 14 '18 at 15:46
1
@Dan "Hopefully whatever is calling this update method is not passing the params as CamelCase" -- It looks like it is, though! From the OP's console log:"metrics_controller"=>"IsValid"=>"False", "Comments"=>"1"
.
– Tom Lord
Nov 14 '18 at 16:02
yikes, I see that now. It would be nice to see the schema to see how far reaching this unconventional naming is. It's possible the DB fields are camel case. Ultimately, the OP just needs to be consistent across all parts of his app. It's possible he is and case isn't the issue...
– Dan
Nov 14 '18 at 16:44
add a comment |
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301133%2funable-to-save-update-the-record-into-db-in-rails%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 15 '18 at 15:24
answered Nov 14 '18 at 17:18
kevchakevcha
590513
590513
add a comment |
add a comment |
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.
I agree that the first step is to get the naming conventions fixed in this code. Hopefully whatever is calling this update method is not passing the params as CamelCase. If so that should be fixed as well. This is the second question I've seen this week where not following Rails' naming convention has caused an app to not work.
– Dan
Nov 14 '18 at 15:46
1
@Dan "Hopefully whatever is calling this update method is not passing the params as CamelCase" -- It looks like it is, though! From the OP's console log:"metrics_controller"=>"IsValid"=>"False", "Comments"=>"1"
.
– Tom Lord
Nov 14 '18 at 16:02
yikes, I see that now. It would be nice to see the schema to see how far reaching this unconventional naming is. It's possible the DB fields are camel case. Ultimately, the OP just needs to be consistent across all parts of his app. It's possible he is and case isn't the issue...
– Dan
Nov 14 '18 at 16:44
add a comment |
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.
I agree that the first step is to get the naming conventions fixed in this code. Hopefully whatever is calling this update method is not passing the params as CamelCase. If so that should be fixed as well. This is the second question I've seen this week where not following Rails' naming convention has caused an app to not work.
– Dan
Nov 14 '18 at 15:46
1
@Dan "Hopefully whatever is calling this update method is not passing the params as CamelCase" -- It looks like it is, though! From the OP's console log:"metrics_controller"=>"IsValid"=>"False", "Comments"=>"1"
.
– Tom Lord
Nov 14 '18 at 16:02
yikes, I see that now. It would be nice to see the schema to see how far reaching this unconventional naming is. It's possible the DB fields are camel case. Ultimately, the OP just needs to be consistent across all parts of his app. It's possible he is and case isn't the issue...
– Dan
Nov 14 '18 at 16:44
add a comment |
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.
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.
answered Nov 14 '18 at 13:26
Tom LordTom Lord
15.6k23053
15.6k23053
I agree that the first step is to get the naming conventions fixed in this code. Hopefully whatever is calling this update method is not passing the params as CamelCase. If so that should be fixed as well. This is the second question I've seen this week where not following Rails' naming convention has caused an app to not work.
– Dan
Nov 14 '18 at 15:46
1
@Dan "Hopefully whatever is calling this update method is not passing the params as CamelCase" -- It looks like it is, though! From the OP's console log:"metrics_controller"=>"IsValid"=>"False", "Comments"=>"1"
.
– Tom Lord
Nov 14 '18 at 16:02
yikes, I see that now. It would be nice to see the schema to see how far reaching this unconventional naming is. It's possible the DB fields are camel case. Ultimately, the OP just needs to be consistent across all parts of his app. It's possible he is and case isn't the issue...
– Dan
Nov 14 '18 at 16:44
add a comment |
I agree that the first step is to get the naming conventions fixed in this code. Hopefully whatever is calling this update method is not passing the params as CamelCase. If so that should be fixed as well. This is the second question I've seen this week where not following Rails' naming convention has caused an app to not work.
– Dan
Nov 14 '18 at 15:46
1
@Dan "Hopefully whatever is calling this update method is not passing the params as CamelCase" -- It looks like it is, though! From the OP's console log:"metrics_controller"=>"IsValid"=>"False", "Comments"=>"1"
.
– Tom Lord
Nov 14 '18 at 16:02
yikes, I see that now. It would be nice to see the schema to see how far reaching this unconventional naming is. It's possible the DB fields are camel case. Ultimately, the OP just needs to be consistent across all parts of his app. It's possible he is and case isn't the issue...
– Dan
Nov 14 '18 at 16:44
I agree that the first step is to get the naming conventions fixed in this code. Hopefully whatever is calling this update method is not passing the params as CamelCase. If so that should be fixed as well. This is the second question I've seen this week where not following Rails' naming convention has caused an app to not work.
– Dan
Nov 14 '18 at 15:46
I agree that the first step is to get the naming conventions fixed in this code. Hopefully whatever is calling this update method is not passing the params as CamelCase. If so that should be fixed as well. This is the second question I've seen this week where not following Rails' naming convention has caused an app to not work.
– Dan
Nov 14 '18 at 15:46
1
1
@Dan "Hopefully whatever is calling this update method is not passing the params as CamelCase" -- It looks like it is, though! From the OP's console log:
"metrics_controller"=>"IsValid"=>"False", "Comments"=>"1"
.– Tom Lord
Nov 14 '18 at 16:02
@Dan "Hopefully whatever is calling this update method is not passing the params as CamelCase" -- It looks like it is, though! From the OP's console log:
"metrics_controller"=>"IsValid"=>"False", "Comments"=>"1"
.– Tom Lord
Nov 14 '18 at 16:02
yikes, I see that now. It would be nice to see the schema to see how far reaching this unconventional naming is. It's possible the DB fields are camel case. Ultimately, the OP just needs to be consistent across all parts of his app. It's possible he is and case isn't the issue...
– Dan
Nov 14 '18 at 16:44
yikes, I see that now. It would be nice to see the schema to see how far reaching this unconventional naming is. It's possible the DB fields are camel case. Ultimately, the OP just needs to be consistent across all parts of his app. It's possible he is and case isn't the issue...
– Dan
Nov 14 '18 at 16:44
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 14 '18 at 15:54
DanDan
6211024
6211024
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301133%2funable-to-save-update-the-record-into-db-in-rails%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You need to leave the post_params method in as it permits the parameters. Can you posy what error message occurs when you try and update please?
– Mark
Nov 14 '18 at 13:18
I'm not sure, but it looks like your header's params are send as body. How do you call update method? Ajax async call? Html/Rails form? could you update your answer?
– Pietro Allievi
Nov 14 '18 at 15:15
Hi @Mark , I don't get any error but the data doesn't get saved in db
– Mallela SriPrakash
Nov 14 '18 at 15:20
1
@MallelaSriPrakash replace if(@metric.update_attributes(post_params)) with if(@metric.update_attributes!(post_params)) please, the server will then throw an error and explain what is going wrong
– Mark
Nov 14 '18 at 15:21
@Mark As you have said, I have replaced it. But still I don't get any error while updating. But, when I changed the text as this if(@metric.update_attributes(!post_params)) then I get this error: When assigning attributes, you must pass a hash as an argument.
– Mallela SriPrakash
Nov 14 '18 at 15:45