views:

27

answers:

1

Hello. I'm diving into RoR and as I'm going through the tutorials, scaffolds, and docs, I'm coming across some code that confuses me. For example, I just read up on the 'redirect_to' method, but the guide I read didn't cover the example of redirecting to an instance var, such as the code that is generated in a typical scaffold...

# POST /articles
  # POST /articles.xml
  def create
    @article = Article.new(params[:article])

    respond_to do |format|
      if @article.save
        format.html { redirect_to(@article, :notice => 'Article was successfully created.') }
        format.xml  { render :xml => @article, :status => :created, :location => @article }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end

In the statement format.html { redirect_to(@article, :notice => 'Article was successfully created.') }, the code is redirecting to the instance var article, which causes a 'redirect_to' the show method in the current controller. Why does this cause it to redirect to the show method?

Thanks so much for your help!

+1  A: 

Why does this cause it to redirect to the show method?
Because if you don't specify particular action, Rails assumes you want to 'show' object. If you have another action in mind, try

redirect_to :action => :do_something, :id => @article
Nikita Rybak
Thanks, Nikita! I assume then that means that 'redirect_to(@article)' is equivalent to 'redirect_to(:action => :show, id => @article)' ?
BeachRunnerJoe
@BeachRunnerJoe Looks like. There's not a lot of choice for framework, since _resources :articles_ generates only 7 routes and _show_ seems like the most logical option.
Nikita Rybak