views:

282

answers:

2

There is always a degree of black magic when it comes to Rails and I can't find the documentation to help me figure out this one. What redirect_to does is clearly straight forward. In fact, this question isn't even directly related to, but an argument I see passed to redirect_to often and can't understand where that argument is coming from. For instance, if you scaffold a new object, let's say 'user', you'll see some code like this in user_controller.rb:

  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml  { head :ok }
    end
  end

Question is, what exactly is users_url here? Where does it come from? Can someone point me in the right direction in as far as documentation goes?

+1  A: 

There are many named routes that Rails automatically generates if you use resource based routes. You can see those by running rake routes. Other examples would be edit_user, userandnew_user. By appending _url to that name, the name will be resolved to the matching url. And the url is what redirect_to needs.

ajmurmann
Some useful reading material on this can be found at RailsGuides: http://guides.rubyonrails.org/routing.html
Josh Lindsey
That last statement isn't true. `redirect_to` will happily take a path. Use the `*_url` form if you need the full URL rather than a relative path.
John Topley
+1  A: 

Hi,

The users_url in this case is the full URL path to the view users. This is autogenerated by Rails action_dispatch based on your domain object. If you are interested on the documentation on this, you can read it from here.

HTH

jpartogi