Rails 3.2 config.cache_classes = true results blank values in view - ruby-on-rails

I have a template (show.html.erb) which submits adjustment requests. Also in the template I have a table that breaks down Pending Adjustments and Completed Adjustments that show you information about the adjustment you just submited. EX. type, reason, created at.
When I set config.cache_classes = true in my environments/development.rb the Pending Adjustments and Completed Adjustments table the values dont show except for the created at timestamp. The type or reason do not show. If I set config.cache_classes = false everything appears. I've also set config.action_view.cache_template_loading = false and still get the same results empty table values.
I am trying to get it to work with config.cache_classes = true because in production I need the caching to be turned on for other things.
controllers/print_cost_controller.rb
class PrintCostController < ApplicationController
def show
#printcost = PrintCost.find(params[:id])
#order = Core::Order.find(params[:id])
authorize! :show, #order
respond_to do |format|
format.html { render :show }
end
end
end
models/printcost.rb
class PrintCost < ActiveResource::Base
self.site = ENV["PRINT_COST_URL"]
#headers = { 'Authorization' => "Token token=#{ENV['PRINT_COST_TOKEN']}" }
end
show.html.erb
<% if !#printcost.adjustments.empty? %>
<div class="adjustments_pending">
<h4 class="printcost">Pending Adjustments</h4>
<table class="table">
<tr>
<th>Requested On</th>
<th>Reason</th>
<th>Type</th>
<th></th>
</tr>
<% #printcost.adjustments.each do |adjustment| %>
<% if adjustment.completed_at == nil %>
<tr>
<td><%= DateTime.parse(adjustment.created_at).in_time_zone("Eastern Time (US & Canada)").strftime("%^b-%d-%y %I:%M:%S %P") %></td>
<td><%= adjustment.explanation %></td>
<td><%= adjustment.adjustment_type %></td>
<td><%= link_to 'X', remove_adjustment_path(adjustment.id), :method => :delete, :class => "badge", "data-title" =>"Cancel Request" %></td>
</tr>
<% end %>
<% end %>
</table>
</div>
<div class="adjustments_completed">
<h4 class="printcost">Completed Adjustments</h4>
<table class="table">
<tr>
<th>Requested On</th>
<th>Reason</th>
<th>Type</th>
<th>Note</th>
</tr>
<% #printcost.adjustments.each do |adjustment| %>
<% if adjustment.completed_at %>
<tr>
<td><%= DateTime.parse(adjustment.completed_at).in_time_zone("Eastern Time (US & Canada)").strftime("%^b-%d-%y %I:%M:%S %P") %></td>
<td><%= adjustment.adjustment_type == "Tax" ? "$#{adjustment.adjustment_cost} - #{adjustment.explanation}" : adjustment.explanation %></td>
<td><%= adjustment.adjustment_type %></td>
<td><%= adjustment.note %></td>
</tr>
<% end %>
<% end %>
</table>
</div>
<% end %>
Any ideas on why this is happening?

I figured out the problem. It was an issue with another model I had called Adjustments. Had to rename some things.

Related

Rails syntax error </div>

