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

后端 未结 4 1344
别跟我提以往
别跟我提以往 2021-01-31 16:59

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).

For

4条回答
  •  Happy的楠姐
    2021-01-31 17:54

    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

提交回复
热议问题