[英]Devise: current_user == nil?
My goal is to display the 'edit' and 'delete' buttons only to the user who created the listing. 我的目标是仅向创建列表的用户显示“编辑”和“删除”按钮。 However, for some reason, current_user is returning nil. 但是,由于某种原因,current_user返回nil。 Any idea why this is happening? 知道为什么会这样吗?
Here is my user model: 这是我的用户模型:
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
Here is my listing model: 这是我的列表模型:
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 %>
Here is the create action in the Listing controller 这是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
Here is my create_listings migration 这是我的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
Make sure you have before_filter :authenticate_user!
确保您具有before_filter :authenticate_user!
set in your controller: 在您的控制器中设置:
class ListingsController < ActionController::base
before_filter :authenticate_user!
def index
@listings = Listing.all
end
end
As for your create method, so long as the table has a user_id column you just need to set the user for that listing: 至于您的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
Lastly, for the listing to belong to a user it needs to be able to record that users id. 最后,要使列表属于用户,它必须能够记录该用户的ID。 Make sure your table has a user_id column: 确保您的表具有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
You can re-run this migrate if it is your latest by calling rake db:migrate:redo
from the console. 如果是最新迁移,则可以通过从控制台调用rake db:migrate:redo
重新运行此迁移。 If it isn't the latest you need to run the down and up specifically with VERSION=xxx
to for that migrates ID (follow the steps here: 4.3 Running Specific Migrations ). 如果不是最新版本,则需要使用VERSION=xxx
来向上和向下运行该迁移ID(请按照此处的步骤操作: 4.3运行特定迁移 )。 THIS WILL EMPTY THE TABLE. 这将清空表格。 If you need to keep date in that table then you need to write a new migrate with just the command add_column :listings, :user_id, :integer
. 如果您需要在该表中保留日期,则只需使用命令add_column :listings, :user_id, :integer
编写新的迁移。
I'd do something like signed_in? && current_user.id == listing.user_id
我会做诸如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.