HI I am using active admin with carrier gem. I am unable to view preview on image upload in hint
my image src is always empty i.e f.template.image_tag(f.object.image.url) is empty
I can see image relative url after clicking upload button.
my form looks like this
ActiveAdmin.register Product do
permit_params :name, :description , :category_id , :image
form(:html => { :multipart => true }) do |f|
f.inputs "Product" do
f.input :category_id , :as => :select , :collection => Category.all
f.input :name
f.input :description
f.input :image, :as => :file, :hint => f.template.image_tag(f.object.image.url)
end
f.actions
end
end
model
class Product < ActiveRecord::Base
belongs_to :category
mount_uploader :image, ImageUploader
end
serializer
class ProductSerializer < ActiveModel::Serializer
attributes :id, :name, :description , :image
end
my uploader looks like this
class ImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
GENERATE HTML
<label class="label" for="product_image">Image</label>
<input id="product_image" name="product[image]" type="file">
<p class="inline-hints"><img src=""></p>
After a few modifications, for me works fine:
ActiveAdmin.register Team do
permit_params :name, :country :image
index do
selectable_column
column :nome
column :country
actions
end
form do |f|
f.inputs "Team" do
f.input :nome
f.input :country
f.input :image, :image_preview => true
end
f.actions
end
show do
attributes_table do
row :nome
row :country
row :image do
image_tag(equipe.image.url)
end
end
end
end
You can see that I use three options:
- Index: to show the list of items
- Form: for the form
- Show: to show the item details
This way you can customize all pages.
Related
I have models products and gallery with has_many belongs_to relation, I'm implementing nested_attributes feature, Problem here is: When I click on add gallery two inner forms get created instead of one, as shown in below image:
CODE
form do |f|
f.inputs "Product" do
f.input :title, :required => true
f.input :description
f.input :price
f.input :display_image
f .input :product_detail, :hint => "Upload PDF file"
f.input :category
end
f.inputs 'Product Gallery' do
f.has_many :galleries, allow_destroy: true, new_record: 'Add Gallery' do |c|
c.input :image, :hint => c.template.image_tag(c.object.image.url(:thumb))
end
end
f.inputs 'Product Specification' do
f.has_many :specifications, allow_destroy: true, new_record: true do |c|
c.input :specification_label
c.input :specification_details
end
end
f.actions
end
I need help on this. I'm not able to solve this!!, any help would be appreciable.
Try this.
f.inputs 'Product Gallery' do
f.has_many :galleries do |c|
c.input :image, :hint => c.template.image_tag(c.object.image.url(:thumb))
c.input :_destroy, :as => :boolean
end
end
Hope this will work for you.
I'm using devise gem with paperclip to handle authentication and upload pictures. Problem is that I have used paperclip on the same model twice for storing two pictures (let's call those paperclip columns :avatar , :superbadge), my model name is User.
Now, when I choose to upload two pictures, my rails application ignores the first file that I have chosen, instead It uses second chosen file and saves it in the first paperclip column, leaving second paperclip column blank. How do I fix it?
My application controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_devise_permitted_parameters, if: :devise_controller?
protected
def configure_devise_permitted_parameters
registration_params = [:name, :email, :password, :password_confirmation,:avatar,:superstarbadge]
if params[:action] == "update"
devise_parameter_sanitizer.for(:account_update) {
|u| u.permit(registration_params << :current_password)
}
elsif params[:action] == "create"
devise_parameter_sanitizer.for(:sign_up) {
|u| u.permit(registration_params)
}
end
end
end
My User.rb model:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :validatable
has_attached_file :avatar, :styles => { :small => "100x100>" }
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
has_attached_file :superstarbadge, :styles => { :tiny => "100x100>" }, :default => "superbadge.jpg"
validates_attachment_content_type :superstarbadge, :content_type => /\Aimage\/.*\Z/
has_many :questions
has_many :answers
def to_s
email
end
end
My form_for for creating new user with devise gem (I'm using slim template language not erb):
h1 Sign up
= form_for(resource, as: resource_name, url: registration_path(resource_name), :html => { :multipart => true }) do |f|
= devise_error_messages!
.field
label= f.label :name
= f.text_field :name, autofocus: true
.field
label= f.label :email
= f.email_field :email, autofocus: true
.field
label= f.label :password
= f.password_field :password, autocomplete: 'off'
.field
label= f.label :password_confirmation
= f.password_field :password_confirmation, autocomplete: 'off'
.field
= f.label :avatar
= f.file_field :avatar
.field
= f.file_field :avatar
div
= f.submit "Sign up"
Both the file_field's in your form are referring to avatar field so when you submit the form the second chosen file (i.e., latest) gets saved as avatar. There is no file_field for superstarbadge so it never gets saved.
You need one file_field for avatar and other one for superstarbadge. So, your code should look like:
.field
= f.label :avatar
= f.file_field :avatar
.field
= f.file_field :superstarbadge ## This one should be superstarbadge and NOT avatar
When using activeadmin with rails 4, permit_params has to be set to allow fields to be saved.
simple fields are working, however a multi select for a has_many field is silently ignored. How can i set permit_params for that field?
so home has_many providers, and my admin looks like this:
ActiveAdmin.register Home do
permit_params :title, :intro, :providers, :providers_attributes => [:id]
menu :parent => "Content" , :label => "Home Page"
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs do
f.input :title
f.input :intro
f.input :providers
end
f.actions
end
index do
column :link
actions
end
end
I don't know if they changed it with Rails 4 .. but I've always done it this way
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs do
f.input :title
f.input :intro
end
f.has_many :providers do |pf|
pf.input :title #or whatever attributes you have there
pf.input :_destroy, :as => :boolean, :label => "Delete" if !pf.object.nil?
end
f.actions
end
And in your model.rb you should have something similar to
attr_accessible :providers_attributes
has_many :providers
accepts_nested_attributes_for :providers, :allow_destroy => true
Let me know if that helps !
I'm not sure what I'm doing wrong here. File uploads are working but if I submit the form without selecting a file to upload it deletes the previously attached image(s).
Here's what the ActiveAdmin form looks like:
form do |f|
f.inputs do
f.input :model_number
f.input :description
f.input :slug
f.input :categories
f.has_many :product_images do |image|
image.input :product_id, as: :hidden, id: :product_id, input_html: { value: "%i" }
image.input :image
end
end
f.actions
end
And the relevant parts of the respective models:
class ProductImage < ActiveRecord::Base
belongs_to :product
mount_uploader :image, ProductImageUploader
validates :image, :product_id, presence: true
end
class Product < ActiveRecord::Base
has_many :product_images, dependent: :destroy
accepts_nested_attributes_for :product_images
validates_associated :product_images
end
Any insights would be much appreciated. Thanks!
It looks like I was a bit overzealous with my validations. Removing product id from the ProductImages validations and simplifying image.input :product_id, as: :hidden, id: :product_id, input_html: { value: "%i" } in the form to image.input :product_id, as: :hidden in the form makes image attach correctly to existing products or new products.
First please note that i am working with rails 4 and activeadmin rails 4 branch. https://github.com/gregbell/active_admin/tree/rails4
I have two models pictue which is ploymorphic
class Picture < ActiveRecord::Base
belongs_to :picture_category
belongs_to :imageable, polymorphic: true
mount_uploader :image, ImageUploader
end
And ship
class Ship < ActiveRecord::Base
has_many :pictures, as: :imageable
accepts_nested_attributes_for :pictures
end
And activeadmin ship.rb
ActiveAdmin.register Ship do
form do |f|
f.inputs do
f.inputs
f.has_many :pictures, :sortable => :picture_categories do |ff|
ff.input :name
end
end
f.actions
end
controller do
def permitted_params
params.permit(:ship => [:name, :title, :short_desc, :description, :position, :pictures, :enabled, :ship_provider_id, :picture_category_id, :picture_id])
end
end
end
but the result is this:
How can get rid of delete button and have submit
how can i show this form only on show and edit form because thats not make sense having child before parents
Please point what are mistakes in code.
I know how make a solve a first problem/
You need insert :submit action instead button for your nested resource input. Example:
form do |ship|
ship.inputs t(:ship_details) do
ship.input :name, :label => t(:name)
ship.input :description, :label => t(:description)
ship.has_many :pictures do |picture|
picture.input :url
picture.input :imgdescription
picture.actions do
picture.action :submit
end
end
end
end