簡體   English   中英

設計:current_user ==零?

[英]Devise: current_user == nil?

我的目標是僅向創建列表的用戶顯示“編輯”和“刪除”按鈕。 但是,由於某種原因,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 %>

這是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

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

確保您具有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運行特定遷移 )。 這將清空表格。 如果您需要在該表中保留日期,則只需使用命令add_column :listings, :user_id, :integer編寫新的遷移。

我會做諸如signed_in? && current_user.id == listing.user_id類的signed_in? && current_user.id == listing.user_id signed_in? && current_user.id == listing.user_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM