gpt4 book ai didi

ruby-on-rails - form_for生成以 '.1'结尾的资源路径

转载 作者:行者123 更新时间:2023-12-04 02:23:05 25 4
gpt4 key购买 nike

我有一个名为 :school 的资源,当我调用 form_for(@school) 时,它会生成表单操作:

/学校.1

我是这一切的新手,所以任何关于它为什么这样做的线索将不胜感激。 sleep 不足,还有 3 小时就要 catch 最后期限了,哎呀!

谢谢:)

路线.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 %>

最佳答案

通过使用单一资源方法,您告诉 rails 这些对象中只有一个存在,在您的路由文件中,我认为您需要将资源 :school 更改为...

resources :schools

如果您只想要一所学校,那么我认为您需要将 url 选项添加到 form_for...

<% form_for(@school, :url => school_path) %>

后续问题的建议解决方案...

我想你需要的是这样的东西......

# routes.rb
resources :users do
resources :schools
end
resources :schools

# app/controllers/schools_controller.rb
class SchoolsController < ApplicationController
def new
@user = User.find(params[:user_id])
@school = @user.build_school
end

def create
@user = User.find(params[:user_id])
@school = @user.build_school(params[:school])
if @school.save
# success...
else
# fail...
end
end

def edit
@school = School.find(params[:id])
end

def update
@school = School.find(params[:id])
if @school.update_attributes(params[:school])
# success
else
# fail
end
end
end

# app/views/schools/new.html.erb
<%= form_for([@user, @school]) do |f| %>
<!-- fields go here -->
<% end %>

# app/views/schools/edit.html.erb
<%= form_for([@user, @school]) do |f| %>
<!-- fields go here -->
<% end %>

关于ruby-on-rails - form_for生成以 '.1'结尾的资源路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7597790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com