form_for renders get rather than post, using rails

2019-09-04 11:37发布

I have a long form that auto-fills with information from our database (based upon partial information supplied by a user). When the user verifies the information and re-submits the form, the information should be saved as the users. The form - qualifying_events - is a partial that's in the views / shared folder. The data that autofills it is rendered by the qualifying_events_controller as part of the create method. The partial is presented for verification in a views / users page that's rendered by the user controller. The initial, partial information input by a user - in a similar partial on the same page - is correctly saving. Here's the top of the form:

<form class="form-inline">
  <%= form_for(@qualifying_event) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
<div >   
  <div class="form-group col-sm-12 text-center" style="margin-bottom: 5px;"><h5>
    <div>
      <li style="list-style-type: none; float: left; display: none; visibility: hidden;">
      <%= current_user %>
      </li>
    </div>
    <div>
      <li style="list-style-type: none; float: left;">
      <%= f.label :class_date %><br/>
      <%= f.text_area :class_date, autofocus: true %>
      </li>
    </div> 

Here is the error message:

No route matches [GET] "/qualifying_events" 

Here's what I've tried:
1. Explicitly adding a route to the config / routes file although it already showed if I ran rake routes:

post 'qualifying_events_path'     =>  'qualifying_events#create'


2. Changing the form_for language to explicitly call 'post':

<%= form_for(@qualifying_event, url: qualifying_events_path, method: => post) do |f| %>

I still get the same error message. Since I have other forms that are saving to the database with the same code, I have to assume something changes when a form is populated from a database and one wants to re-save the information. I'm using the devise gem so I looked at the registration#edit code, hoping I could follow the format:

  <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>

Unfortunately, that approach didn't work either.

For 'completeness', here's the qualifying_events_controller code:

def create 
  @user = current_user
  @qualifying_event = current_user.qualifying_events.build(qualifying_event_params) 

  if @qualifying_event.validated.nil?
  # Match partial course information from current_user with records in table

  # Get partial record's information
  active_date = @qualifying_event.class_date
  active_course = @qualifying_event.course_id
  active_sponsor = @qualifying_event.sponsor_name

  # Match on class_date, course_id, and sponsor_name
  @qualifying_event = QualifyingEvent.new
  @qualifying_event.assign_attributes(QualifyingEvent.
      where("(class_date = :class_date AND course_id = :course_id) 
      OR (class_date = :class_date AND sponsor_name = :sponsor_name) 
      OR (course_id = :course_id AND sponsor_name = :sponsor_name)",
      {class_date: active_date, course_id: active_course, 
          sponsor_name: active_sponsor}).first.attributes)

  # render to confirmation form / form for completion
  flash[:success] = "Qualifying Event saved for verification!"
  respond_to do |format|
    format.html  {render 'users/user_dashboard' }
  end

else
  # if the record is complete          
  @qualifying_event.save
  flash[:success] = "Qualifying Event created!"
  redirect_to user_dashboard_path
end 

There may be an error in the 'else' statement. I haven't yet added the code to verify that all the required information is present, etc.

I can save to and retrieve information from the database, however a re-submit of edited information calls 'get' rather than 'post.' What am I missing? Thank you in advance.

2条回答
太酷不给撩
2楼-- · 2019-09-04 12:29

Remove <form class="form-inline"> (and the closing tag if any) before form_for. HTML forms can't be nested, thus only the outer form are processed which is have GET method.

查看更多
等我变得足够好
3楼-- · 2019-09-04 12:34

Remove the first <form> element and fix your form_for code from:

<%= form_for(@qualifying_event, url: qualifying_events_path, method: => post) do |f| %>

to:

<%= form_for(@qualifying_event, url: qualifying_events_path, method: :post, class: 'form-inline') do |f| %>
查看更多
登录 后发表回答