Question

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...

Was it helpful?

Solution

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

OTHER TIPS

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top