This is my Post controller create action:
def create
@user = current_user
@post = @user.posts.create(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
private:
def post_params
params.require(:post).permit( :title, :description, :size, images_attributes [:id,:image,:imageable_id,:imageable_type])
end
end
This is show.html.erb:
(<%= image_tag @post.images.url %>)
This is the Post model:
class Post < ActiveRecord::Base
belongs_to :user
has_many :images, as: :imageable
accepts_nested_attributes_for :images
end
This is the Image model:
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
mount_uploader :image, ImageUploader
end