ruby on rails 3.1 fragment caching - ruby

I have an app and I'd like to cache some parts of the page, and I have been reading a lot about the ways to do this.
I understand that fragment caching is the best way to do it on my project, but I canĀ“t find a simple example to learn how to implement this.
I would like to use the fragment cache with autoexpire.
<% cache(:action => 'recent', :action_suffix => 'all_products') do %>
All available products:
<% Product.all.each do |p| %>
<%= link_to p.name, product_url(p) %>
<% end %>
<% end %>
Where do I set the autoexpire? any examples around? how can I do this?

In your product model you can do something like this
after_save :expire_caches
after_destroy :expire_caches
# can't do this in a sweeper since there isn't a controller involved
def expire_caches
ActionController::Base.cache_store.delete_matched(%r{product\?for=\d+&fragment=products})

Related

Separating Ruby Code which requires a block with Partials

I want to use
<% form_tag ( <<variables from partial here>> ) do %>
on a partial; problem is on the IDE it already is trying to tell me I have invalid code; this is because the "DO" is to be paired with an "END". I cannot end on the partial itself; because there is some content after the "DO" which is on the page where the partial is called.
How do I use the form_tag in the partial while using the content I have from the page which called the partial?
Sounds like you're in a very sticky situation. This is what I think you've got from your description:
#partial_1
<% form_tag (:model_name) do %>
#partial_2
<% form_contents %>
<% end %>
#controller
#model = Model.load
render(partial1 << partial2)
You may have to correct me if I'm wrong, but is it possible instead to do this?
#controller
#model = Model.load
render(partial_1)
#partial_1
<% form_tag ... %>
<% render(partial_2) %>
<% end %>
#partial_2
<% all the form guts %>
If you're using straight ruby you're probably using the ERB library and you are binding every time, which should keep the variables live all the way through.

How to dry code?

I am working with has many through association in Application.
I access email from employee table in INDEX action of InventoryController like below code:
<% #inventories.each do |inventory| %>
<% inventory.employee_inventories.each do |e| %>
<%if e[:status]== 'ALLOTED'%>
<%= e.employee.email%>
<% end %>
<% end %>
<% end %>
Please help me How to DRY this code in view? Thanks in advance
#alloted_emails = #inventories.flat_map(&:employee_inventories).select do |i|
i[:status] == 'ALLOTED'
end.map do |i|
i.employee.email
end
Unless you want to create a scope as suggested by #Pavan, just introduce this var and use it.
If you are using the same code in different views then you can create a helper.
Otherwise write down this code like-
<% #inventories.each do |inventory| %>
<% inventory.employee_inventories.each do |e| %>
<%= e.employee.email if e[:status]== 'ALLOTED' %>
<% end %>
<% end %>
The main problem in your code is n+1 query problem. If you are using eager loading then there is no issue otherwise use eager loading using includes keyword in your query.

Clarification on accessing methods

I am having trouble accessing a method i have written in a class, the problem is im trying to access it within an instance of that class, but would appreciate someone explain in a bit more details as to why this doesn't work and things to look at to get a solution.
I have created a simple helper method to use in my view to join two attributes
class TeamMember < ActiveRecord::Base
def fullname
"#{self.forename} #{self.surname}"
end
end
Within my view (show action) I want to be able to use this method
def show
#team_member = TeamMember.find(params[:id])
end
So doing this for example gives me an undefined method
<%= link_to fullname(#team_member) %>
OR
<% #team_member.each do |t| %>
<%= link_to fullname, t %>
<% end %>
Whereas this works
<% #team_member.each do |t| %>
<%= link_to "#{t.forename} #{t.surname}", t %>
<% end %>
Could someone help to clarify this for me so that i can learn from it please
You defined fullname method in your TeamMember class, but you try to call this method with implicit receiver in view, which is ActionView::Base instance. Instead, you should use explicit receiver, which must be TeamMember instance:
<%= link_to #team_member.fullname, #team_member %>
and:
<%= link_to t.fullname, t %>

rails change path of partial collection rendering

I have an STI relationship where a conversation is composed of both messages and images.
Now when I go to render them i use:
<%= render conversation %>
which works perfect. It finds the given template for the given object and renders it.
Now for my mobile site I want to use the same thing only now it should find say:
/mobile/message/_message.html.erb
instead of
/message/_message.html.erb
So in my controller i said:
if mobile?
prepend_view_path "mobile"
end
Which does get called, and it "prepends my view path" which i can see is working when i do:
raise view_paths.inspect
However now when i do my
<%= render conversation %>
It is still looking in the default location i.e. /views/ for the partial
Well, this should work, but it is distressingly inelegant:
<% conversation.each do |c| %>
<% c_class = c.class.to_s.downcase.underscore %>
<%= render :partial => "mobile/#{c_class}/#{c_class}", :object => c %>
<% end %>

How to do a Switch based on controller with ruby on rails?

How could one do switch in ruby on rails something like:
case controller "home"
do home
case controller "about"
do about
else
do home
I currently have this code:
<% case current_page(:controller) %>
<% when "forums" %>
<%= render :partial => 'shared/sidebar/sidebar_forums' %>
<% when "events" %>
<%= render :partial => 'shared/sidebar/sidebar_events' %>
<% else %>
<%= render :partial => 'shared/sidebar/sidebar_frontpage' %>
<% end %>
Quoting from http://rails.nuvvo.com/lesson/6371-action-controller-parameters:
The params hash will always contain the :controller and :action keys, but you should
use the methods controller_name and action_name instead to access these values. Any other parameters defined by the routing, such as :id will also be available.
So you should definitely be able to access it via params[:controller], and, if the controller_name method is in scope in a view, you should use that instead.
As for the switch syntax itself, you do need to do it like
case controller_name
when "home"
do_home
when "about"
do_about
else
do_default
end
You could do some hacking and get
case true
when controller "home"
do_home
when controller "about"
do_about
else
do_default
end
But why?
Whenever you had to do something like this, that means there is something not right with the application design. Not that I have never done that in the past but I dont do this now and it is being frowned upon.
Instead of doing what you are doing now, if you namespace your controllers appropriately according to their responsibilities, after all they are just classes and handle the requests coming from the users,etc., then you may not have to do this switch statement. For example, after namespacing them you may have different layouts made of different partials for your views which may not require you to do this switching in your controller/views hence keeping the code clean.

Resources