I have a Campaign model and a Category model. They have a has-many-through relationship with each other. The intermediate model is campaign_categories.
Campaign:
class Campaign < ActiveRecord::Base
attr_accessible :name, :carousel_image, :show_in_carousel, :title, :carousel_description,
:video_embed_code, :goal, :end_date, :backer_count, :author_name, :author_photo,
:author_description_md, :author_description_html, :description_markdown, :description_html,
:funds_description_md, :funds_description_html, :campaign_status_id
#associations
has_many :campaign_categories
has_many :categories, through: :campaign_categories
end
Category:
class Category < ActiveRecord::Base
attr_accessible :name
#associations
has_many :campaign_categories
has_many :campaigns, through: :campaign_categories
end
Campaign_Category:
class CampaignCategory < ActiveRecord::Base
attr_accessible :campaign_id, :category_id
belongs_to :campaign
belongs_to :category
end
I have following in campaigns.rb for Activeadmin:
ActiveAdmin.register Campaign do
form :html => { :enctype => 'multipart/form-data'} do |f|
f.inputs "Campaign Basic Information" do
f.input :name
f.input :categories
end
f.actions
end
end
The categories show up correctly in a multi select box. But I receive following error on form submission:
Can't mass-assign protected attributes: category_ids
I tried calling accepts_nested_attributes_for :categories in Campaign, but that did not work. How can I resolve this issue? Thanks.
Adding :category_ids to your attr_accessible call in the Campaign model should do it
Related
model.rb
class Category < ApplicationRecord
has_many :video_categories
has_many :videos, :through => :video_categories
end
class VideoCategory < ApplicationRecord
belongs_to :video
belongs_to :category
end
class Video < ApplicationRecord
has_many :video_categories
has_many :categories, :through => :video_categories
end
I have 3 model. Many to Many association.
rails_admin.rb
config.model Video do
list do
field :url
field :title
field :description
field :category, :belongs_to_association
field :views
field :votes
end
edit do
field :url do
required true
end
field :title do
required true
end
field :description do
html_attributes do
{:maxlength => 200,
:cols => 50}
end
end
field :category, :belongs_to_association
field :thumbnail
end
end
Here's the error in browser:
These are my models:
Product.rb:
class Product < ApplicationRecord
belongs_to :position
has_many :products_sizes
has_many :sizes, through: :products_sizes
has_many :reviews, dependent: :destroy
accepts_nested_attributes_for :products_sizes
end
Products_size.rb:
class ProductsSize < ApplicationRecord
belongs_to :product
belongs_to :size
has_many :prices
accepts_nested_attributes_for :prices
end
Size.rb:
class Size < ApplicationRecord
has_many :products_sizes
has_many :products, through: :products_sizes
end
and Price.rb:
class Price < ApplicationRecord
belongs_to :products_size
end
In ActiveAdmin I need to make a form for Product, for when I update the product, I could create a Price, so a part of the form looks like this:
... #here is the begining of the form
f.inputs 'Sizes' do
f.semantic_fields_for ProductsSize.where(product_id: params[:id], size_id: Product.find(params[:id]).products_sizes.size.to_i).first.prices.new do |ps|
ps.input :products_size_id, label: 'Size', as: :select, collection: Product.find(params[:id]).sizes.map { |s| ["#{s.title}", s.id] }
ps.input :quantity
ps.input :amount
li do
link_to 'Add size', '#'
end
end
end
It's all seems to be good, except when clicking the submit button, the Price isn't created. I think, that's because the permit_params are not specified for price. How can I specify them? Thanks.
ActiveAdmin.register Post do
permit_params :title,
comments_attributes: [:name, :hidden]
end
Post is a moodel and comments is another. You can use params from comments with comments_attributes, if your model name is price you can use price_attributes: [...params...].
Here is how you allow parameters in active admin
ActiveAdmin.register Post do
permit_params :title, :content, :author
end
This is just an example, use your own params
I have set a polymorphic association and added a nested form in the view. Im trying to create the main record and the association at the same time. The main record gets created but the association won't.
Here are the two models in question :
class UnRegistered < ActiveRecord::Base
has_one :vehicle, as: :detailable, dependent: :destroy
belongs_to :dealer
class Vehicle < ActiveRecord::Base
belongs_to :purchase_details, polymorphic: true
belongs_to :brand
belongs_to :model
belongs_to :color
belongs_to :customer
Here's the form definitions :
<%= form_for(#un_registered, url: panel_un_registereds_path, remote: true) do |f| %>
<%= f.fields_for :vehicle do |f_vehicle| %>
Here's a sample params set I get :
{"utf8"=>"✓", "un_registered"=>{"vehicle"=>{"brand_id"=>"", "model_id"=>"", "year"=>"", "engine_number"=>"gdfg", "chassis_number"=>"", "color"=>"", "options"=>""}, "original_price"=>"", "insurance"=>"", "freight"=>"", "tt"=>"", "tt_date"=>"", "duty"=>"", "clearance_fee"=>"", "other_expenses"=>"", "dealer_id"=>"", "landing_date"=>"", "loading_date"=>""}, "controller"=>"panel/un_registereds", "action"=>"create"}
Here's the controller actions :
def create
#un_registered = UnRegistered.create(un_registered_params)
end
def un_registered_params
params.require(:un_registered).permit(:original_price, :insurance, :freight, :tt, :tt_date, :duty, :clearance_fee, :other_expenses, :loading_date, :landing_date, :dealer_id, vehicle_attributes: [:id, :brand_id, :model_id, :engine_number, :chassis_number, :color_id, :year, :options, :selling_price, :customer_id, :purchase_date, :_destroy])
end
Full form code :
https://gist.github.com/THPubs/9665e0e5594e15fcc76a
New method :
def new
#un_registered = UnRegistered.new
end
Your form is fine. You just need to add below changes.
In your un_registered.rb model
class UnRegistered < ActiveRecord::Base
has_one :vehicle, as: :detailable, dependent: :destroy
belongs_to :dealer
accepts_nested_attributes_for :vehicle #this one
end
And in your controller,
def new
#un_registered = UnRegistered.new
#un_registered.build_vehicle #this one
end
I have a problem with my ruby on rails 4 app with the activeadmin gem
I have generated multiple resources that works well but I have a problem with a resource that has a has_many through relationship. The new records are not registered through the join table.
Here is my model :
video.rb
class Video < ActiveRecord::Base
has_many :album_videos
has_many :albums, :through => :album_videos
end
Album.rb
class Album < ActiveRecord::Base
has_many :album_videos
has_many :videos, :through => :album_videos
end
Album_video.rb
class AlbumVideo < ActiveRecord::Base
belongs_to :video
belongs_to :album
end
And the activeadmin file for album
album.rb
ActiveAdmin.register Album do
menu :priority => 6, :label => proc{ "Album de vidéos" }
filter :album_videos
permit_params :titre, :description, videos_attributes: [:id, :titre, :_update,:_create]
form do |f|
f.inputs
f.inputs "Vidéos" do
f.input :videos, as: :check_boxes, collection: Video.all, :member_label => :titre
end
f.actions
end
end
Album's form is well displayed, I have a panel with all my registered videos.
When I create a new album, the album is registered but nothing in the album_videos table.
Anything I am missing ?
I found the solution. You juste have to add
video_ids: []
to your permit_params.
Mine looks like this now :
permit_params :titre, :description, video_ids: []
I'm having trouble setting up this association between my models.
A User has many Accommodations, and Accommodations have one User.
Accommodations have many Notifications, and Notifications have one Accommodation.
Requests have many Notifications.
How can I make it so that I can get all of the Requests for a given User ( that is, User -> Accommodations (each) -> Notification -> Request)?
Update:
Here's my current controller file:
class PanelController < ApplicationController
before_filter :login_required
def index
#accommodations = current_user.accommodations.all
#requests = Array.new
#accommodations.each do |a|
a.notifications.each do |n|
#requests << Request.where('id' => n.request_id)
end
end
end
end
And models:
models/user.rb
class User < ActiveRecord::Base
[snip]
has_many :accommodations
has_many :notifications,
:through => :accommodations
end
models/accommodation.rb
class Accommodation < ActiveRecord::Base
validates_presence_of :title, :description, :thing, :location, :spaces, :price, :photo
attr_accessible :photo_attributes, :title, :description, :thing, :location, :spaces, :price
has_one :photo
has_many :notifications
belongs_to :user
accepts_nested_attributes_for :photo, :allow_destroy => true
end
models/notification.rb
class Notification < ActiveRecord::Base
attr_accessible :accommodation_id, :request_id
has_one :request
belongs_to :accommodation
end
models/request.rb
class Request < ActiveRecord::Base
belongs_to :notifications
attr_accessible :firstname, :lastname, :email, :phone, :datestart, :dateend, :adults, :children, :location, :status
validates_presence_of :firstname, :lastname, :email, :phone, :datestart, :dateend, :children, :adults, :location
end
Something like this should work:
#reqs = []
#user.accommodations.all.each do |a|
#reqs << a.notification.request
end
Assuming this is correct:
class User
has_many :accommodations
end
class Accommodation
belongs_to :user
has_many :notifications
end
class Notification
belongs_to :accomodation
belongs_to :request
end
class Request
has_many :notifications
end
Using has_many :through will not work for multiple models, as seen here: Ruby-on-Rails: Multiple has_many :through possible?
But you can do something like this in your user model:
class User
has_many :accommodations
has_many :notifications,
:through => :accommodations
def requests
self.notifications.all.collect{|n| n.request }
end
end