Forum: Ruby on Rails rendering original view

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-09 20:10
  def create

    # next statement is original and is removed
    # @review = Review.new(params[:review])

    @school = School.find(params[:school_id])
    # @review = @school.reviews.create!(params[:review])
    @review = @school.reviews.build(params[:review])


    respond_to do |format|
      if @review.save

        flash[:notice] = 'Review was successfully created.'
        # format.html { redirect_to(@review) }
        format.html { redirect_to(@school) }
        format.xml  { render :xml => @review, :status => :created,
:location => @review }
      else
        # format.html { render :action => "new" }
        format.html { render :action => "new" }  #  <----- HERE
        format.xml  { render :xml => @review.errors, :status =>
:unprocessable_entity }
      end
    end
  end


In this view the line

        format.html { render :action => "new" }

renders the reviews new method when the validation fails...thing is I
want to show the school and the error messages, (schools controller show
method) like this line,  format.html { redirect_to(@school) }.

any help on how to do this?
C0dd31d9c6d343ca4ec4428a7a76ace7?d=identicon&s=25 K. R. (ribit)
on 2009-02-09 22:06
Why do you really want to render the show method of schools_controller?
Do you not render the new-action of the schools_controller? the bad form
was send from the new action of the schools controller isn't it?

format.html { render :action => "new", :controller => 'schools' }




bingo bob wrote:
>   def create
>
>     # next statement is original and is removed
>     # @review = Review.new(params[:review])
>
>     @school = School.find(params[:school_id])
>     # @review = @school.reviews.create!(params[:review])
>     @review = @school.reviews.build(params[:review])
>
>
>     respond_to do |format|
>       if @review.save
>
>         flash[:notice] = 'Review was successfully created.'
>         # format.html { redirect_to(@review) }
>         format.html { redirect_to(@school) }
>         format.xml  { render :xml => @review, :status => :created,
> :location => @review }
>       else
>         # format.html { render :action => "new" }
>         format.html { render :action => "new" }  #  <----- HERE
>         format.xml  { render :xml => @review.errors, :status =>
> :unprocessable_entity }
>       end
>     end
>   end
>
>
> In this view the line
>
>         format.html { render :action => "new" }
>
> renders the reviews new method when the validation fails...thing is I
> want to show the school and the error messages, (schools controller show
> method) like this line,  format.html { redirect_to(@school) }.
>
> any help on how to do this?
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-10 12:02
K. R. wrote:
> Why do you really want to render the show method of schools_controller?
> Do you not render the new-action of the schools_controller? the bad form
> was send from the new action of the schools controller isn't it?
>
> format.html { render :action => "new", :controller => 'schools' }
>

I'm pretty confused now as to how to do this. I'm hopig from one
controller to another and maybe that's my problem (I shouldn't do this
perhaps).

The show in Schools is what I'd like, reason is this....

I setup the up with Schools and Reviews controllers

I edit the Schools show action so that it display a form allowing
reviews to be submitted (submits to the reviews via the line above

@review = @school.reviews.build(params[:review])

works fine.

Under the form on the Schools show page I display all the reviews for
that school.. @School.reviews

works fine.

Thing is, when I introduce validation to the review model (presence of
first_name for example), it doesnt work - I'd like it when the
validation failed to return to the schools controller show method and
display the error?

Any insight appreciated.
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-10 19:58
am i just going about this in the wrong way?
This topic is locked and can not be replied to.