I am having this scenario (main view template):
<% if #cars.count(:all) > 0 %>
<% presenter.collection.each_pair do |date, collection| %>
<tr>
<td>
...
</td>
</tr>
<%= render partial: 'car_row_content', collection: collection, as: :car, cached: Proc.new{|car| [cache_prefix, acl_fingerprint, car.record] } %>
<% end %>
<% end %>
And here's how to the partial car_row_content looks like:
<tr>
<td>
<%= car.something1 %>
</td>
<td>
<%= car.something2 %>
</td>
<td>
<%= car.something3 %>
</td>
</tr>
In this partial, the attributes something1 and something2 stay the same all the time, but the attribute something3 changes a lot (couple times a day).
What's happening here - when one of our admin changes the attribute something3, it's usually not "refreshed" immediately the main view template and we are seeing there the "old" value. How do I force Rails cache to immediately refresh when a change has happened?
Use a cache key, when you update anything, cache key will be updated and your data will be updated.
Related
I have a template that displays a list of events
<tbody>
<%= render partial: 'event', collection: events, cached: true %>
</tbody>
The partial event:
<% cache event do %>
<tr>
<td>
Something
</td>
<td>
<%= render 'identifiable_link_with_tag', identifiable: event.identifiable %>
</td>
</tr>
<% end %>
The partial identifiable_link_with_tag:
<% cache identifiable do %>
<span class="badge badge-info"><%= identifiable.type %></span> <%= link_to identifiable.identifier, identifiable %>
<% end %>
Now, the odd thing is what follows. Sometimes I notice in the events view that for some events another partial (identifiable) is rendered instead of identifiable_link_with_tag: _identifiable. This seems very odd, and on a page that lists 25 events, this would only happen for 1 or 2 or 3 (or 0) events.
So in short, it seems that sometimes the wrong identifiable is rendered. I do use Rails fragment caching, so that may be a factor. Am I missing something or have I encountered a Rails bug? This issue is very hard to reproduce in development, thus hard to debug.
I have a form_tag with a radio_button_tag and it populates with data from DB. It must simply be directed to a customised update action(update_multiple) where a boolean column is updated for all those 2 records which have been changed in the form.
For e.g. say when initially the form was populated from DB with record 1's radio button selected and now user changed his selection to record 3, then at Submit of form tag the update of both records must occur but the problem is the code at submit, only collects id of record which is now selected in that group.How do I get id of that record also which was unselected so that I can update_all for both of them at one go?
And if Submit cannot handle this action, then is there a way in controller or form to persist the id of the initial selected record before populating the form? As you see, I've tried with collecting an array of ids[] with radio_button_tag.
TIA for your help.
Here's the form code:
<%= form_tag update_multiple_user_cv_attachments_path, method: :put, action: :update_multiple do %>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th> Select a CV </th>
<th> Resume Name </th>
</tr>
</thead>
<tbody>
<% #cv_attachments.each do |cv_attachment| %>
<%= hidden_field_tag cv_attachment.main, :value => params[:main] %>
<tr>
<td><%= radio_button_tag "cv_attachment_ids[]", cv_attachment.id, cv_attachment.main %> </td>
<td><%= cv_attachment.attachment.file.basename %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= submit_tag "Select Main", :class =>'button' %>
<% end %>
Here's the controller update_multiple code.
def update_multiple
CvAttachment.update_all(["updated_at=?", Time.now], :id => params[:cv_attachment_ids])
end
I can think of 2 ways to achieve your objective.
update the boolean for all the attachments which belong to the user to false and then update the one which has been selected to true
include a hidden field in the form and set it to the id that is already true. Then in the controller action, update the one that is selected to true and the one in the hidden field to false. This is probably a better option and you'll probably want to wrap the d/b updates in a transaction.
<tbody>
<% #cv_attachments.each do |cv_attachment| %>
<% if cv_attachment.main %>
<%= hidden_field_tag "ex_main_cv", cv_attachment.id %>
<% end %>
<tr>
<td><%= radio_button_tag "main_cv", cv_attachment.id, cv_attachment.main %> </td>
<td><%= cv_attachment.attachment.file.basename %></td>
</tr>
<% end %>
</tbody>
controller
def update_main_attachment // probably a better name for this method
if params["ex_main_cv"] != params["main_cv"]
Attachment.transaction do
deselected_attachment = Attachment.find(params["ex_main_cv"]
deselected_attachment.update_attribute(:main, false)
selected_attachment = Attachment.find(params["main_cv"]
selected_attachment.update_attribute(:main, true)
end
end
end
Many thanks #margo. Here' how I resolved it partly your way of using hidden_field. But for now keeping this thread open as I'm making 2 DB updates for toggle of same column.
<tbody>
<% #cv_attachments.each do |cv_attachment| %>
<% if cv_attachment.main %>
<%= hidden_field_tag "ex_main", cv_attachment.id %>
<% end %>
<tr>
<td><%= radio_button_tag "new_main", cv_attachment.id, cv_attachment.main, :id => "#{cv_attachment.id}"%> </td>
<td><%= cv_attachment.attachment.file.basename %></td>
</tr>
<% end %>
</tbody>
and in controller:
def update_main
if request.put?
if params["ex_main"] != params["new_main"]
CvAttachment.find(params[:ex_main]).toggle!(:main)
CvAttachment.find(params[:new_main]).toggle!(:main)
end
end
It's a simple question.
For example, I have 3 data
number name country
1 Jack US
2 Coda UK
3 Fredy TW
How do I display this number in rails dynamically.
here is part of code
<% #stay_times.each do |s| %>
<tr>
<td>
<%#= I don't know what to put here %>
</td>
<td>
<%= s.name %>
</td>
<td>
<%= s.nationality %>
</td>
<% end %>
Also you can use each_with_index method:
<% #stay_times.each_with_index do |s, index| %>
<tr>
<td>
<%= index + 1 %> <!-- index starts with zero -->
</td>
<td>
<%= s.name %>
</td>
<td>
<%= s.nationality %>
</td>
<% end %>
each_with_index(*args) public
Calls block with two arguments, the item and its index, for each item
in enum. Given arguments are passed through to #each().
If no block is given, an enumerator is returned instead.
hash = Hash.new
%w(cat dog wombat).each_with_index {|item, index|
hash[item] = index
}
hash #=> {"cat"=>0, "dog"=>1, "wombat"=>2}
It looks like you just need to use <%= s.number %> for the line where you have a comment. It depends on what the number field in the data table is called in your #stay_times variable. Hope this helps!
TheChamp helped me out to sort out with the array of hashes issue. Thanks. Now I would like to improve on that. It is really messy in the views
Move the code to model, so only calling the instance variable will show the desired result. Like one variable for flight types, the departure times, price structure, etc.
Have the result sorted in the model itself, based on some conditions. Like for price, default to lowest first.
It takes a lot of time to get the API response. What are the various options to cache the response, so results are instantaneous. Also, what values need to be checked to ensure the cache is fresh.
This is my code base. (for ur consideration, a portion of API response - http://jsfiddle.net/PP9N5/)
Model
class Cleartrip
include HTTParty
debug_output $stdout
base_uri "api.staging.cleartrip.com/air/1.0/search"
headers 'X-CT-API-KEY' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
format :xml
def self.get_flight
response = get("?from=DEL&to=BLR&depart-date=2014-08-10&adults=1&children=0&infants=0")
if response.success?
response
else
raise response.message
end
end
Controller
# This does nothing
expires_in 20.minutes, public: true
#flight = Cleartrip.get_flight
Views
<table>
<tbody>
<% #flight["air_search_result"]["onward_solutions"]["solution"].each do |h| %>
<tr>
<td> # This gets the flight info
<% Array.wrap(h["flights"]["flight"]["segments"]["segment"]).each do |segment| %>
<strong><%= segment['airline'] %></strong> <br>
<%= segment['departure_airport'] %> - <%= segment['departure_date_time'] %> ||
<%= segment['arrival_airport'] %> - <%= segment['arrival_date_time'] %> <br>
<% end %>
<hr>
</td>
<td> # this gets the type of fare, Refundable/ Non-refundable
<%= h["pax_pricing_info_list"]["pax_pricing_info"]["pricing_info_list"]["pricing_info"]["fare_type"] %>
</td>
<td> # This gets the fare structure
<% h["pax_pricing_info_list"]["pax_pricing_info"]["pricing_info_list"]["pricing_info"]["pricing_elements"]["pricing_element"].each do |f| %>
<%= f['category']%> - <%= f['amount'] %> <br>
<% end %>
</td>
<td> # The pricing summary
<strong><%=h["pricing_summary"]["total_fare"] %></strong>
</td>
</tr>
<% end %>
</tbody>
</table>
Appreciate general guidelines.
Thanks.
Beneficiaries
id (PK)
name
age
income
Beneficiary_loans
id (PK)
beneficiary_id (FK)
amount
rate
period
What I'm doing is, selecting a list of beneficiary from [Beneficiaries] table and displaying as follows
<%= form_tag({:action => 'update_survey_list_status', :status=>4}) do %>
<table width="100%">
<tr>
<th>Beneficiary Details</th>
<th>Amount</th>
<th>Rate</th>
<th>Period</th><th>
<input type='checkbox' name='checkall' onclick='checkedAll();'>
</th>
</tr>
<% #publishedlist.each do |b| %>
<tr>
<td><%= b.firstname %></td>
<%= fields_for :beneficiaryloan do |bloan| %>
<td> <%= bloan.text_field :amount%></td>
<td> <%= bloan.text_field :rate%></td>
<td> <%= bloan.text_field :period%></td>
<% end %>
<td><%= check_box_tag "benificiary_ids[]",b.id, :name => "benificiary_ids[]"%> </td>
</tr>
<% end %>
</table> <%= submit_tag "Approve", :class=>'form_buttons' %>
<% end %>
In controller,
#beneficiaries=Beneficiary.find(:all, :conditions => ["id IN (?)", params[:benificiary_ids]])
#beneficiaries.each do |b|
#beneficiaryloan = Beneficiaryloan.new(params[:beneficiaryloan])
#beneficiaryloan.beneficiary_id=b.id
#beneficiaryloan.hfi_id=session[:id].to_s
#beneficiaryloan.status_id=params[:status]
#beneficiaryloan.save
end
What I'm not getting is
params[:beneficiaryloan]
Values are coming as NULL
Am I missing something here in this form?
Check the following,
With the help of any browser tool(eg:firebug), make sure that the form has been rendered properly. Sometimes due to some misplaced tags(generally happens with table structure) the form does not renders properly.
Try to remove the table structure and see if the form works.
Make sure your association is fine, i guess it should be Beneficiary has many Beneficiaryloan
If none of the above helps, please post the request parameters over here.