Rails Carrierwave upload not uploading - ruby-on-rails

So I'm trying to get a basic carrierwave uploader working for a photography website.
I went and generated a scaffold and got the uploader all setup. the page loads showing the upload box, and it looks like I upload a photo but when I go to the show page it spits out something like Image: #<ActionDispatch::Http::UploadedFile:0x007fa03f7389a8> and shows no image.
I'm still new to rails so there is probably something really simple that im not seeing here.
Thanks!
Here is my uploader called image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_limit => [200, 200]
end
end
here is my show.html.rb
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #image.title %>
</p>
<p>
<strong>Description:</strong>
<%= #image.description %>
</p>
<p>
<strong>Tag:</strong>
<%= #image.tag %>
</p>
<p>
<strong>Image:</strong>
<%= #image.image %>
</p>
<%= link_to 'Edit', edit_image_path(#image) %> |
<%= link_to 'Back', images_path %>
and this is my images controller
class ImagesController < ApplicationController
before_action :set_image, only: [:show, :edit, :update, :destroy]
# GET /images
# GET /images.json
def index
#images = Image.all
end
# GET /images/1
# GET /images/1.json
def show
end
# GET /images/new
def new
#image = Image.new
end
# GET /images/1/edit
def edit
end
# POST /images
# POST /images.json
def create
#image = Image.new(image_params)
respond_to do |format|
if #image.save
format.html { redirect_to #image, notice: 'Image was successfully created.' }
format.json { render :show, status: :created, location: #image }
else
format.html { render :new }
format.json { render json: #image.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /images/1
# PATCH/PUT /images/1.json
def update
respond_to do |format|
if #image.update(image_params)
format.html { redirect_to #image, notice: 'Image was successfully updated.' }
format.json { render :show, status: :ok, location: #image }
else
format.html { render :edit }
format.json { render json: #image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.json
def destroy
#image.destroy
respond_to do |format|
format.html { redirect_to images_url, notice: 'Image was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_image
#image = Image.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def image_params
params.require(:image).permit(:title, :description, :tag, :image)
end
end

try changing your show.html.erb:
from:
<p>
<strong>Image:</strong>
<%= #image.image %>
</p>
to:
<p>
<strong>Image:</strong>
<%= image_tag #image.image %>
</p>

Related

Item in Active Record not rendering correctly - Rails 4.2

Not quite sure if 'Active Record' is the right term. The DB? Postgres?
I'm following through Rails Tutorial and having a very frustrating issue. I've found quite a few posts on SO with people struggling, but majority of them went way off base for the answers, so I'm trying to find out what's wrong with my example.
My User Controller
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
#users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
#user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :email)
end
end
My User Model
class User < ApplicationRecord
has_many :micropost
validates :name, presence: true
validates :email, presence: true
end
My Microposts Model
class Micropost < ApplicationRecord
belongs_to :user
validates :content, length: { maximum: 140 },
presence: true
end
My Microposts Controller
class MicropostsController < ApplicationController
before_action :set_micropost, only: [:show, :edit, :update, :destroy]
# GET /microposts
# GET /microposts.json
def index
#microposts = Micropost.all
end
# GET /microposts/1
# GET /microposts/1.json
def show
end
# GET /microposts/new
def new
#micropost = Micropost.new
end
# GET /microposts/1/edit
def edit
end
# POST /microposts
# POST /microposts.json
def create
#micropost = Micropost.new(micropost_params)
respond_to do |format|
if #micropost.save
format.html { redirect_to #micropost, notice: 'Micropost was successfully created.' }
format.json { render :show, status: :created, location: #micropost }
else
format.html { render :new }
format.json { render json: #micropost.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /microposts/1
# PATCH/PUT /microposts/1.json
def update
respond_to do |format|
if #micropost.update(micropost_params)
format.html { redirect_to #micropost, notice: 'Micropost was successfully updated.' }
format.json { render :show, status: :ok, location: #micropost }
else
format.html { render :edit }
format.json { render json: #micropost.errors, status: :unprocessable_entity }
end
end
end
# DELETE /microposts/1
# DELETE /microposts/1.json
def destroy
#micropost.destroy
respond_to do |format|
format.html { redirect_to microposts_url, notice: 'Micropost was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_micropost
#micropost = Micropost.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def micropost_params
params.require(:micropost).permit(:content, :user_id)
end
end
My show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #user.name %>
</p>
<p>
<strong>Email:</strong>
<%= #user.email %>
<% if #user.micropost.any? %>
<%= #user.micropost.first %>
<% end %>
</p>
<%= link_to 'Edit', edit_user_path(#user) %> |
<%= link_to 'Back', users_path %>
When I load a Users page (6 or 7 in my case) I am seeing 'something' being outputted in this format, but it's showing
Which I feel like is an Active Record (?) index? I'm not sure how to get it to show the first (or any) Micropost of a User.
In some solutions I saw people used render #user.micropost but I get an issue about partials (Which I'm familiar with) but the tutorial says you should be able to use the syntax used previously (aka #user.email ) to solve it. So I feel I'm over complicating it?
My issue was I needed to use
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #user.name %>
</p>
<p>
<strong>Email:</strong>
<%= #user.email %>
<% if #user.micropost.any? %>
<%= #user.micropost.first.content %>
<% end %>
</p>
<%= link_to 'Edit', edit_user_path(#user)
%> |
<%= link_to 'Back', users_path %>
I should have realized when it was reporting a hash value.
you can't be rendering the show page because if you did you would get a no method or for nil class.
Your show action has no instance variable called #user.
For your show to display data you need a user object. In your case you have none.
So in your show method, add this:
#user.find_by(params[:id])
That will find the user 6 or 7 and allow you to call #
Can you paste the url from the browser so I can see where you actually are?

image as a link to categories in rails 4 App

In my views/pages/index.html.erb I have this loop, it shows the latest uploaded picture in each category.
<div class="container-fluid">
<% #products.each_slice(3) do |products_group| %>
<div class="row">
<% products_group.each do |category, products| %>
<% products.each_with_index do |product, index| %>
<% if index == 0 %>
<div class="col-lg-4 col-sm-6 col-xs-12 center-block " >
<%= image_tag product.image.url(:medium), class: "img-responsive" %>
<div class="caption">
<p><%= product.category.name %></p>
</div>
<% end %>
<% end %>
</div>
<% end %>
</div>
<% end %>
</div>
I've been trying to add this line of code to the ´image_tag´ part
<%=link_to image_tag product.image.url(:medium), category_path (#category.products), class: "img-responsive" %>
so the user can go to each category by clicking the image in the views/pages/index.html.erb
it gives me this error syntax error, unexpected ( arg, expecting keyword_do or '{' or '(' ...e.url(:medium), category_path (#category.products), class: "... ... ^ /Users/dadi/Documents/Vefir/stores/brainstore/app/views/pages/index.html.erb:25: syntax error, unexpected ',', expecting ')' ...gory_path (#category.products), class: "img-responsive" );#o... ... ^
I've been trying to adjust the code to get rid of the error, but I haven't found the right method to do it.
I´m not sure how to get this right, can any one here guide me to the right path?
this is my categories_controller.rb
class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]
# GET /categories
# GET /categories.json
def index
#categories = Category.all
end
# GET /categories/1
# GET /categories/1.json
def show
#products = #category.products
end
# GET /categories/new
def new
#category = Category.new
end
# GET /categories/1/edit
def edit
end
# POST /categories
# POST /categories.json
def create
#category = Category.new(category_params)
respond_to do |format|
if #category.save
format.html { redirect_to #category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: #category }
else
format.html { render :new }
format.json { render json: #category.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /categories/1
# PATCH/PUT /categories/1.json
def update
respond_to do |format|
if #category.update(category_params)
format.html { redirect_to #category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: #category }
else
format.html { render :edit }
format.json { render json: #category.errors, status: :unprocessable_entity }
end
end
end
# DELETE /categories/1
# DELETE /categories/1.json
def destroy
#category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_category
#category = Category.includes(:products).find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def category_params
params.require(:category).permit(:name)
end
end
this is the pages_controller.rb
class PagesController < ApplicationController
def index
#products = Product.all.order(created_at: :desc).group_by(&:category_id)
end
end
this is the products_controller.rb
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
before_filter :initialize_cart
before_action :authenticate_admin!, only: [ :new, :edit, :update, :create, :destroy ]
# GET /products
# GET /products.json
def index
#products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
#product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #product }
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :description, :price, :image, :category_id, :stock_quantity)
end
end
this is my routes.rb
Rails.application.routes.draw do
get 'pages/index'
get 'pages/about'
get 'pages/location'
get 'pages/stockists'
devise_for :users
resources :categories
resources :categories
resources :category_names
resources :products
resource :cart, only: [:show] do
post "add", path: "add/:id", on: :member
get :checkout
end
resources :orders, only: [ :index, :show, :create, :update ] do
member do
get :new_payment
post :pay
end
end
root 'pages#index'
end
Try to wrap image_tag into the brackets:
<%=link_to image_tag(product.image.url(:medium)), category_path (#category.products), class: "img-responsive" %>
Or use a block:
<%= link_to category_path (#category.products), class: "img-responsive" do %>
<%= image_tag product.image.url(:medium) %>
<% end %>
The best way to do this is to use link_to with a block. What follows is your code when written in block format using link_to:
<%= link_to category_path(#category.products), class: "img-responsive" do %>
<%= image_tag product.image.url(:medium) %>
<% end %>
To know more about link_to.

Images are uploading to S3 but I cannot see them in my app

I am still very new to rails is this is the first time I am uploading files to S3. In my app I have used the carrierwave gem and am able to choose a file and save. It seems like the link between carrierwave and S3 are working because the images are showing up in my S3 bucket. The problem is I can not see the image in my app and I cannot figure out why this is.
Here is my controller:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
#products = Product.all
#uploader = Product.new.image
#uploader.success_action_redirect = new_product_path
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
#product = Product.new(key: params[:key])
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #product }
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :description, :image, :price, :category, :subcategory)
end
end
ImageUploder:
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
include CarrierWave::RMagick
include CarrierWave::MimeTypes
process :set_content_type
version :thumb do
process resize_to_fill: [200,200]
end
end
show.html.erb (where the image should be showing up):
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #product.title %>
</p>
<p>
<strong>Description:</strong>
<%= #product.description %>
</p>
<p>
<strong>Image:</strong>
<%= #product.image_url %>
</p>
<p>
<strong>Price:</strong>
<%= #product.price %>
</p>
<p>
<strong>Category:</strong>
<%= #product.category %>
</p>
<p>
<strong>Subcategory:</strong>
<%= #product.subcategory %>
</p>
<%= link_to 'Edit', edit_product_path(#product) %> |
<%= link_to 'Back', products_path %>
product.rb:
class Product < ActiveRecord::Base
attr_accessor :image
mount_uploader :image, ImageUploader
after_save :enqueue_image
def image_name
File.basename(image.path || image.filename) if image
end
def enqueue_image
ImageWorker.perform_async(id,key) if key.presemt?
end
class ImageWorker
include Sidekiq::Worker
def perform(id, key)
product = Product.find(id)
product.key = key
product.remote_image_url = product.image.direct_fog_url(with_path: true)
end
end
end
Thanks in advance for any help!
Try changing
<%= #product.image_url %>
to
<%= image_tag(#product.image_url) %>

Can't Display a value from my rails model

I'm new to Ruby on Rails and I started with a scaffold and added another model manually. I can't seem to get the values from the model I manually generated to display in my index view.
My first model is for Golf Courses names, city, par, and hole_id. The second model is the amount of holes for each course. For some reason I can't get the hole amount to display Below is my code.
Models
class Course < ActiveRecord::Base
has_many :holes
end
class Hole < ActiveRecord::Base
belongs_to :course
end
Controller
class CoursesController < ApplicationController
before_action :set_course, only: [:show, :edit, :update, :destroy]
# GET /courses
# GET /courses.json
def index
#courses = Course.all
#holes = Hole.all
end
# GET /courses/1
# GET /courses/1.json
def show
end
# GET /courses/new
def new
#course = Course.new
end
# GET /courses/1/edit
def edit
end
# POST /courses
# POST /courses.json
def create
#course = Course.new(course_params)
respond_to do |format|
if #course.save
format.html { redirect_to #course, notice: 'Course was successfully created.' }
format.json { render :show, status: :created, location: #course }
else
format.html { render :new }
format.json { render json: #course.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /courses/1
# PATCH/PUT /courses/1.json
def update
respond_to do |format|
if #course.update(course_params)
format.html { redirect_to #course, notice: 'Course was successfully updated.' }
format.json { render :show, status: :ok, location: #course }
else
format.html { render :edit }
format.json { render json: #course.errors, status: :unprocessable_entity }
end
end
end
# DELETE /courses/1
# DELETE /courses/1.json
def destroy
#course.destroy
respond_to do |format|
format.html { redirect_to courses_url, notice: 'Course was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_course
#course = Course.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def course_params
params.require(:course).permit(:name, :city, :hole_id)
end
end
View
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #course.name %>
</p>
<p>
<strong>City:</strong>
<%= #course.city %>
</p>
<p>
<strong>Hole:</strong>
<%= #course.holes %>
</p>
<%= link_to 'Edit', edit_course_path(#course) %> |
<%= link_to 'Back', courses_path %>
<%= #course.holes %> gives you an ActiveRecord_Associations_CollectionProxy
You need to ask for its size, length or its count to get the total amount of holes that belong to #course which means you have to say #course.holes.size, #course.holes.length or #course.holes.count. Check the documentation for the differences between these three.

FIle upload with Carrier wave returning :nil

I'm attempting to add carrierwave to my application to handle attachments from users (pdf, doc, etc - not images). I've viewed several tutorials and while it seems to be working, I am not able to access the attachment through link_to method or by following the url carrierwave creates. I'm running into two odd things when using my application:
1) The link preview (referring to the popup in the bottom left in chrome showing the destination of the link) only shows localhost::model/id, not the url that is in the uploader
2) When I view a record in the rails console, the column shows nil. I have added this into my permitted params in the controller.
My controller:
class Cms484sController < ApplicationController
before_action :set_cms484, only: [:show, :edit, :update, :destroy]
# GET /cms484s
# GET /cms484s.json
def index
#cms484s = Cms484.all
end
# GET /cms484s/1
# GET /cms484s/1.json
def show
end
# GET /cms484s/new
def new
#cms484 = Cms484.new
end
# GET /cms484s/1/edit
def edit
end
# POST /cms484s
# POST /cms484s.json
def create
#cms484 = Cms484.new(cms484_params)
respond_to do |format|
if #cms484.save
format.html { redirect_to #cms484, notice: 'Cms484 was successfully created.' }
format.json { render :show, status: :created, location: #cms484 }
else
format.html { render :new }
format.json { render json: #cms484.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /cms484s/1
# PATCH/PUT /cms484s/1.json
def update
respond_to do |format|
if #cms484.update(cms484_params)
format.html { redirect_to #cms484, notice: 'Cms484 was successfully updated.' }
format.json { render :show, status: :ok, location: #cms484 }
else
format.html { render :edit }
format.json { render json: #cms484.errors, status: :unprocessable_entity }
end
end
end
# DELETE /cms484s/1
# DELETE /cms484s/1.json
def destroy
#cms484.destroy
respond_to do |format|
format.html { redirect_to cms484s_url, notice: 'Cms484 was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cms484
#cms484 = Cms484.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cms484_params
params.require(:cms484).permit(:supplier_name, :supplier_addr, :supplier_city, :supplier_state, :supplier_zip, :frm_date, :document)
end
end
My uploader:
class FileUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
My form: (abbreviated)
<%= f.file_field :file, multiple: true %>
<div class="actions">
<input type="submit" name="commit" value="Complete Cms484" />
</div>
<% end %>
</body>
</html>
My view:
<p>
<strong>Sup name:</strong>
<%= #cms484.supplier_name %>
</p>
<%= link_to 'Document', #cms484.document.url %> |
<%= link_to 'Edit', edit_cms484_path(#cms484) %> |
<%= link_to 'Back', cms484s_path %>
Any ideas?
Carrierwave relies on the naming convention of your uploader class. In your case you named it FileUploader.
Adjust your permitted attributes from :document to :file
def cms484_params
params.require(:cms484).permit(:supplier_name, :supplier_addr, :supplier_city, :supplier_state, :supplier_zip, :frm_date, :file)
end
In your view
<%= link_to 'Document', #cms484.file_url %>

Resources