Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 检查用户是否拥有服务_Ruby On Rails_Ruby_Rubygems_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 检查用户是否拥有服务

Ruby on rails 检查用户是否拥有服务,ruby-on-rails,ruby,rubygems,ruby-on-rails-4,Ruby On Rails,Ruby,Rubygems,Ruby On Rails 4,我有服务和服务预订模式。我希望能够检查当前用户是否拥有服务,并显示为该用户服务所做的服务预订 我正试图在myservicebookings页面中显示此信息 这是servicebookings\u控制器中的myservicebookings操作,在注释行中,Iam试图检查当前用户是否是所预订服务的所有者或所预订服务的所有者,我需要建议如何执行此操作 def myservicebookings @servicebookings = current_user.services.servicebo

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

我正试图在myservicebookings页面中显示此信息

这是servicebookings\u控制器中的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
classservicebooking
结束

服务模式:

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
类服务“/images/:style/missing.png”
已附加文件:avatar2,:default\u url=>“/images/:style/missing.png”
已附加文件:avatar3,:default\u url=>“/images/:style/missing.png”
已附加文件:avatar4,:default\u url=>“/images/:style/missing.png”
属于:用户
属于:事件
有很多:注释,依赖::销毁
有很多:服务预订
def self.search(搜索)
如果搜索
其中('name LIKE?',“%#{search}%”)
其他的
范围
结束
结束
结束

谢谢各位,非常感谢你们的帮助。

你们可以在控制器中执行类似操作。假设
服务预订
服务

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 %>


我很确定这不是你的最佳选择,但我对你的东西了解不够,不能提出其他建议。但这可能是一个开始。您可能想考虑将这些部分放在模型中或替代辅助方法。

您的模型是什么样子的?您在
用户
服务
之间有关系吗?e、 g用户有多个:服务?您的控制器覆盖了
@servicebookings
实例变量是的,我有用户在两个方向上有多个:服务关系Rakesh,谢谢,Froderik,我没有所有者方法??我写答案时没有看到您的模型。您似乎有一个
user
方法可以使用。用户总是所有者吗?嘿,Froderik,是的,如果没有用户创建服务,服务就不存在,他们可以有很多服务,我想列出登录用户服务的预订请求(如果有的话)?那么你怎么知道用户拥有它?它似乎不在模型中。或者它不是持久状态?服务和服务预订表中有一个用户id列