Reputation: 3444
How do I Change the URL for the view in the controller.
In my controller I generate an ID which I want to display
in the URL of the browser when the view is rendered.
For example, when I enter / in my browser, it should redirect me
to /test/1. The ID is generated randomly by the controller.
So when I access / the 2nd time it could redirect me to /test/3.
I tried to do a match route in my routes.rb file.
But I couldn't find a solution.
routes.rb
get 'test/run'
root to: 'test#run'
match '/test/:id', to: 'test#run'
Upvotes: 0
Views: 678
Reputation: 4394
How about that:
class TestController
def show
...
end
def run
redirect_to Test.random
end
end
Of course, you have to write random
scope for your Test model.
You may find helpful this question also - Random record in ActiveRecord
P.S. I'm not sure that the Test
is a good name for your model. It could be already used by ruby or rails.
Upvotes: 2