Reputation: 371
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.
Upvotes: 0
Views: 497
Reputation: 6223
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 }
Upvotes: 1
Reputation: 3308
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 %>
Upvotes: 0
Reputation: 44370
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 %>
Upvotes: 1