一个月用户特定路由和引脚控制器

时间:2013-04-20 20:35:03

标签: ruby-on-rails devise admin model-associations

我想改变我的引脚生成的路径,以容纳管理员用户访问其他用户引脚并能够删除它们。

我使用devise创建了一个用户模型并添加了admin属性。这工作正常,但由于我已将每个引脚与控制器中的current_user相关联,因此在以管理员用户身份登录时无法删除其他引脚或查看引脚。

我认为问题在于我的pins_controller.rb

@pin = current_user.pins.find(params[:id])

以下是无效的实际路线

<% if current_user == pin.user or user_signed_in? && current_user.admin? %>
    <p>
      <%= link_to content_tag(:i, "", class:"icon-edit"), edit_pin_path(pin) %> |
      <%= link_to content_tag(:i, "", class:"icon-trash"), pin, method: :delete, data: { confirm: 'Are you sure?' } %>
    </p>
<% end %> 

我也错了,问题可能出在routes.rb文件

Omrails::Application.routes.draw do

get "users/show"

resources :pins

devise_for :users
match 'users/:id' => 'users#show', as: :user

get 'about' => 'pages#about'

root :to => 'pins#index'

我的git存储库在这里:https://github.com/nathan-wallace/imageapp.git

1 个答案:

答案 0 :(得分:1)

你可以试试这个

    <% if user_signed_in? %>
      <% if (current_user == @pin.user) or current_user.admin? %>
      <p>
        <%= link_to content_tag(:i, "", class:"icon-edit"), edit_pin_path(pin) %> |
        <%= link_to content_tag(:i, "", class:"icon-trash"), pin, method: :delete, data: {               confirm: 'Are you sure?' } %>
      </p>
     <% end %>
    <% end %>