Rails 如何根据实例变量更改视图?
我有这个域搜索操作:
def domain
country_codes = ['.dk', '.com', '.eu', '.net', '.org', '.biz', '.info', '.nu', '.name', '.se', '.fi', '.net', '.de', '.it'] # etc. could move this to a config if needed
@domain = params[:domain]
@results = {}
country_codes.each do |cc|
@results[cc] = Whois.whois(@domain + cc)
end
render :layout => false
end
如果 params[:domain]
是“asdasdasd”或“something”,我想呈现默认视图。
但是,如果 params[:domain]
是示例“asdasd.dk”或“asdasdasd.com”,我想呈现此操作并将域参数发送到此操作:
def domainname
@tld = "get the tld"
country_codes = [@tld]
@results = Domains.order("#{@tld} ASC")
country_codes.each do |cc|
@results[cc] = Whois.whois(@domain + cc)
end
render :layout => false
end
I have this domain search action:
def domain
country_codes = ['.dk', '.com', '.eu', '.net', '.org', '.biz', '.info', '.nu', '.name', '.se', '.fi', '.net', '.de', '.it'] # etc. could move this to a config if needed
@domain = params[:domain]
@results = {}
country_codes.each do |cc|
@results[cc] = Whois.whois(@domain + cc)
end
render :layout => false
end
If the params[:domain]
are "asdasdasd" or "something" I want to render the default view.
But if the params[:domain]
are example "asdasd.dk" or "asdasdasd.com" I want to render the this action and send the domain params to this action:
def domainname
@tld = "get the tld"
country_codes = [@tld]
@results = Domains.order("#{@tld} ASC")
country_codes.each do |cc|
@results[cc] = Whois.whois(@domain + cc)
end
render :layout => false
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
参数在两个操作中都可用,您不需要将它们“发送”到第二个方法;他们已经在那里了。
如果满足您的条件,只需调用第二种方法,否则执行您已经在执行的操作。
The params are available in both actions, you don't need to "send" them to the second method; they're already there.
Just call the second method if your conditions are met, otherwise do what you're already doing.
我想写一个 before_filter,但经过一想,使它成为一个动作会更加干燥......
我不明白 @results = Domains.order("#{@tld} ASC") 应该做什么,但是如果你需要对结果做一些事情,以防你的参数中有 tld,你可以随时检查codes.size==1
I wanted to write a before_filter, but after a thought, making it a one action will be more DRY...
I don't understand what @results = Domains.order("#{@tld} ASC") is supposed to do, but if you need to do something with results in case you have tld in params, you can always check codes.size==1