Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails redirect_to arguments

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?

like image 605
randombits Avatar asked Oct 14 '22 08:10

randombits


1 Answers

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.

like image 128
ajmurmann Avatar answered Oct 19 '22 01:10

ajmurmann