Here’s my form
<%=h @school.name %> (<%=h @school.place %>)
<%= link_to @school_url, @school.url %>
<% form_for [@school, Review.new] do |f| %>
<%= f.label :first_name, "First Name" %>
<%= f.text_field :first_name, :length => 40 %>
<%= f.label :body, "Leave a review..." %>
<%= f.text_area :body, :cols => 40, :rows => 6 %>
<%= f.submit "Add Review" %>
<% end %>
<% unless @school.reviews == [] %>
<%= render :partial => @school.reviews %>
<% end %>
my routes
map.resources :schools, :has_many => :reviews
and my controller for reivews
class ReviewsController < ApplicationController
def create
@school = School.find(params[:school_id])
@review = @school.reviews.build(params[:review])
@review.save
redirect_to @school
end
end
I’d like to show validation errors in the initial form, so how do I do
the line…
<% form_for [@school, Review.new] do |f| %>
Is that right?
I realise i need to update the controller to render new if the record
doesnt save…
so here’s my reviews controller, i’d like to return to the schools show
action and show errors if the record not saved.
class ReviewsController < ApplicationController
def create
@school = School.find(params[:school_id])
@review = @school.reviews.build(params[:review])
if @review.save
redirect_to @school
else
render :action => "new"
end
end
end
On 16 Feb 2009, at 15:27, bingo bob wrote:
I’d like to show validation errors in the initial form, so how do I do
the line…
<% form_for [@school, Review.new] do |f| %>
You don’t want Review.new. You want to use the instance of review that
had the errors on it. This will also means that the text boxes etc.
will be filled with what the user typed in before. You can use
f.error_messages_for to display the errors or you can roll your own
thing if you don’t like the output that generates but the key thing is
that you need to use th instance which has errors on it.
Fred
But this doesn’t work for me…
<% form_for [@school, @review.new] do |f| %>
Your form_for should be using @school, @review.
In the controller new method, just like you do a (I hope)
@school = School.new
do a
@review = Review.new
OK, kind of got you…appreciate i need the instance with the errors in
it, how do i achieve that?
so not this…
<% form_for [@school, Review.new] do |f| %>
But this doesn’t work for me…
<% form_for [@school, @review.new] do |f| %>
there is no new method…
just create… here’s my reviews controller
class ReviewsController < ApplicationController
def create
@school = School.find(params[:school_id])
@review = @school.reviews.build(params[:review])
if @review.save
redirect_to @school
else
render :action => "new"
end
end
end
and here’s my school show view…
<%=h @school.name %> (<%=h @school.place %>)
<%= link_to @school_url, @school.url %>
<% form_for [@school, Review.new] do |f| %>
<%= f.label :first_name, "First Name" %>
<%= f.text_field :first_name, :length => 40 %>
<%= f.label :body, "Leave a review..." %>
<%= f.text_area :body, :cols => 40, :rows => 6 %>
<%= f.submit "Add Review" %>
<% end %>
<% unless @school.reviews == [] %>
<%= render :partial => @school.reviews %>
<% end %>
On 16 Feb 2009, at 17:09, bingo bob wrote:
<% form_for [@school, @review.new] do |f| %>
if @review is the object with your errors you don’t need to call new -
just @review will do
Fred
On 16 Feb 2009, at 19:30, bingo bob wrote:
if @review.save
redirect_to @school
else
# what goes here ???
end
exactly what you had before - just render your ‘new’ action
Fred
i don’t have a new action or view in my reviews controller…
my comments are being entered and displayed on the schools show page.
ok thanks fred… but…
class ReviewsController < ApplicationController
def create
@school = School.find(params[:school_id])
@review = @school.reviews.build(params[:review])
if @review.save
redirect_to @school
else
# what goes here ???
end
end
end
On Feb 16, 7:57 pm, bingo bob [email protected]
wrote:
i don’t have a new action or view in my reviews controller…
my comments are being entered and displayed on the schools show page.
then pass whatever you need to pass to render to get it to render the
form again. You know where that file is, I don’t
Fred
Arrrr! I see…sorry being dopey, just enhance the render command - I
read you can do this elesewhere…
BUT, It still deosnt work!
class ReviewsController < ApplicationController
def create
@school = School.find(params[:school_id])
@review = @school.reviews.build(params[:review])
if @review.save
redirect_to @school
else
render :action => "show", :controller => "schools" # here....!!!!
end
end
end
===== it’s trying to go here…
Showing app/views/reviews/show.html.erb where line #42 raised:
undefined method `edit_review_path’ for #ActionView::Base:0x21c2260
It should go back to here I suppose…
http://localhost:3000/schools/2
On 17 Feb 2009, at 07:26, bingo bob wrote:
Arrrr! I see…sorry being dopey, just enhance the render command - I
read you can do this elesewhere…
which bit of the view is causing this ?
Fred
ignore the previous error…
basically its not looking as my schools controller even though i
specficy it.
here’s the view…
<%=h @school.name %> (<%=h @school.place %>)
<%= link_to @school_url, @school.url %>
<% form_for [@school, @review] do |f| %>
<%= f.label :first_name, "First Name" %>
<%= f.text_field :first_name, :length => 40 %>
<%= f.label :body, "Leave a review..." %>
<%= f.text_area :body, :cols => 40, :rows => 6 %>
<%= f.submit "Add Review" %>
<% end %>
<% unless @school.reviews == [] %>
<%= render :partial => @school.reviews %>
<% end %>
ad here’s the cont.
class ReviewsController < ApplicationController
def create
@school = School.find(params[:school_id])
@review = @school.reviews.build(params[:review])
if @review.save
redirect_to @school
else
render :controller => "schools", :action => "show"
end
end
end
result is this when validation fails…
Template is missing
Missing template reviews/show.erb in view path
/Users/rupe/Sites/schools/app/views:
which is weird because I said… render :controller => “schools”,
:action => “show”