Can I redirect from a layout file to a controller? or?
Currently there is code in one of my layout views something like this.
<%- pending_items = items.pending.count
if pending_reviews > 0 -%>
<div id="notice">
<%= link_to("YOU HAVE #{pluralize(user_pending_items, 'PENDING ITEMS')}", user_item_path) %>
</div>
<%- end -%>
I need to expand 开发者_运维技巧the code with a few more conditions and then if a certain condition is met redirect to another action. Here are a few questions:
- if there is a certain condition met, could i redirect back to a controller from the layout file? i haven't found anything that works yet.
- also, would it make more sense to run all these conditions in a controller? I tried placing it in my home_controller but the layout was not picking up the values.
Thank you.
Looks like code that should be moved to a controller. If you do this for every page, you can move it to application controller, into a before filter (about filters). Remember that controller should set instance variables (those with @
at the beginning) if you wish to use them in view.
I would recommend placing the logic in your controller or model. You will have to use instance variables to pass values to your layout:
# in your controlller @pending_items = Item.pending.count if @pending_items > 0 ... redirect_to ... else ... redirect_to ... end
The @pending_items instance variable will be accessible in your layout.
精彩评论