设计:current_user == nil?

时间:2013-11-21 21:29:51

标签: ruby-on-rails ruby devise

我的目标是仅向创建商家信息的用户显示“修改”和“删除”按钮。但是,由于某种原因,current_user返回nil。知道为什么会这样吗?

这是我的用户模型:

class User < ActiveRecord::Base
  has_many :listings
  has_many :thoughts
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end

这是我的上市模式:

class Listing < ActiveRecord::Base
  belongs_to :user
  has_many :thoughts
end

<% @listings.each do |listing| %>
      <tr>
        <td><%= listing.title %></td>
        <td><%= listing.school %></td>
        <td><%= listing.price %></td>
        <td><%= listing.description %></td>
        <% if current_user == listing.user %>
        <td><%= link_to 'Show', listing %></td>
        <td><%= link_to 'Edit', edit_listing_path(listing) %></td>
        <td><%= link_to 'Delete', listing, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <% else %>
        <td><%= link_to 'Show', listing %></td>
        <% end %>
      </tr>
    <% end %>

这是清单控制器中的创建操作

def create
  @listing = Listing.new(listing_params)

  respond_to do |format|
    if @listing.save
      format.html { redirect_to @listing, notice: 'Listing was successfully created.' }
      format.json { render action: 'show', status: :created, location: @listing }
    else
      format.html { render action: 'new' }
      format.json { render json: @listing.errors, status: :unprocessable_entity }
    end
  end
end

这是我的create_listings迁移

class CreateListings < ActiveRecord::Migration
  def change
    create_table :listings do |t|
      t.string :title
      t.string :school
      t.integer :price
      t.text :description

      t.timestamps
    end
  end
end

2 个答案:

答案 0 :(得分:6)

确保您在控制器中设置了before_filter :authenticate_user!

class ListingsController < ActionController::base
  before_filter :authenticate_user!

  def index
    @listings = Listing.all
  end
end

对于您的create方法,只要该表具有user_id列,您只需要为该列表设置用户:

def create
  @listing = Listing.new(listing_params)
  @listing.user = current_user

  respond_to do |format|
    if @listing.save
      format.html { redirect_to @listing, notice: 'Listing was successfully created.' }
      format.json { render action: 'show', status: :created, location: @listing }
    else
      format.html { render action: 'new' }
      format.json { render json: @listing.errors, status: :unprocessable_entity }
    end
  end
end

最后,对于属于用户的列表,它需要能够记录该用户ID。确保您的表具有user_id列:

class CreateListings < ActiveRecord::Migration
  def change
    create_table :listings do |t|
      t.string :title
      t.string :school
      t.integer :price
      t.text :description
      t.integer :user_id

      t.timestamps
    end
  end
end

如果是最新版本,您可以通过从控制台调用rake db:migrate:redo来重新运行此迁移。 如果不是最新版本,则需要使用VERSION=xxx专门针对该迁移ID运行向下和向上(请按照此处的步骤操作:4.3 Running Specific Migrations)。这将是空的表。 如果您需要在该表中保留日期,则需要使用命令add_column :listings, :user_id, :integer编写新的迁移。

答案 1 :(得分:0)

我会做signed_in? && current_user.id == listing.user_id

之类的事情
相关问题