设计:current_user == nil?

Dyl*_*rds 0 ruby ruby-on-rails 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
Run Code Online (Sandbox Code Playgroud)

这是我的上市模型:

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 %>
Run Code Online (Sandbox Code Playgroud)

这是Listing控制器中的create动作

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
Run Code Online (Sandbox Code Playgroud)

这是我的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
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 6

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

class ListingsController < ActionController::base
  before_filter :authenticate_user!

  def index
    @listings = Listing.all
  end
end
Run Code Online (Sandbox Code Playgroud)

至于你的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
Run Code Online (Sandbox Code Playgroud)

最后,对于属于用户的列表,它需要能够记录该用户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
Run Code Online (Sandbox Code Playgroud)

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