11175
(-- --)
February 16, 2009, 3:27pm
1
Here's my form
<h1 id="school_name"><%=h @school.name %> (<%=h @school.place %>)</h1>
<h1 id="school_url"><%= link_to @school_url , @school.url %></h1>
<% form_for [@school , Review.new] do |f| %>
<p>
<%= f.label :first_name, "First Name" %><br />
<%= f.text_field :first_name, :length => 40 %>
</p>
<p>
<%= f.label :body, "Leave a review..." %><br />
<%= f.text_area :body, :cols => 40, :rows => 6 %>
</p>
<p><%= f.submit "Add Review" %></p>
<% end %>
<% unless @school.reviews == %>
<div id="reviews">
<%= render :partial => @school.reviews %>
</div>
<% end %>
11175
(-- --)
February 16, 2009, 3:31pm
2
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
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
11175
(-- --)
February 16, 2009, 5:35pm
4
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| %>
if @review is the object with your errors you don't need to call new -
just @review will do
Fred
11175
(-- --)
February 16, 2009, 7:30pm
6
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
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.
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
11175
(-- --)
February 17, 2009, 7:26am
9
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
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