2014-08-28 48 views
1

我正在使用devise gem。我的項目的目的用戶可以上傳文件&刪除他們自己的文件,但不是其他用戶的文件。那麼,我寫「用戶?」確保正確的用戶只能顯示刪除按鈕的方法。我還確保刪除文件的'correct_user'方法。但現在我正面臨這個問題「/Mode_Files上的NoMethodError ..」 「未定義的方法`user?' 「未定義的方法`用戶?' in rails4

這裏是我的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

你應該把你的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 %> 
+0

謝謝。它的工作。但我仍然有一個問題。 「所有用戶都看到刪除按鈕」。我想隱藏誰是不正確的用戶。 – Mezbah 2014-08-28 10:31:57

+1

@Mezbah它現在應該工作。順便說一句,你的方法很奇怪。 – 2014-08-28 10:38:38

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

非常感謝。 – Mezbah 2014-08-28 11:13:49

2

Marek是正確的,但是,您可能希望使用authorization寶石如CanCanCan這個

有一個偉大的Railscast about authorization here

enter image description here

給你一個簡要的摘要,授權是用戶必須刪除對象的權限。身份驗證(設計)用於授予用戶「權限」以使用應用程序中的各種功能;授權允許用戶根據訪問級別編輯/更改數據

您選擇嘗試添加按鈕,以便用戶可以刪除自己的對象。這是完美CanCanCan領土:

-

CanCanCan

這種寶石最初被稱爲「CanCan」,但瑞恩·貝茨已經休假,一些Rails社區的上臺後,自己讓自己的寶石,稱這是CanCanCan

它的工作方式比較簡單:

  1. 有一個「能力」的模式來定義用戶的能力
  2. 調用can?方法來確定用戶可以在一個特定的動作參加

這意味着,你將能夠使一個可擴展的功能塊,可根據需要授予用戶對特定對象的訪問權限。具體方法如下:

> 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> 
+1

如果你想讓我爲你定義能力,我會重構代碼。我不是故意替換Marek的答案,只是加上 – 2014-08-28 11:04:06

+0

感謝您的回覆。 :) – Mezbah 2014-08-28 11:12:40

+0

在我的項目中,我有兩個單獨的模型用戶和另一個管理員。對於這個以前我有一些問題。你能告訴我一個方法嗎?我如何定義兩種模型的功能? – Mezbah 2014-08-28 12:00:09

相關問題