Forum: Ruby on Rails displaying validation messages - what do i do in my form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-16 16:27
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 %>


---


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...
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-16 16:31
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-16 17:55
(Received via mailing list)
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
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-16 18:09
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| %>
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-02-16 18:35
> 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
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-16 19:42
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...




<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 %>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-16 20:09
(Received via mailing list)
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
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-16 20:30
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-16 20:34
(Received via mailing list)
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
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-16 20:57
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-16 21:17
(Received via mailing list)
On Feb 16, 7:57 pm, bingo bob <rails-mailing-l...@andreas-s.net>
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
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-17 08:26
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-17 12:22
(Received via mailing list)
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
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-17 14:50
ignore the previous error...

basically its not looking as my schools controller even though i
specficy it.

here's the view..


<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] 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 %>




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"
This topic is locked and can not be replied to.