Q:

Redirect to previous page with params attached

I have a review page for schools that i link to like this;
<%= school_reviews_path(school_id: @school.id) %>

Generating a link like this;
http://address.com/school_reviews?school_id=1

However, under the reviews is a form that create a new review posting it create action of schools_reviews controller
However, after creation of the review i want to rerender the page with the accompanying parameters i.e school_id=1
I have tried several methods like this but it aint working instead it redirects to http://address.com/school_reviews without the params meanig we do not fetch the right information.
def create
    @review = SchoolReview.new(params[:review])

    respond_to do |format|
      if @review.save        
        format.html { redirect_to school_reviews_path(:school_id => @review.school_id) }
        format.json { head :no_content }  
      else
        format.html { redirect_to school_reviews_path(:school_id => @review.school_id) }
        format.json { head :no_content }  
      end
    end
  end

Any ideas, will be greatful.

A:

The problem here is that your form, most likely, isn't setting the school id anywhere, so let's make your code be more like what it should be, first setup routes like this:
resources :schools do 
  resources :school_reviews
end

Your controller name doesn't change, but the implementation will change to be as follows:
class SchoolReviewsController < ApplicationController

  before_filter :load_school

  def create
    @review = @school.school_reviews.build(params[:review])

    respond_to do |format|
      if @review.save        
        format.html { redirect_to school_school_reviews_path(@school) }
        format.json { head :no_content }  
      else
        format.html { redirect_to school_school_reviews_path(@school) }
        format.json { head :no_content }  
      end
    end
  end

  protected 

  def load_school
    @school = School.find(params[:school_id])
  end

end

You will also have to change the form_for call at your view to something like:
<%= form_for( [@school, @review] ) do |f| %>
    Your form content here.
<% end %>

Your link will change as well to:
<%= school_school_reviews_path(@school) %>

You might also want to change the SchoolReview model just to Review to remove the school_school_ from the URLs.
And, as usual, read the documentation on routes to understand what's going on.

