I have a site implemented with Rails and mongoDB to submit, read and update items.
There are a few different areas on the site that you can view items and filter them based on search keywords or different querying methods.
For example: on the home page there is a list of categories to sort by. 'Use Case', 'Status', 'Level'. When a user clicks the 'Use Case' button a list will show up with more specific categories for use cases such as 'E-Commerce', if they click that a div will populate with the items that have a Use Case that equals E-Commerce. From this point on a specific item they can click a 'Details' button which will show them all the properties for the item.
In this 'detailed' view I have a next and back button. Right now it scrolls through the database of items by incrementing and decrementing the item ID number.
I want the next and back button to be able to scroll through the list they were presented with in the populated div. It's accessible by an instance variable called @items
which is initialized in the items controller but can't be seen in the details view even when saved as a global variable...
The only option I have thought of is to pass the list of items through as a URL parameter - but that seems like a messy way to do it.
Is there any other way to pass an instance variable between different views?
Example code:
item_controller.rb :
def list
@items = Item.asc(:id)
end
list.html.erb :
<tbody>
<% @items.each do |item| %>
<% $list = @items%>
<tr>
<td><%= item.name %></td>
<td><% item.Use_Cases.each do |u| %>
<ul>
<li><%= u %></li>
</ul>
<% end %>
</td>
<td><%= item.Level %></td>
<td><%= item.Status %></td>
<td><%= link_to item, class: 'btn btn-default' do %>
<span class="glyphicon glyphicon-eye-open"></span> Details
<% end %>
</td>
</tr>
<% end %>
</tbody>
show.html.erb - scroll buttons :
<div class="panel-footer">
<%if @item.id != @first_id %>
<% #need to change to scroll to previous in $list %>
<%= link_to item_path(:id => (@item.id-1)), class: 'btn btn-default' do %>
<span class="glyphicon glyphicon-menu-left"></span> Back
<% end %>
<% end %>
<%if @item.id != @last_id %>
<% #need to change to scroll to next in $list %>
<%= link_to item_path(:id => (@item.id+1)), class: 'btn btn-default' do %>
Next <span class="glyphicon glyphicon-menu-right"></span>
<% end %>
<% end %>
</div>
Use the same scope to retreive a list of items and a single item with its next and previous.
# item_controller.rb
def list
@items = items_scope
end
def show
@item = items_scope.find(params[:id])
@next_item_id = items_scope.select(:id).where("id > ?", params[:id]).first
@previous_item_id = items_scope.select(:id).where("id < ?", params[:id]).last
end
private
def items_scope
# this scope might depend on url parameters which, for example, present current chosen category
# `order("id ASC")` is a very important part. it makes this solution work
Item.order("id ASC").any_other_scopes_or_conditions_depending_on_chosen_category
end
In the view:
<%= link_to item_path(:id => @next_item_id) ...
<%= link_to item_path(:id => @previous_item_id) ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.