I have the following situation in my rails project
application.html.erb:
...
<%= render :partial => 'layouts/sidebar' %>
<%= yield %>
...
_sidebar.html.erb:
<% #groups.each do |group| %>
<li><%= link_to group.name, "#" %></li>
<% end %>
Sidebar consists of a list of groups which doesn't change often. That's why i don't want to query the list in DB every time I go to another page in content part (yield). Is there a way how can i preserve the list throughout several pages? I KNOW about the sessions and caches, but maybe there is a better solution.
Thanks
Related
I'm developing an online course site using Ruby on Rails and bootstrap. I have a model for Courses and a model for Lessons. In the lesson controller show view I am not only showing the lesson page with video, discussion and notes but also a list of all the lessons belonging to the course. This is done by:
<%= #lessons.each do |lesson| %>
<%= link_to [lesson.course, lesson] do %>
<%= lesson.title %>
<% end %>
<% end %>
The user can, from the individual lesson page, pick the next lesson to watch.
Question:
The URL generated is: localhost:3000/courses/1/lessons/2 (I'll change this with Friendly Id gem later). I would like to show in the list of all lessons on the individual lesson page which lesson the user is watching right now. So basically maybe have something say "You are currently watching : Lesson with id 2" and have it in a different background color with some custom html. How can I have different HTML and CSS for the currently watching lesson in the all lessons list?
Thanks a lot in advance!
/Jacob
A simple way would be to add a conditional, in the case you have the current lesson available in a local variable or method current_lesson. Then it can look like this:
<%= #lessons.each do |lesson| %>
<% if lesson.id == current_lesson.id %>
... other html ...
<% else %>
<%= link_to [lesson.course, lesson] do %>
<%= lesson.title %>
<% end %>
<% end %>
<% end %>
I have a page that uses a lot of nested partials to display content according to user type and product status across a range of categories. Is it a good idea to manage this using nested partials and conditional status? I wonder if there is a performance hit to all of this?
For example - product.html.erb:
<% if product.status == "available" %>
<%= render available_product %>
<% else %>
<%= render manage_supplier %>
<% end %>
And available_product.html.erb:
<% if #user.role == owner %>
<%= render product_headline_form %>
<% else %>
<%= render product_headline_show %>
<% end %>
<%= render some_nice_stuff_from_supplier %>
... and on.
I'm keeping my partials pretty atomised 'cos I figure it will give me more flexibility when I come to add ajax in places.
that depends on
the ruby version
the rails version
your partials
in other words: you have to measure
in general: partials tend to be much slower than ie helpers because rails has to do much more work to setup the rendering context for a partial than it has to do if there are none.
is it relevant? depends on your requirements.
Please help me.
My code is
<%= Region.find(:all).each do |myregion| %>
<li><%= t(myregion.name) %></li>
Database has only two records: japan and korea.But, it shows other information outside the loop like [#, #]... i dont want these information on my website unnecessarily.
How to remove this?
instead of
<%= Region.find(:all).each do |myregion| %>
use
<% Region.find(:all).each do |myregion| %>
<%= %> # render string
<% %> # execute code
I'm using the Enki blogging gem as a type of content management system. It allows you to create posts and pages. It automatically generates two pages (Home and Archives). I've also created two other example pages, Services and Products, and will create many more. Therefore, when I want to list all the pages on the home page, I do this
<% page_links_for_navigation.each do |link| -%>
<li><%= link_to(link.name, link.url) %></li>
<% end -%>
Home
Archives
Services
Products
I may want to create more pages in the future, so it's better to loop over all the pages like this rather than hardcode the url for each page.
But how would I change that code if I wanted to exclude one of those pages (i.e. archives). Enki automatically generates that page and doesn't give me an option to delete it. Moreever, I don't want to delete Archives, because I want to use it where I post link to blog posts.
So, in short, how would I exclude one particular page from this code
<% page_links_for_navigation.each do |link| -%>
<li><%= link_to(link.name, link.url) %></li>
<% end -%>
The url for Archives is localhost:3000/archives
another way
<% page_links_for_navigation.each do |link| -%>
<% next if link.name == 'Archives' %>
<li><%= link_to(link.name, link.url) %></li>
<% end -%>
<% page_links_for_navigation.each do |link| -%>
<% if link.name != 'Archives' %>
<li><%= link_to(link.name, link.url) %></li>
<% end %>
<% end -%>
or use page_links_for_navigation.reject {|page| page.name == 'Archives'}.each
Edit:
to add more pages do !['Archives', 'Home'].include? link.name or just add the ones you want to include and remove !
read
http://www.humblelittlerubybook.com/
http://www.ruby-doc.org/docs/ProgrammingRuby/
I have a list of books that displays edit buttons and a bunch of extra info if the user that's logged in is an admin. Right now I have two separate partials that are rendered depending on what type of user is logged in. I used to have just one partial with a bunch of if user.admin? statements, but it started to get real ugly. Now I am juggling around two files, with little bits of duplicate data in each. Is there any better way to do this?
index.html.erb
<ul>
<% if #current_user.admin? %>
<%= render :partial => "book", :collection => #books %>
<% else %>
<%= render :partial => "non_admin_book", :collection => #books %>
<% end %>
</ul>
_book.html.erb
Title: <%= book.title %> EDIT BUTTON
<!-- Awesome extra info for admins -->
Author: <%= book.author %>
<!-- Awesome extra info for admins -->
_non_adminbook.html.erb
Title: <%= book.title %>
Author: <%= book.author %>
This question is like : should I only use I18n keys all over one partial/view or should I use X views/partials for each language ?
There is no good or bad solution. My opinion is that you should begin by using conditionals like <% if admin? %> blah blah <% end %>...
Then, if your admin view grandly differs from your non admin views, delete the conditionals and make two views : my_view / my_view_admin.
#192 Authorization with CanCan this cancan gem may help you
I really do not like any kind of duplication, but sometimes it is the easiest solution.
In your case, I can tell that
an administrator has the option to edit fields (inline?)
an administrator sees more fields
Generally I use the on_the_spot gem for inline editing, and I then work with a helper like this:
def on_the_spot_edit_if_allowed(object, field, options)
if current_user.admin?
on_the_spot_edit object, field, options
else
object.send(field)
end
end
And in that case my views becomes something like
Title: <%= on_the_spot_edit_if_allowed book, title %>
<%- if current_user.admin? %>
<!-- Awesome extra info for admins -->
<% end %>
Author: <%= book.author %>
<%- if current_user.admin? %>
<!-- Awesome extra info for admins -->
<% end %>
Unless it is otherwise (design/UI constraints) impossible, I would refactor that view to the following:
Title: <%= on_the_spot_edit_if_allowed book, title %>
Author: <%= book.author %>
<%- if current_user.admin? %>
<%= render :partial => 'extra_admin_fields'
<% end %>
Hope this helps.
Keep it like it is.
Your duplications are not that big.
The #current_user.admin? condition will be run only once with your solution.
If you put #current_user.admin? in a shared partial, it will be run for every single member of this collection. Not cool.