未定义的方法`user?'在rails4中

时间:2014-08-28 10:22:15

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

我正在使用设计宝石。我的项目目的用户可以上传文件和删除自己的文件但不删除其他用户的文件。那么,我写“用户?”确保正确用户只能显示删除按钮的方法。我还确保删除文件的'correct_user'方法。但现在我正面临着这个问题“NoMethodError at / upload_files ..” “未定义的方法`用户?' “

这是我的 upload_files_controller.rb 文件:

class UploadFilesController < ApplicationController

  before_action :logged_in
  before_action :correct_user,   only: :destroy

  def index
    @upload_files = UploadFile.all
  end

  def new
    @upload_file = UploadFile.new
  end


  def create
    @upload_file = current_user.upload_files.build(upload_params)


    if @upload_file.save
      redirect_to upload_files_path, notice: "The file #{@upload_file.name} has been uploaded."
    else
      render "new"
    end
  end

  def destroy
    upload_file = UploadFile.find(params[:id]).destroy
    redirect_to upload_files_path, notice:  "The file #{upload_file.name} has been deleted."
  end



  def user?(check_user)
    check_user == current_user.id
  end




  private
  def upload_params
    params.require(:upload_file).permit(:name, :upload_file)
  end


  def logged_in
    if admin_signed_in?
      return true
    else
      authenticate_user!
    end
  end


  def correct_user
    @upload_file = current_user.upload_files.find_by(id: params[:id])
    redirect_to root_url if @upload_file.nil?
  end

end

这是我的 upload_files / index.html.erb 文件:

  <% @upload_files.each do |file| %>
      <tr>
        <td><%= file.name %></td>
        <td><%= link_to "Download File", file.file_name_url %></td>


        <%if admin_signed_in? %>

            <td><%= button_to "Delete",  file, method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{file.name}?" %></td>

        <% else user?(file.user_id) %>

            <td><%= button_to "Delete",  file, method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{file.name}?" %></td>

        <% end %>


      </tr>
  <% end %>

我错了什么?请告诉我一个方法。

谢谢, Mezbah

3 个答案:

答案 0 :(得分:3)

您应该将user?方法放在帮助器中。

关于您的观看代码,为什么不使用布尔替代?

<% if admin_signed_in? || user?(file.user_id) %>
  <td><%= button_to "Delete",  file, method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{file.name}?" %></td>
<% end %>

答案 1 :(得分:2)

Marek是正确的,但是,您可能希望使用authorization这样的CanCanCan宝石

有一个很棒的Railscast about authorization here

enter image description here

为了给您一个简要的概要,授权是用户对CRUD对象的权限。身份验证(设计)用于授予用户“权限”#34;在应用程序中使用各种功能;授权允许用户根据访问级别编辑/更改数据

您选择尝试添加按钮,以便用户可以删除自己的对象。这是完美的CanCanCan领域:

-

<强> CanCanCan

这个宝石原本叫做#34; CanCan&#34;,但是当Ryan Bates休假时,一些Rails社区自己制作了自己的宝石,称之为CanCanCan < / p>

它的工作方式相对简单:

  
      
  1. 拥有&#34;能力&#34;用于定义用户能力的模型
  2.   
  3. 调用can?方法以确定用户是否可以参与特定操作
  4.   

这意味着您将能够创建可扩展的功能,以便根据需要授予您的用户访问特定对象的权限。以下是:

> rails g cancan:ability

这将创建ability模型来定义所有方法:

#app/models/ability.rb
class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)
    if user.admin?
      can :manage, :all
    else
      can :read, :all
    end
  end
end

这使您能够在各种对象上调用can?方法:

<td>
  <% if admin_signed_in? || (can? :destroy, file) %>
    <%= button_to "Delete",  file, method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{file.name}?" %>
  <% end %>
</td>

答案 2 :(得分:1)

我会将以下方法添加到您的application_controller.rb。这使得该方法可用于应用程序中的所有控制器和视图。

# application_controller.rb
def current_user?(user)
  current_user == user
end
helper_method :current_user?

在您的视图中使用这样的方法:

# in view
<% if admin_signed_in? || current_user?(file.user) %>
  <td>
    <%= button_to('Delete', file, 
                   method:  :delete, 
                   class:   'btn btn-danger', 
                   confirm: "Are you sure that you wish to delete #{file.name}?") %>
  </td>
<% end %>