Form_for generates resource path ending with '.1'
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_au开发者_Python百科thority_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 %>
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 %>
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.
精彩评论