Activeadmin has_many form for show and edit only - ruby-on-rails-4

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

Related

Rails nested_form Invalid association. Make sure that accepts_nested_attributes_for is used for assoctiation

I am using rails 4.0.4 with nested_form 0.3.2 to build an app that allows users to organize movies in lists.
I have these main models, a List model (I've excluded things such as validations):
class List < ActiveRecord::Base
belongs_to :user
has_many :list_movie_pairs
has_many :movies, :through => :list_movie_pairs
accepts_nested_attributes_for :list_movie_pairs, :allow_destroy => true
accepts_nested_attributes_for :movies, :allow_destroy => true
end
A Movie model:
class Movie < ActiveRecord::Base
has_many :list_movie_pairs
has_many :lists, :through => :list_movie_pairs
has_many :reviews
end
A ListMoviePair model for the many-to-many relationship:
class ListMoviePair < ActiveRecord::Base
belongs_to :list
belongs_to :movie
validates_presence_of :list_id, :movie_id
validates_uniqueness_of :movie_id, scope: :list_id
end
I am trying to build an interface for the user to add movies to a created list. These routes serve my purpose:
get "/users/:username/lists/:id/add" => "lists#add_movies", :as => :user_list_list_movie_pairs
post "/users/:username/lists/:id/add" => "lists#submit_movies"
These are the classes in my ListsController that should make this possible:
def add_movies
#pair = list.list_movie_pairs.new # "list" is a helper that returns the current list
end
def submit_movies
#list = current_user.lists.find(params[:id])
#pair = #list.list_movie_pairs.new(pair_params)
if #pair.save
redirect_to user_list_path(current_user.username, #list)
else
render :add_movies
end
end
def list_params
params.require(:list).permit(:name, :description, :private, \
list_movie_pairs_attributes: [:id, :list_id, :movie_id, :_destroy], \
movies_attributes: [:id, :title, :_destroy])
end
And this is the form in my view
<%= nested_form_for [current_user, list, #pair] do |f| %>
<%= f.fields_for :movies do |movie_form| %>
<%= movie_form.text_field :title %>
<%= movie_form.link_to_remove "Remove movie" %>
<% end %>
<%= f.link_to_add "Add movie", :movies %>
<% end %>
I get this error when trying to access the view:
Invalid association. Make sure that accepts_nested_attributes_for is used for :movies association.
Which pops at this line:
<%= f.link_to_add "Add movie", :movies %>
Note 1: I am using the Devise gem for users, hence the "current_user" helper;
Note 2: I have tried using both "movies" and "list_movie_pairs", i.e.:
f.fields for :list_movie_pairs
and
f.link_to_add "Add movie", :list_movie_pairs
in my view, neither association seems to work
Your code in the view should be like this
<%= nested_form_for [current_user, list, #pair] do |f| %>
<%= f.fields_for :movies do |movie_form| %>
<%= movie_form.text_field :title %>
<%= movie_form.link_to_remove "Remove movie" %>
<%= movie_form.link_to_add "Add movie", :movies %> #note the change here
<% end %>
<% end %>
Update
There are several issues in your code
1.In your List model,this line is not required
accepts_nested_attributes_for :movies, :allow_destroy => true #not requied
2.In your ListsController,you have this line
#pair = #list.list_movie_pairs.new(pair_params)
It should be
#pair = #list.list_movie_pairs.new(list_params) because you have list_params method not pair_params

Allowing has_many multiselect with rails 4 & active admin

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 !

activeadmin carrierwave image hint not displayed

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.

Nested table isn't updated in rails 4

I am trying to update a nested form. The client table is updated successfully but the location table isn't update. Instead of its creating a new location. Do you guys have any solution ? I've already spend one day on it.
My models:
class Client < ActiveRecord::Base
has_many :locations, :dependent => :destroy
accepts_nested_attributes_for :locations, :allow_destroy => true, :update_only => true
end
class Location < ActiveRecord::Base
belongs_to :client
end
Selectcom::Application.routes.draw do
resources :clients
end
My Controller:
class ClientsController < ApplicationController
before_action :set_client, only: [:edit, :update]
def index
#clients = Client.paginate(page: params[:page])
end
def new
#client = Client.new
end
def create
#client = Client.new(client_params)
if #client.save
flash[:success] = "Client added successfully"
redirect_to clients_path
else
render 'new'
end
end
def edit
end
def update
if #client.update(only_client_params)
flash[:success] = "Job updated successfully"
redirect_to clients_path
else
render 'edit'
end
end
private
def set_client
#client = Client.find(params[:id])
end
def client_params
params.require(:client).permit(
:name,
:phone,
:fax,
:url,
:address,
:city,
locations_attributes: [
:site,
:fax,
:phone,
:url,
:address,
:_destroy
]
)
end
end
This is the client's edit.html.erb form
<%= form_for(#client, class: 'form-horizontal') do |f| %>
<%= render(partial: 'client_field', locals: {f: f}) %>
<%= f.fields_for :locations do |l| %>
<%= l.hidden_field :client_id, value: #client.id %>
<%= l.hidden_field :_destroy %>
<%= l.text_field :site, class: 'form-control' %>
<% end %>
<%= f.submit "Save", class: "btn lg-button" %>
<% end %>
Actually, you have two issues here. One in your Model and other in your Controller permitted params. Let's dig into them:
1) Model
The update_only option is ignored when used with collection association (it is your case), as said in Rails documentation:
For a one-to-one association, this option allows you to specify how
nested attributes are to be used when an associated record already
exists. In general, an existing record may either be updated with the
new set of attribute values or be replaced by a wholly new record
containing those values.
By default the :update_only option is false and the nested attributes are used to update the existing record only if they include
the record's :id value. Otherwise a new record will be instantiated
and used to replace the existing one.
However if the :update_only option is true, the nested attributes are
used to update the record's attributes always, regardless of whether
the :id is present. The option is ignored for collection
associations.
So, the first step would be removing the update_only option of your Client class, because it will be ignored since you have a has_many association (collection association) with locations:
class Client < ActiveRecord::Base
has_many :locations, :dependent => :destroy
accepts_nested_attributes_for :locations, :allow_destroy => true
end
2) Controller
You have to permit the :id key for the :locations_attributes array. Since your update_only option in the model was ignored, Rails needs the param to tell that it is a record that exists and it's being updated rather than created.
You can solve your issue using the following in your client_params method (note the addition of the id key in your :locations_attributes key):
def client_params
params.require(:client).permit(
:name,
:phone,
:fax,
:url,
:address,
:city,
locations_attributes: [
:id, # Should be present; otherwise, Rails thinks that is a new record
:site,
:fax,
:phone,
:url,
:address,
:_destroy
]
)
end
I hope it helps !!

ActiveAdmin deleting attached CarrierWave images on form submission

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.