I am adding carrierwave_mongoid to store images, i have saved images to mongo
And my data like this:
{ "contentType" : "image/jpeg", "length" : 512659, "chunkSize" : 4194304, "uploadDate" : ISODate("2013-01-26T00:00:00Z"), "md5" : "3d44aa8fcfba7cae34fe9a592407410b", "filename" : "uploads/deal/image/295/15a11db8e441c610330af53a77e2b136.jpg", "_id" : 5 }
I have a story model:
class Deal
include Mongoid::Document
include Mongoid::Timestamps
field :image, :type => String
field :status, :type => Integer
mount_uploader :image, ImageUploader
end
My ImageUploader code is here:
class ImageUploader < CarrierWave::Uploader::Base
...
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
now i want to display image from mongodb,and in the view i have
<%= image_tag(#deal.image.url) %>
Finally Images are not display in the view and get the following error:
ActionController::RoutingError (No route matches [GET] "/assets/uploads/deal/image/295/15a11db8e441c610330af53a77e2b136.jpg"):
i want to know how can i read the image through the carrierwave_mongoid
ActionController::RoutingError indicates that your rails application dosen't handle such requests.
Define routes to a controller to query the gridfs and send the image. Additionally, you may need to configure Nginx not to handle such requests as static files.
Related
I have an Event Model which as many images fields
class Event < ApplicationRecord
has_one_attached :poster
has_one_attached :ticket_image
has_many_attached :images
end
How can I create and permit images on active admin events dashboard with the above fields?
I am using active_storage as my image uploader
Try something like this:
ActiveAdmin.register Event do
...
form do |f|
input :poster, as: :file
input :ticket_image, as: :file
input :images, as: :file, input_html: { multiple: true }
end
permit_params :poster, :ticket_image, images: []
end
In my Rails web application, I have to upload audio and video files and for validating against invalid file types, I have used jquery-validation engine and I could do the same successfully. But, if I create a text file and change the extension from .txt to .mp3, e.g. test.txt to test.mp3, it will be taken as valid by jquery validation engine as the file extension is valid for an audio.
I want to check the content type also. When I opened the test.mp3 in a player, it showed me an error message Stream contains no data. I want this kind of validation to be performed in the interface. Is it possible in Rails?
I'm using,
Rails 3.2.13
Ruby 2.0.0-dev
Hope anyone can help me out. Thanks :)-
I recommend you to check paperclip gem its a file attachment library for Active Record. This library is very straightforward to use and make validations with each content type. For example if you want to validate an Image you can by the next validation:
class User < ActiveRecord::Base
attr_accessible :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment :avatar,
:presence => true, :content_type => { :content_type => "image/jpg" },
:size => { :in => 0..500.kilobytes }
end
Hope it helps.
I have nested resources, and I'm trying to create form partials for each individual resource to use for the new and edit action of each.
routes.rb
resources :accounts, shallow: true, :except => [ :destroy ] do
resources :service, :except => [ :destroy ]
end
If I use the following in the form partial, the edit form renders correctly and saves updates correctly, but the new form fails to render with the error undefined method services_path
/app/views/services/_service_form.html.erb
<%= simple_form_for #service do |f| %>
If I use the following in the form partial, the new form renders correctly and saves the object correctly, but the edit form fails to render with the error undefined method account_service_path
/app/views/services/_service_form.html.erb
<%= simple_form_for [#account, #service] do |f| %>
I've tried adding url: service_path(#service) to both versions of the form block declaration as shown, but it doesn't solve either problem.
There's bound to be something obvious I'm missing with getting this working, I'm sure I've had nested resources working correctly with a single form partial in the past, I just can't see what's different this time.
Change your code to following
change :service to :services
Given your Account Model
has_many :services
and Service Model have
belongs_to : account
resources :accounts, shallow: true, :except => [ :destroy ] do
resources :services, :except => [ :destroy ]
end
I'm playing with MongoDB in Rails 3 via Mongoid. I've defined the classes below, but when I attempt to create a new Hyperlink via the scaffolded view I get an error. I believe what is happening is that the Tags array is not being handled properly. I'm using the default controller scaffold. What do I need to do to ensure mongoid knows how to add the tags?
class Hyperlink
include Mongoid::Document
field :name
field :url
embeds_many :comments
references_many :tags
validates_presence_of :name, :url
validates_uniqueness_of :name, :url
end
class Tag
include Mongoid::Document
field :name
validates_uniqueness_of :name
referenced_in :hyperlink
end
Controller response
TypeError in HyperlinksController#create
can't convert Symbol into Integer
**Request**
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"yn5SwZPBIMcpzrGQeO9t3tJ2Y2Q6nlsDBPbI43ahj0k=",
"hyperlink"=>{"name"=>"Stack Overflow",
"link"=>"http:://www.stackoverflow.com",
"tags"=>{"tag"=>"programming"}},
"commit"=>"Create Hyperlink"}
Try adding accepts_nested_attributes_for :tags to HyperLink .
Ideally your parameters should look something like this(notice the change in tags):
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"yn5SwZPBIMcpzrGQeO9t3tJ2Y2Q6nlsDBPbI43ahj0k=",
"hyperlink"=>{"name"=>"Stack Overflow",
"link"=>"http:://www.stackoverflow.com",
"tags"=>{"0" => {"name"=>"programming", :id => "xxxx"}}},
"commit"=>"Create Hyperlink"}
As jdc already pointed controller and view code would be much helpful to actually point out the problem.
After searching on the web I got a problem :
I use rails3 and I do the following command
rails plugin install git://github.com/patshaughnessy/auto_complete.git
then i restart my rails server
my index.html.erb :
<%= text_field_with_auto_complete :departement, :nom, {}, {:method => :get} %>
my controller :
class AutocompController < ApplicationController
auto_complete_for :departement, :nom
end
I got a model like :
class CreateDepartements < ActiveRecord::Migration
def self.up
create_table :departements do |t|
t.column :region_id, :integer
t.column :num_dept, :string
t.column :nom, :string
end
end
def self.down
drop_table :departements
end
end
my routes.rb :
resources :autocomp, :collection => { :auto_complete_for_departement_nom => :get }
I get the following error :
No route matches {:controller=>"autocomp", :action=>"auto_complete_for_departement_nom"}
I don't get why it doesn't work? is auto_complete compatible with rails3? Or should I use jQuery?
You need to setup the route. Its missing, therefor you get this message.
Goto config/routes.rb and add your route, like:
resources : departements do
get : auto_complete_for_departement_nom, :on => :collection
end
Im using, autocomplete