Getting NameError while linking Dragonfly gem to index.html.erb

163 views Asked by At

I have already linked Dragonfly gem to my project and it uploads images very well into creating posts via _form, but when I'm trying to add all these created posts to index.html.erb alltogether with uploaded using Dragonfly-gem images, it shows me an NameError. I have already made this operation in few projects, but in this project I have no idea where the error comes from. Here is the case:

NameError in Posts#index undefined local variable or method `posts' for #<#:0x66f9d20>

Extracted source (around line #5):

5. <%= link_to image_tag(posts.image.thumb('64x64!').url) %>

Posts controller:

class PostsController < ApplicationController
    before_action :find_post, only: [:show, :edit, :update, :destroy]
    before_action :authenticate_user!, except: [:index, :show]

    def index
        @posts = Post.all.order("created_at DESC")
    end

    def show
        @post = Post.find(params[:id])
    end

    def new
        @post = current_user.posts.build
    end

    def create
        @post = current_user.posts.build(post_params)

        if @post.save
            redirect_to @post
        else 
            render 'new'
        end
    end

    def edit

    end

    def update
        if @post.update(post_params)
            redirect_to @post
        else
            render 'edit'
        end
    end

    def destroy
        @post.destroy
            redirect_to root_path
    end

    private

    def find_post
        @post = Post.find(params[:id])
    end

    def post_params
        params.require(:post).permit(:title, :link, :description, :image)
    end

end

index.html.erb:

<% @posts.each do |post| %>
<h2><%= link_to post.title, post %></h2>
<% end %>
<%= link_to image_tag(posts.image.thumb('64x64!').url) %>

Post model:

class Post < ActiveRecord::Base
    belongs_to :user
    dragonfly_accessor :image
end

User model:

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

Show.html.erb:

<h1><%= @post.title %></h1>
<%= image_tag @post.image.thumb('300x300#').url if @post.image_stored? %>
<p><%= @post.link %></p>
<p><%= @post.description %></p>
<p><%= @post.user.name %></p>
2

There are 2 answers

2
Florent Ferry On

In your index.html.erb:

<% @posts.each do |post| %>
  # inside 
  <h2><%= link_to post.title, post %></h2>
<% end %>
<%= link_to image_tag(posts.image.thumb('64x64!').url) %> # outside

So you can't have access to post outside the loop who itinerates through @posts.

In order to use post, you need to place your image_tag inside the loop.

And like you use post to render each post, you need to replace posts by post.

<% @posts.each do |post| %>
  <h2><%= link_to post.title, post %></h2>
  <%= link_to image_tag(post.image.thumb('64x64!').url) if post.image_stored? %>
<% end %>
1
Pavan On

Try changing your index.html.erb like this

<% @posts.each do |post| %>
<h2><%= link_to post.title, post %></h2>
<%= link_to image_tag(post.image.thumb('64x64!').url) %>
<% end %>