pandaZW pandaZW - 1 month ago 20
Ruby Question

redirect_to with additional parameters

I am a noob.

redirect_to users_url, notice: 'Succeed.'

<p id="notice"><%= notice %></p>


Then I add a
message
and it failed:

redirect_to users_url, notice: 'Succeed.', message: 'test'

<p id="notice"><%= notice %></p>
<h1><%= params[:message] %></h1> # no result
<h1><%= message %></h1> # name error


What's the differences between
users_url
and
users_path
?

Answer

You'll want to pass it in the _url or _path method:

redirect_to users_url(message: 'test'), notice: 'Succeed.'

This will redirect to the absolute path for /users, setting the flash notice to "Succeed." with an additional parameter of message set to "test".

To answer your second question, _path returns a relative path to the route whereas _url returns an absolute URL to the route:

users_url # => http://www.example.com/users
users_path # => /users

_url is useful, for example, in emails or for links that will live outside your app.

Comments