form_for使用rails渲染而不是发布

时间:2015-01-07 02:49:07

标签: ruby-on-rails ruby forms

我有一个很长的表单,可以自动填充来自我们数据库的信息(基于用户提供的部分信息)。当用户验证信息并重新提交表单时,该信息应保存为用户。表单 - qualifying_events - 是views / shared文件夹中的部分内容。自动填充它的数据由qualifying_events_controller作为create方法的一部分呈现。部分被呈现用于在用户控制器呈现的视图/用户页面中进行验证。用户输入的初始部分信息 - 在同一页面上的类似部分 - 正确保存。这是表格的顶部:

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

以下是错误消息:

No route matches [GET] "/qualifying_events" 

以下是我尝试的内容:
1.显式添加到config / routes文件的路由,尽管已经显示我是否运行了rake路由:

post 'qualifying_events_path'     =>  'qualifying_events#create'


2.更改form_for语言以明确调用&#39; post&#39;:

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

我仍然收到相同的错误消息。由于我有其他使用相同代码保存到数据库的表单,因此当从数据库填充表单并且想要重新保存信息时,我必须假设有些更改。我正在使用设计宝石,所以我查看了注册编辑代码,希望我能遵循以下格式:

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

不幸的是,这种方法也没有用。

对于&#39;完整性&#39;,这里是qualifying_events_controller代码:

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 

其他&#39;可能会有错误。声明。我还没有添加代码来验证所有必需的信息是否存在等等。

我可以保存到数据库中并从中检索信息,但是重新提交已编辑的信息电话会得到&#39; get&#39;而不是发布。&#39;我错过了什么?提前谢谢。

2 个答案:

答案 0 :(得分:1)

删除第一个<form>元素并修复您的form_for代码:

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

到:

<%= form_for(@qualifying_event, url: qualifying_events_path, method: :post, class: 'form-inline') do |f| %>

答案 1 :(得分:0)

<form class="form-inline">之前移除form_for(以及结束标记,如果有的话)。 HTML表单无法嵌套,因此只处理具有GET方法的外部表单。

相关问题