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?
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, user
andnew_user
. By appending _url
to that name, the name will be resolved to the matching url. And the url is what redirect_to needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With