Reputation: 15501
Having many checks in my controller like below:
if @profile.expired
redirect_to profile_path(@profile.id)
return
end
Would it be possible to refactor this to a single line?
Also if using this code it redirects to /profiles.28
where 28 is the id How would one correctly redirect to
/profiles/show/28 or profiles/28 ?
Upvotes: 0
Views: 96
Reputation: 14750
This is most likely what you need:
redirect_to(profile_path(@profile.id)) if @profile.expired
You almost never (and by almost never, I mean effectively never) need return
statements in your controller code. If you really need this for some reason, you can do
redirect_to(profile_path(@profile.id)) && return if @profile.expired
As for your other question, I'm guessing you incorrectly specified something in your routes, and if you posted them I'd be happy to correct them.
Upvotes: 2
Reputation: 7616
I'm not fully clear about what you wanted to achieve but with my best guess, you can do it in a single line as follows:
(@profile.expired) ? (redirect_to profile_path(@profile.id) and return) : 'do something else'
Upvotes: 0