Rails 如何根据实例变量更改视图?

发布于 2024-12-12 03:12:22 字数 772 浏览 0 评论 0原文

我有这个域搜索操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆兒滾 2024-12-19 03:12:22

参数在两个操作中都可用,您不需要将它们“发送”到第二个方法;他们已经在那里了。

如果满足您的条件,只需调用第二种方法,否则执行您已经在执行的操作。

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.

惜醉颜 2024-12-19 03:12:22

我想写一个 before_filter,但经过一想,使它成为一个动作会更加干燥......

def domain
  codes = get_tld(params[:domain]) || country_codes
  codes.each do |c|
    @results[c] = Whois.whois(@domain + c)
  end
    render :layout => false
  end
end

# return array with one element if matched, else nil
def get_tld(string)
  country_codes.each{|cc| return [cc] if string.end_with?(cc)}
  nil
end

#contry_codes should be defined somewhere else...

我不明白 @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...

def domain
  codes = get_tld(params[:domain]) || country_codes
  codes.each do |c|
    @results[c] = Whois.whois(@domain + c)
  end
    render :layout => false
  end
end

# return array with one element if matched, else nil
def get_tld(string)
  country_codes.each{|cc| return [cc] if string.end_with?(cc)}
  nil
end

#contry_codes should be defined somewhere else...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文