Form_for 生成以“.1”结尾的资源路径

发布于 2024-12-07 18:23:18 字数 2143 浏览 0 评论 0原文

我有一个名为 :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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朕就是辣么酷 2024-12-14 18:23:19

通过使用单一资源方法,您告诉 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 %>

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

resources :schools

If however you do want only one school then I think you will need to add the url option to form_for...

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

Proposed solution for follow up questions...

I think what you need will be something like this...

# 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 %>
夢归不見 2024-12-14 18:23:19

我的routes.rb 文件中的单个配置文件资源也遇到了类似的问题:

resource :profile

我花了几个小时才找到解决方案,因此我决定分享它以节省其他人的麻烦。
我必须通过指定特定格式来删除路线的“(.:format)”部分(在运行“rake paths”时显示):

constraints :format => "html" do  
  resource :profile  
end  

我还必须将 :url 选项添加到 form_for标签:

<% form_for(@profile, :url => profile_path) %>

这有效。

I just had a similar issue with a singular profile resource in my routes.rb file:

resource :profile

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:

constraints :format => "html" do  
  resource :profile  
end  

I also had to add the :url option to form_for tags:

<% form_for(@profile, :url => profile_path) %>

and that worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文