检查用户是否拥有服务

时间:2013-12-15 08:33:32

标签: ruby-on-rails ruby rubygems ruby-on-rails-4

我有服务和服务预订模型。我希望能够检查当前用户是否拥有服务并显示为该用户服务所做的服务预订。

我想在myservicebookings页面中显示它。

这是servicebookings_controller中的myservicebookings操作,在注释行Iam试图检查当前用户是否是所做的任何服务预订的所有者或预订的任何服务的所有者,我需要建议如何走吧?

def myservicebookings
   @servicebookings = current_user.services.servicebooking.find(params[:user_id])
   @servicebookings = current_user.servicebookings.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
  end

这是myservicebookings视图,Iam尝试显示当前用户的服务传入/传出请求:

<h1>My Service bookings</h1>

<table>
  <tr>
    <th><%= sortable "date" %></th>
    <th><%= sortable "time" %></th>
    <th><%= sortable "service name" %></th>
  </tr>
<% if @servicebookings.any? %>

  <h4>Incoming requests:</h4>

  <% @servicebookings.each do |servicebooking| %>
    <tr>
      <td><%= servicebooking.date %></td>
      <td><%= servicebooking.time %></td>
      <td><%= servicebooking.service_name %></td>
      <td><%= link_to "View this booking", servicebooking_path(servicebooking) %></td>
    </tr>
  <% end %>

  <h4>Outgoing requests:</h4>

  <% @servicebookings.each do |servicebooking| %>
    <tr>
      <td><%= servicebooking.date %></td>
      <td><%= servicebooking.time %></td>
      <td><%= servicebooking.service_name %></td>
      <td><%= link_to "View this booking", servicebooking_path(servicebooking) %></td>
    </tr>
  <% end %>
</table>
<%else%>
<%= "You have no incoming or outgoing service booking requests"%>
<%end%>
<%= will_paginate @servicebookings %>
<%= link_to "Homepage", :controller => "welcome", :action => "index" %>

服务预订模式:

class Servicebooking < ActiveRecord::Base
  attr_accessible :service_id, :date, :time, :user_id, :service_name, :accept_booking
  belongs_to :user
  belongs_to :service

  def self.search(search)
  if search
    where('name LIKE ?', "%#{search}%")
  else
    scoped
  end
end

服务模式:

class Service < ActiveRecord::Base
  attr_accessible :avatar, :avatar2, :avatar3, :avatar4, :name, :date_available, :time_available, :description, :price, :size, :company_name, :company_details
  has_attached_file :avatar, :default_url => "/images/:style/missing.png"
  has_attached_file :avatar2, :default_url => "/images/:style/missing.png"
  has_attached_file :avatar3, :default_url => "/images/:style/missing.png"
  has_attached_file :avatar4, :default_url => "/images/:style/missing.png"
  belongs_to :user
  belongs_to :event
  has_many :comments, dependent: :destroy
  has_many :servicebookings


  def self.search(search)
  if search
    where('name LIKE ?', "%#{search}%")
  else
    scoped
  end
end
end

谢谢你们,感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以在控制器中执行类似的操作。假设ownerServiceBooking

上有Service方法
def myservicebookings
    @servicebookings = current_user.servicebookings.includes(:user).search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])

    owns_servicebooking = current_user.servicebookings.detect do |sb| 
        sb.user == current_user 
    end
    owns_service = current_user.services.detect do |s| 
        s.user == current_user 
    end
    @is_an_owner = owns_servicebooking and owns_service
end

detect返回一个对象,如果它与块匹配,并且因为它是ruby,你可以在布尔表达式中使用它来获得你想要的东西。

更新:在上面的代码示例中添加了控制器方法。要在您的视图中使用它,例如:

<% if @is_an_owner %>
    <markup/>
<% end %>

我很确定这不是你个案中最好的选择,但我不太了解你的建议。这可能是一个开始。您可能需要考虑将部分内容放入模型或改为辅助方法。