Reputation: 150
I am using Paperclip, and my only problem is that in my image tag, I have an undefined method "image".
<%= image_tag @posts.image.url(:medium) %>
is where my error is occurring. When I loaded a new migration, I accidentally did "rails g paperclip user image" instead of "rails g paperclip posts image" so I went into my code and changed "user" to "posts." I asked something similar and someone mentioned that it was with my schema.rb file(?).
Just to be safe, this is my post controller:
class PostsController < ApplicationController
def index
@posts = Post.all.order('created_at DESC')
end
def new
@post = Post.new
if @post.save
redirect_to @post
else
render 'new'
end
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :body))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:title, :body, :image)
end
end
but here is my migration file:
class AddAttachmentImageToUsers < ActiveRecord::Migration
def self.up
change_table :posts do |t|
t.attachment :image
end
end
def self.down
remove_attachment :posts, :image
end
end
and my user.rb file:
has_attached_file :image, :styles => { large: '600x600>', medium: '300x300>', thumb: '150x150#' }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
Upvotes: 2
Views: 3020
Reputation: 1
I think the problem might be how you changing "user" to "posts.".it would be okay to just change controller or view name,but change model is a different thing that you have to generate a migration to change table name
Upvotes: 0
Reputation: 76784
From the comments and your code, it looks like your @posts
variable is either empty or undefined. To add to the original answer, here's what I'd do:
#app/controllers/posts_controller.rb
class PostsController < ActionController::Base
def index
@posts = Post.all.order(created_at: :desc)
end
end
#app/views/posts/index.html.erb
<% if @posts.any? %>
<% @posts.each do |post| %>
<%= image_tag post.image.url(:medium) %>
<% end %>
<% end %>
You must have the @posts
variable referenced in the view
which corresponds to the action
you're running. From seeing your comments, it appears this might not be the case.
You need to be referencing single instances of the Post
model, in order to have the image
method available.
If it is not available, it either means you don't have the @posts
variable declared properly, or your Paperclip
gem is not set up properly.
this is my user.rb file
haha okay. THIS is the problem.
Make sure you put this in your post.rb
file:
#app/models/post.rb
class Post < ActiveRecord::Base
has_attached_file :image, :styles => { large: '600x600>', medium: '300x300>', thumb: '150x150#' }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
Paperclip basically links stored images to database records. Thus, in order to append the image
method to your model, you have to have the Paperclip
options appended to it.
Putting the paperclip calls in user.rb
will add the image
method to your User
model. You need to put those lines in the Post
model.
BTW I met the guys who made Paperclip:
that it was with my schema.rb file(?).
The schema.rb
file is located in /config/db/schema.rb
.
Essentially, each time you migrate to your DB, the schema
is updated, so that in the event you need to repopulate the db, Rails will be able to pull a pre-built set of SQL calls to do it.
Indeed, it is recommended that you use
rake db:schema:load
to rebuild a database (rather thanrake db:migrate
). HOWEVER (VERY IMPORTANT), IT DELETES ANY DATA in that database, making it unadvisable to use it for anything other than building new db's.
You can read about Schema.rb here.
In regards to what your friend was saying, if you go into your schema.rb
file, you'll be able to see which table has the paperclip columns added to it. By generating the migration for the users
table, you may have caused a conflict.
Upvotes: 2
Reputation: 3268
You need to access image for one post at a time so you need to traverse the posts collection and fetch image for each post like this
<% @posts.each do |post| %>
<%= image_tag post.image.url(:medium) %>
<% end %>
Upvotes: 2