Note that there are some explanatory texts on larger screens.

plurals
  1. PODevise: current_user == nil?
    primarykey
    data
    text
    <p>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. Any idea why this is happening?</p> <p>Here is my user model:</p> <pre><code>class User &lt; 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 </code></pre> <p>Here is my listing model:</p> <pre><code>class Listing &lt; ActiveRecord::Base belongs_to :user has_many :thoughts end &lt;% @listings.each do |listing| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= listing.title %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= listing.school %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= listing.price %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= listing.description %&gt;&lt;/td&gt; &lt;% if current_user == listing.user %&gt; &lt;td&gt;&lt;%= link_to 'Show', listing %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Edit', edit_listing_path(listing) %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Delete', listing, method: :delete, data: { confirm: 'Are you sure?' } %&gt;&lt;/td&gt; &lt;% else %&gt; &lt;td&gt;&lt;%= link_to 'Show', listing %&gt;&lt;/td&gt; &lt;% end %&gt; &lt;/tr&gt; &lt;% end %&gt; </code></pre> <p>Here is the create action in the Listing controller</p> <pre><code>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 </code></pre> <p>Here is my create_listings migration</p> <pre><code>class CreateListings &lt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload