Form_for 生成以“.1”结尾的资源路径
我有一个名为 :school 的资源,当我调用 form_for(@school) 时,它会生成表单操作:
/school.1
我对这一切都很陌生,因此任何有关其原因的线索将不胜感激。睡眠不足,还有 3 小时就要截止了,arrrggg!
谢谢:)
路线.rb:
resource:school
学校.rb:
<%= form_for(@school, :url => school_path) do |f| %>
<% if @school.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@school.errors.count, "error") %> prohibited this school from being saved:</h2>
<ul>
<% @school.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :address1 %><br />
<%= f.text_field :address1 %>
</div>
<div class="field">
<%= f.label :address2 %><br />
<%= f.text_field :address2 %>
</div>
<div class="field">
<%= f.label :address3 %><br />
<%= f.text_field :address3 %>
</div>
<div class="field">
<%= f.label :town %><br />
<%= f.text_field :town %>
</div>
<div class="field">
<%= f.label :county %><br />
<%= f.text_field :county %>
</div>
<div class="field">
<%= f.label :postcode %><br />
<%= f.text_field :postcode %>
</div>
<div class="field">
<%= f.label :local_authority_id %><br />
<%= f.collection_select :local_authority_id, LocalAuthority.all, :id, :name %>
</div>
<% if current_user.person.primary_user? %>
<div class="field">
<%= f.label 'Are you happy for us to send you regular updates about VRH?' %><br />
<%= f.check_box :allow_contact %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I have a resource called :school and when I call form_for(@school) it generates the form action:
/school.1
I'm new to all this so any clues as to why it's doing that would be much appreciated. Sleep deprived and heading for a deadline in 3 hours, arrrggg!
Thanks :)
routes.rb:
resource:school
school.rb:
<%= form_for(@school, :url => school_path) do |f| %>
<% if @school.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@school.errors.count, "error") %> prohibited this school from being saved:</h2>
<ul>
<% @school.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :address1 %><br />
<%= f.text_field :address1 %>
</div>
<div class="field">
<%= f.label :address2 %><br />
<%= f.text_field :address2 %>
</div>
<div class="field">
<%= f.label :address3 %><br />
<%= f.text_field :address3 %>
</div>
<div class="field">
<%= f.label :town %><br />
<%= f.text_field :town %>
</div>
<div class="field">
<%= f.label :county %><br />
<%= f.text_field :county %>
</div>
<div class="field">
<%= f.label :postcode %><br />
<%= f.text_field :postcode %>
</div>
<div class="field">
<%= f.label :local_authority_id %><br />
<%= f.collection_select :local_authority_id, LocalAuthority.all, :id, :name %>
</div>
<% if current_user.person.primary_user? %>
<div class="field">
<%= f.label 'Are you happy for us to send you regular updates about VRH?' %><br />
<%= f.check_box :allow_contact %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用单一资源方法,您告诉 Rails 这些对象中只存在一个,在您的路线文件中我认为您需要将资源 :school 更改为...
但是,如果您确实只想要一所学校,那么我认为您将需要将 url 选项添加到 form_for...
后续问题的建议解决方案...
我认为您需要的是这样的...
by using the singular resource method you are telling rails that only one of these objects exist, in your routes file I think you need to change your resource :school to...
If however you do want only one school then I think you will need to add the url option to form_for...
Proposed solution for follow up questions...
I think what you need will be something like this...
我的routes.rb 文件中的单个配置文件资源也遇到了类似的问题:
我花了几个小时才找到解决方案,因此我决定分享它以节省其他人的麻烦。
我必须通过指定特定格式来删除路线的“(.:format)”部分(在运行“rake paths”时显示):
我还必须将 :url 选项添加到 form_for标签:
这有效。
I just had a similar issue with a singular profile resource in my routes.rb file:
It took me a few hours to find a solution so I decided to share it to save someone else the trouble.
I had to remove the "(.:format)" part of the route (shown when running "rake routes"), by specifying a specific format:
I also had to add the :url option to form_for tags:
and that worked.