form_for渲染获取而不是发布,使用rails [英] form_for renders get rather than post, using rails
问题描述
我有一个很长的表单,可以自动填充来自我们数据库的信息(基于用户提供的部分信息)。当用户验证信息并重新提交表单时,信息应该保存为用户。表单 - qualifying_events - 是视图/共享文件夹中的一部分。自动填充它的数据由qualifying_events_controller作为create方法的一部分呈现。该部分被呈现用于在由用户控制器呈现的视图/用户页面中进行验证。由用户输入的初始部分信息 - 在同一页面上的相似部分 - 正确保存。以下是表单的顶部:
< form class =form-inline>
<%= form_for(@qualifying_event)do | f | %GT;
<%= render'shared / error_messages',object:f.object%>
< div>
< div class =form-group col-sm-12 text-centerstyle =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>
以下是错误讯息:
没有路线匹配[GET]/ qualifying_events
我已经尝试了:
1.明确地向config / routes文件添加一个路由,尽管它已经显示了我是否运行了rake路由:
post'qualifying_events_path'=> 'qualifying_events#create'
2.将form_for语言改为显式调用'post':
<%= form_for(@qualifying_event,url:qualifying_events_path,method:=> post) | F | %GT;
我仍然收到相同的错误讯息。由于我有其他形式使用相同的代码保存到数据库,因此当从数据库填充表单并想重新保存信息时,我必须假设进行了一些更改。我使用的是设计宝石,所以我看着注册#编辑代码,希望我可以按照以下格式:
< %= form_for(resource,as:resource_name,url:registration_path(resource_name),html:{method :: put})do | f | %GT;
不幸的是,这种方法也没有奏效。
$ b $ p
对于'完整性',以下是qualifying_events_controller代码:
def创建
@user = current_user
@qualifying_event = current_user.qualifying_events.build(qualifying_event_params)
如果@ qualifying_event.validated.nil?
#将来自current_user的部分课程信息与表中的记录进行匹配
#获取部分记录的信息
active_date = @ qualifying_event.class_date
active_course = @ qualifying_event.course_id
active_sponsor = @ qualifying_event.sponsor_name
#在class_date,course_id和sponsor_name上匹配
@qualifying_event = QualifyingEvent.new
@ qualifying_event.assign_attributes(QualifyingEvent。
其中((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)
#渲染完成确认表单/表单
flash [:success ] =为验证而保存的合格事件!
respond_to do | format |
format.html {render'users / user_dashboard'}
结束
其他
#如果记录完成
@ qualifying_event.save
flash [:success] =创建的资格赛事!
redirect_to user_dashboard_path
end
可能在'else'声明。我还没有添加代码来验证所有需要的信息是否存在等。
我可以保存并从数据库中检索信息,提交编辑的信息电话'get'而不是'post'。我错过了什么?
删除< form class =form-inline>
(以及结束标记,如果有的话)在 form_for
之前。 HTML表单不能嵌套,因此只处理具有 GET
方法的外部表单。
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.
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.
这篇关于form_for渲染获取而不是发布,使用rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!