M1nt_zwy
M1nt_zwy

Reputation: 927

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?

Upvotes: 2

Views: 5983

Answers (1)

CDub
CDub

Reputation: 13354

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.

Upvotes: 4

Related Questions