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

后端 未结 4 1244
别跟我提以往
别跟我提以往 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条回答
  •  臣服心动
    2021-01-31 17:36

    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, tell the partial what you want it to do.

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

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

    Then in the partial:

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

提交回复
热议问题