I am trying to set "edit" function for a simple CMS. I can make it to create/delete, but it just won't let me "edit".
here is the error message:
SyntaxError in SectionsController#edit
app/views/sections/edit.html.erb:42: syntax error, unexpected keyword_ensure, expecting keyword_end
Extracted source (around line #42):
40
41
when I checked my edit.html.erb. it seems fine?
'index'}, :class => 'back-link') %>
<div class="sections edit">
<h2>Update Sections</h2>
<%= form_for(:two, :url => {:action => 'update', :id => #one.id}) do |f| %>
<table summary="Section form fields">
<% #one.each do |f| %>
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th>Visible</th>
<td><%= f.text_field(:visible) %></td>
</tr>
<tr>
<th>content_type</th>
<td><%= f.text_field(:content_type) %></td>
</tr>
<tr>
<th>content</th>
<td><%= f.text_field(:content) %></td>
</tr>
<tr>
<th>page_id</th>
<td><%= f.text_field(:page_id) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Update Section") %>
</div>
<% end %>
</div>
Here is the controller:
class SectionsController < ApplicationController
def index
#one = Section.all
end
def show
#one = Section.find(params[:id])
end
def new
#one = Section.new
end
def create
#one = Section.new(section_params)
if #one.save
flash[:notice] = "Section created successfully!"
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#one = Section.find(params[:id])
end
def update
#one = Section.find(params[:id])
if #one.update_attributes(section_params)
flash[:notice] = "Subject updated successfully!"
redirect_to(:action => 'show', :id =>#one.id)
else
render('edit')
end
end
def delete
#one = Section.find(params[:id])
end
def destroy
subject = Section.find(params[:id]).destroy
flash[:notice] = "Subject deleted successfully!"
redirect_to(:action => 'index')
end
private
def section_params
params.require(:two).permit(:id,:name,:position,:visible,:page_id,:content,:content_type)
end
end
Thanks so much!!
Thanks #Pavan, #MarsAtomic and #Mandeep ! I found the issue: <% #one.each do |f| %>. I don't really need this line. If I need it, I need a end code for this.
In my case, I don't, so I go ahead deleted this line, and it worked now!
thanks again everyone!

Rails downloading empty csv file

I have a problem with Downloading csv files from my rails app.
First the controller looks like this;
def report
#report = SentMessage.where('user_id = ? AND created_at <= ? AND created_at >= ?', current_user.id, params[:before], params[:after]).order('created_at Desc')
respond_to do |format|
format.html
format.csv do
response.headers["Content-Type"] = "text/csv; charset=UTF-8; header=present"
response.headers["Content-Disposition"] = "attachment; filename=sent_messages.csv"
end
end
end
then my report.csv.erb file looks like this;
<%= provide(:title, "Sent Messages") %>
<div>
<div class="panel radius"> <h2 class="text_center">Sent Messages</h2></div>
<table class="table table-striped table-bordered table-hoverlarge-12 radius columns large-centered" id="sent_table">
<thead>
<tr>
<th>To</th>
<th>From</th>
<th>Message</th>
<th>Delivery Report</th>
<th>cost</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<% #report.each do |message| %>
<tr>
<td><%= message.to %></td>
<td><%= message.from %></td>
<td><%= message.message %></td>
<td><%= message.delivery %></td>
<td><%= message.cost %></td>
<td><%= message.created_at.to_datetime.strftime("%b %d, %Y %I:%M%P") %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
When i click the download link, the downloaded file is just empty besides the headers as show in the report.csv.erb file.
What could be the problem?
I simply had to change the link to the download from;
to
<%= link_to "Download CSV", params.merge(format: "csv"), class: "button radius" %>
And it worked.
Do like that may be work for you.
def report
#report = SentMessage.where('user_id = ? AND created_at <= ? AND created_at >= ?', current_user.id, params[:before], params[:after]).order('created_at Desc')
respond_to do |format|
format.html
format.csv{
response.headers["Content-Type"] = "text/csv; charset=UTF-8; header=present"
response.headers["Content-Disposition"] = "attachment; filename=sent_messages.csv"}
end
end

How to use an if statement to filter records from a model object ror

I have service and servicebooking models, I want to view the current users made servicebookings by checking the owner_id attribute in the servicebooking model.
My servicebooking controller method:
def myservicebookings
if current_user.id == #servicebooking.owner_id
#servicebookings = current_user.servicebookings.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
else
"You have no service bookings"
end
end
My servicebookings view:
<% if current_user.id == #servicebooking.owner_id %><% #servicebookings.each do |servicebooking| %>
<tr>
<td><%= servicebooking.date %></td>
<td><%= servicebooking.time %></td>
<td><%= servicebooking.service_name %></td>
</tr>
<% end %>
<% else %>
<%= "You have no outgoing service bookings" %>
<% end %>
Currently I get the following error when trying to load the myservicebookings form:
undefined method `owner_id' for nil:NilClass
Any ideas on how to change this code to make it work? thanks in advance guys.
in you case, you dont need if statment because in this variable you have all current user servicebooking:
def myservicebookings
#servicebookings = current_user.servicebookings.
^^^^^^^^^^^^ search(params[:search]).
order(sort_column + " " + sort_direction).
paginate(:per_page => 5, :page => params[:page])
end
use this:
<% if #servicebookings.any? %>
<% #servicebookings.each do |servicebooking| %>
<tr>
<td><%= servicebooking.date %></td>
<td><%= servicebooking.time %></td>
<td><%= servicebooking.service_name %></td>
</tr>
<% end %>
<% else %>
<%= "You have no outgoing service bookings" %>
<% end %>
or this:
<% if #servicebookings.any? %>
<% for servicebooking in #servicebookings %>
<tr>
<td><%= servicebooking.date %></td>
<td><%= servicebooking.time %></td>
<td><%= servicebooking.service_name %></td>
</tr>
<% end if current_user.id == servicebooking.owner_id %>
<% else %>
<%= "You have no outgoing service bookings" %>
<% end %>
You should switch the position of the loop and and if statement in your view, then use the loop variableservicebooking not the instance variable #servicebooking, making it look like this
<% #servicebookings.each do |servicebooking| %>
<% if current_user.id == servicebooking.owner_id %>
the rest of the view ...
UPDATE: If you want to update on the controller level, then it's almost the same:
#servicebookings = current_user.servicebookings.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
#servicebookings.select! { |servicebooking| servicebooking.owner_id == current_user.id }
I would not mess with my views or my controller. I would go OOPS here. In RoR, its skinny controllers, fat models.
Goto your model,
class ServiceBooking < ActiveRecord::Base
# u = User, srch = params[:search],p = params[:page],p_p = per_page,s_c =sort_columns, s_d = sort_direction
def self.mine(u,srch,s_c,s_d,p,p_p = 5)
u.servicebookings.search(srch).order(s_c + " " + s_d).paginate(per_page: p_p, page: => p)
end
end
In your controller,
def myservicebookings
# 6th argument to this method is overridable.
#mine = ServiceBooking.mine(current_user,params[:search],sort_column,sort_direction,params[:page])
end
In your View,
<% #mine.each do |m| %>
<tr>
<td><%= m.date %></td>
<td><%= m.time %></td>
<td><%= m.service_name %></td>
</tr>
<% end %>

Displaying events for a specific user in a view ruby on rails

I am using Rails 4 and devise. I am trying to use an if statement in my 'myevents' view to only display the events that belong to the currently logged in user, can anybody help, currently it returns all the events and ignores the if statement. I also tried putting the if statement in the controller but no success there, here is my code:
///////////Myevents view
<h1>My Events</h1>
<table>
<tr>
<th></th>
<th><%= sortable "name" %></th>
<th><%= sortable "date" %></th>
<th><%= sortable "time" %></th>
<th><%= sortable "description" %></th>
<th><%= sortable "dresscode" %></th>
<th><%= sortable "price" %></th>
</tr>
<% #events.each do |event| %>
<% if event.user_id == current_user %>
<tr>
<td><%= image_tag event.avatar.url %></td>
<td><%= event.name %></td>
<td><%= event.date %></td>
<td><%= event.time %></td>
<td><%= event.description %></td>
<td><%= event.dresscode %></td>
<td><%= event.price %></td>
<td><%= link_to "Show", event_path(event) %></td>
<td><%= link_to "Edit", edit_event_path(event) %></td>
<td><%= link_to 'Destroy', event_path(event),
method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% else %>
<%= puts "No events to display" %>
<% end %>
<% end %>
</table>
///////routes.rb declaration
get 'myevents', to: 'events#myevents'
////controller action
def myevents
#events = Event.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end
The way you're fetching Events is wrong. Your models should be setup as so:
class User
has_many :events
end
class Event
belongs_to :user
end
controller
def myevents
#events = current_user.events.search(params[:search).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end
view
<% if #events.any? %>
# #events.each do |event|
<% else %>
# no events
<% end %>

rails 3.2 ajax live search

I'm still a Rails-Learner and getting desperate about implementing an ajax live search. The search seems to work on submitting, but not on keyup. Can't figure out why...
index.html.erb
<%= form_tag contacts_path, :method => 'get', :id => "contacts_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<div id="cresults_div"><%= render 'cresults' %></div>
<% end %>
<%= link_to 'New Contact', new_contact_path %>
contacts_controller.rb
def index
#contacts = Contact.search(params[:search])
respond_to do |format|
format.html # index.html.erb
format.json { render json: #contacts }
end
end
index.js.erb
$("#cresults_div").html("<%= escape_javascript(render("cresults")) %>");
contact.rb
def self.search(search)
if search
where('last_name LIKE ?', "%#{search}%")
else
scoped
end
end
contacts.js.coffee
jQuery ->
# Ajax search on submit
$('#contacts_search').submit( ->
$.get(this.action, $(this).serialize(), null, 'script')
false
)
# Ajax search on keyup
$('#contacts_search input').keyup( ->
$.get($("#contacts_search").attr("action"), $("#contacts_search").serialize(), null, 'script')
false
)
_cresults.html.erb
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<h1>Listing contacts</h1>
<table>
<tr>
<th>Salutation</th>
<th>First name</th>
<th>Last name</th>
<th>Stree</th>
<th>Street no</th>
<th>Zip</th>
<th>City</th>
<th>State</th>
<th>Country</th>
<th>Phone</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #contacts.each do |contact| %>
<tr>
<td><%= contact.salutation %></td>
<td><%= contact.first_name %></td>
<td><%= contact.last_name %></td>
<td><%= contact.stree %></td>
<td><%= contact.street_no %></td>
<td><%= contact.zip %></td>
<td><%= contact.city %></td>
<td><%= contact.state %></td>
<td><%= contact.country %></td>
<td><%= contact.phone %></td>
<td><%= contact.email %></td>
<td><%= link_to 'Show', contact %></td>
<td><%= link_to 'Edit', edit_contact_path(contact) %></td>
<td><%= link_to 'Destroy', contact, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
also tried to additional add
application.js
$(function() {
$("#contacts_search input").keyup(function() {
$.get($("#contacts_search").attr("action"), $("#contacts_search").serialize(), null, "script");
return false;
});
});
But the live search won't start on typing... why?
In this particular case I had to remove the
respond_to do |format|
format.html # index.html.erb
format.json { render json: #contacts }
end
Block from the index-Method in the contacts controller

Resources