简体   繁体   中英

Rails form_for sending params to controller action and not model

In the controller review_queue I have a custom action that posts a result to a target URL, I want to build a form for this action. I am not going to save any of the fields to the DB I am just going to pass them in the params to the post_review action.

def post_review
  RestClient::Request.execute(:method => :post,
                              :url => Rails.application.secrets['target_url'],
                              :content_type => :json,
                              :payload => @result_params.merge!(params[:reasons]).to_json,
                              :headers => HEADERS)
end

In the view I have a form that will be filled out and on submit it should send up the reasons when the form is submited, I am setting the review_queue_id and the status in the form, since these are static, but the reasons should come from the textarea

<%= form_for(:review_queue, url: { action: 'post_review', :review_queue_id => @review_queue.id, :status => 'accepted'} ) do |f| %>
  <div class='form-group'>
    <label for='comment'>Please give a reason? (required)</label>
    <%= f.text_area(:reasons, placeholder: 'Your commentns ...', rows: 9, class: 'form-control') %>
  </div>
  <div class='modal-footer'>
    <%= f.submit 'Approve', class: 'btn btn-success btn-decission btn-modal-left-side'  %>
    <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
  </div>
<% end %>

error message:

NoMethodError - undefined method `reasons' for #<ReviewQueueApplication:0x007fa7ff7832d8>:

It seems as if rails is assuming the MVC architecture here, and assuming I want to pass the reasons to the review_queue model. there is no reasons column so it's dropping a no method error. Is there a way of specifying that the form is 'temporary' and only getting as far as the controller?

This seems like it should be a simple thing but there is some rails magic happening here.

The rails helper form_for is used for forms for rails resources. You want to use the form_tag helper. Search for form_for and form_tag here for more information on these 2 methods.

NoMethodError - undefined method `reasons' for ReviewQueueApplication:0x007fa7ff7832d8

form_for assumes that you are creating a form for a model object and expects the fields to be present in that specific model's table ( in a normal situation ).

You should be going with form_tag

<%= form_tag post_review_path, method: :get, :review_queue_id => @review_queue.id, :status => 'accepted'} ) do |f| %>
  <div class='form-group'>
    <label for='comment'>Please give a reason? (required)</label>
    <%= text_area_tag(:reasons, placeholder: 'Your commentns ...', rows: 9, class: 'form-control') %>
  </div>
  <div class='modal-footer'>
    <%= submit_tag 'Approve', class: 'btn btn-success btn-decission btn-modal-left-side'  %>
    <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
  </div>
<% end %>

And in the controller access it like params[:reasons] . Also if you noticed, I've added method: :get to the form_tag as you don't want to save the info to DB

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