简体   繁体   中英

Can't access form data from controller

Im trying to take a specific value that the user inputs in my form and add the value to an already saved value in the database. The trouble is, I can't figure out how to access the form data and pass it to my controller.

Here's my form

  <%= form_for :song_rank, :url => {:action => "updaterank", :id => song_rank} do |form| %>


    <div class="actions">
      <%= form.select :ranknum, [1, 2, 3], :prompt => 'Score' %>
      <%= form.submit "save" %>
    </div>
  <% end %>

Here's what gets set on the POST

Parameters: {"utf8"=>"✓", "authenticity_token"=>"oGELsE7m2G+R98XPnowdbjKpEXCMn1z9iCWHR+fRVs3YtK1EXtcl0BAq4XcMFHlvL29HjPdjxk4lW3uXRXDg2g==", "song_rank"=>{"ranknum"=>"1"}, "commit"=>"save", "id"=>"13"}

The method being called in my controller is going to take the value in the database and the value submitted in the form, add them, and save the new value in the database.

This should be accessible in your controller. Try like this

params['song_rank']['ranknum']

In controller look for update method if you already have.

For Example:

def update
  if params['song_rank']['ranknum'].present?
    // update your column value here
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM