0

I'm trying to read parameters from html form and update some fields in the database... Html is showing data from multiple "stories" and I want to be able to change Story.estimate field... The html has text input fields for every story showing like so:

<%= text_field_tag story.id, story.estimate, :class => "input-small" %>

My idea was to name these input fields by the ID of their story and then read and update them in the controller like so:

@story.update_attribute("estimate",params[@story.id])

But this of course does not work... I need some help... There has to be a better, simpler way of doing this...

2 Answers 2

1

You should try

<%= text_field_tag "story[#{story.id}]", story.estimate, :class => "input-small" %>

and in your controller

you will get the params like this

if params[:story].present?
  id = params[:story].keys.to_i
  value = params[:story].values.first
  @story = Story.where(id: id).first
  #and then finally update the story 
  @story.update_attribute("estimate",value)

  OR

  #this will also update your story for the corresponding story id
  Story.where(id: id).update_all(estimate: value)
end
Sign up to request clarification or add additional context in comments.

5 Comments

what does that method .keys.to_i actually do?
params[:story] should be like this story => {:id => value} # {123 => "example"}
yeah I found the problem now... for some reason the params[:story] looks like this {"'123'" => example} Those double quotations make the to_i not work and it always returs 0
could you please show me the params that you are getting on your controller ?
updated my answer...removed the single quote form text_field_tag..please check now with params
0

Also, I believe update_attribute may have been removed in rails 4 in favor of update_column. So if you are running rails 4 that may be an issue. (https://github.com/rails/rails/commit/a7f4b0a1231bf3c65db2ad4066da78c3da5ffb01)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.