Rails How to make an simple link hider?
I am trying to create an simple link hider to hide external links.
I was thinking about creating a cont开发者_C百科roller named Links:
class links < ApplicationController
def index
redirect_to :(My link column)
end
end
And my database should be something like this:
ID link
1 http://stackoverflow.com
2 http://google.com
Example if I visit links/index/1 i would be redirected to http://stackoverflow.com
How do I redirect to the link that in the link column?
Assuming your Link model doesn't do anything out of the ordinary, you can do something like this...
controller.rb
def index
redirect_to Link.find(params[:id]).link
end
When you visit links/index/1
you are actually calling the show action (usually) or any other action that you specified such a route for.
What you could is, in your corresponding action,
you could make a redirect call to the content in the link column for that id (1 in your route params)
.
If you need help with a code sample, let me know. I think its pretty easy to understand the way I said.
精彩评论