Ruby-on-rails 基于控制器动作的 Rails 条件 ('if') 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1329122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:40:16  来源:igfitidea点击:

Rails conditional ('if') statements based on controller action

ruby-on-rails

提问by Elliot

There might be a better way to do this, but I'm trying to make an if statement in rails, based on the current action, in a controller (this will be used in a view).

可能有更好的方法来做到这一点,但我正在尝试根据当前操作在控制器中在 rails 中创建一个 if 语句(这将在视图中使用)。

For example, if its the edit page, or the show page, etc. I'd like a different style for something - is there an if statement that can specify this?

例如,如果它是编辑页面,或显示页面等。我想要不同的样式 - 是否有可以指定这一点的 if 语句?

(I need an if statement, because its used in a partial, on multiple pages).

(我需要一个 if 语句,因为它在多个页面上的部分使用)。

Thanks!

谢谢!

Elliot

艾略特

回答by Daniel Vandersluis

The paramshash that is available in the controller contains :controllerand :actionkeys, which specify the controller and action names of the request.

params控制器中可用的哈希包含:controller:action键,它们指定请求的控制器和操作名称。

Therefore you could say

因此你可以说

if params[:action] == "foo"
  # Show stuff for action 'foo'
elsif params[:action] == "bar"
  # Show stuff for action 'bar'
elsif ...
  # etc.
end

回答by showaltb

It's not good practice IMO to have partials asking what the current controller and action names are. Think "tell, don't ask" (http://www.pragprog.com/articles/tell-dont-ask). That is, rather than having the partial ask it's caller about its state, tellthe partial what you want it to do.

让partials询问当前的控制器和动作名称是什么并不是IMO的好习惯。想想“告诉,不要问”(http://www.pragprog.com/articles/tell-dont-ask)。也就是说,与其让局部询问它的调用者关于它的状态,不如告诉局部你想要它做什么。

One way to do this is by passing variables to the partial through the localsoption:

一种方法是通过locals选项将变量传递给部分:

<%= render :partial => "/common/toolbar", :locals => {:edit => true} %>

Then in the partial:

然后在部分:

<% if defined?(edit) && edit %>
... stuff appropriate to edit mode
<% end %>

回答by Staelen

You can do it this way:

你可以这样做:

class ApplicationController < ActionController::Base
  layout :set_layout

  def set_layout
    case params[:action]
    when "foo"
      "foo_layout"
    when "bar"
      "bar_layout"
    ...
    else
      "default_layout"
    end
  end

  ...

end

hope it helps =)

希望它有帮助 =)

回答by jonnii

You can use layouts for partials too:

您也可以为部分使用布局:

<%= render :partial => 'some_partial', :layout => 'wrap_with_stuff' %>

If you want to work out what layout to use dynamically I'd put that in a helper. So you'd end up with

如果你想确定动态使用什么布局,我会把它放在一个助手中。所以你最终会得到

# In your view

<%= render :partial => 'some_partial', :layout => layout_for_my_partial %>

# In your helper

def layout_for_my_partial
    params[:action] == 'show' ? 'show_wrapper' : 'everything_else_wrapper'
end

This will only work in some circumstances, but might be what you're trying to do.

这仅在某些情况下有效,但可能是您想要做的。

See more here.

在这里查看更多。

http://ryandaigle.com/articles/2007/8/3/what-s-new-in-edge-rails-partials-get-layouts

http://ryandaigle.com/articles/2007/8/3/what-s-new-in-edge-rails-partials-get-layouts