Im trying to save a post with category relation.
These are my models
class Post < ActiveRecord::Base
has_many :categorizes
has_many :post_categories, :through=>:categorizes
accepts_nested_attributes_for :post_categories
end
class PostCategory < ActiveRecord::Base
has_many :categorizes
has_many :posts, :through=>:categorizes
end
class Categorize < ActiveRecord::Base
belongs_to :post
belongs_to :post_category
end
and in ActiveAdmin post.rb.
ActiveAdmin.register Post do
permit_params :title, :content, post_category_ids:[:id]
index do
selectable_column
id_column
column :title
column :post_category_id
column :created_at
actions
end
filter :created_at
form do |f|
f.inputs "Post Details" do
f.input :title
f.input :content,:input_html => { :class => "tinymce_editor" }
#f.input :post_categories, :as=> :check_boxes#, :collection => PostCategory.all
end
f.has_many :post_categories ,new_record: false do |c|
c.inputs do
c.input :title
end
end
f.actions
end
controller do
defaults :finder => :find_by_slug_url
end
end
I need to see my all categories from post_categories and i should select more than one.
i check in rails console but the post hasnt any category.
Post.First.post_categories equal to []
Try post_category_attributes instead of post_category_ids
See more here.
Related
I have something like this:
class Actor < ActiveRecord::Base
attr_accessible :name, :actor_movie_relationships_attributes
validates :name
has_many :actor_movie_relationships, autosave: true
has_many :movies, through: :actor_movie_relationships
accepts_nested_attributes_for :actor_movie_relationships
end
And:
class ActorMovieRelationship < ActiveRecord::Base
belongs_to :movie
belongs_to :actor
validates :movie, :actor, presence: true
end
Since Actor can have many movies, I'd like to paginate the ActiveAdmin form that is used to administer these relationships.
Currently I have this form (which is NOT paginated):
form do |f|
tabs do
tab 'Actor' do
f.inputs 'Basic Information' do
f.semantic_errors *f.object.errors.keys
f.input :name
f.input :deleted
end
end
tab 'Movies' do
f.inputs 'List of movies' do
f.has_many :actor_movie_relationships, heading: '', new_record: 'Add a Movie', allow_destroy: true do |a|
a.input :Movie, collection: Movie.dropdown_names_map.call
end
end
end
end
f.actions
end
Any idea how to add pagination to Movies in this form?
I have three objects: Contact, Sector, and Contact_sector.
Contact contains an id and some other irrelevant (non-reference) columns
Sector contains an id and sector column with ~10 editable entries
Contact_sector has a contact_id reference and a sector_id reference. In my mind I imagine that every sector that applies to some contact can be found here, and if a sector is un-applied it is removed from here.
I want to have a collection of checkboxes in the contact _form formed from list of entries in :sectors, but updating the form with certain boxes ticked adds/removes entries from :contact_sectors.
Where am I going wrong?
UPDATED: Fixed strong_params to permit sectors, now I am unable to find the sectors by id ActiveRecord::RecordNotFound (Couldn't find Sector with ID=["1", ""] for Contact with ID=1)
Models:
class Contact < ActiveRecord::Base
has_many :contact_sectors
has_many :sectors, through: :contact_sectors
accepts_nested_attributes_for :contact_sectors, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :sectors, :reject_if => :all_blank, :allow_destroy => true
end
class Sector < ActiveRecord::Base
has_many :contact_sectors
has_many :contacts, through: :contact_sectors
def name_with_initial
"#{sector}"
end
end
class ContactSector < ActiveRecord::Base
belongs_to :contact
belongs_to :sector
end
View:
<%= f.fields_for(:sectors) do |s| %>
<%= s.collection_check_boxes :id, Sector.all,
:id, :name_with_initial,
{ prompt: true }, { class: 'form-control' } %>
<% end %>
Controller
def edit
#contact.sectors.build
end
def contact_params
#Not sure if I need something like this or not
params['contact']['sectors'] = params['contact']['sectors']['id'].split(',')
params.require(:contact).permit(:firstname, :lastname,
contact_sectors_attributes: [:id],
sectors_attributes: [:_destroy, {:id => []}])
end
Instead of creating the join model explicitly you can just declare the relationship as has_many through: and let ActiveRecord handle the join model:
class Contact < ActiveRecord::Base
has_many :contact_sectors
has_many :sectors, through: :contact_sectors
accepts_nested_attributes_for :sector,
reject_if: :all_blank, allow_destroy: true
end
class Sector < ActiveRecord::Base
has_many :contact_sectors
has_many :contacts, through: :contact_sectors
end
class ContactSector < ActiveRecord::Base
belongs_to :contact
belongs_to :sector
end
<%= form_for(#contact) do |f| %>
<%= f.fields_for(:sectors) do |s| %>
<%= s.collection_check_boxes :id, Sector.all,
:id, :name_with_initial,
{ prompt: true }, { class: 'form-control' } %>
<% end %>
<% end %>
models
class Contact < ActiveRecord::Base
has_many :sectors, through: :contact_sectors
has_many :contact_sectors
accepts_nested_attributes_for :sectors
end
class Sector < ActiveRecord::Base
has_many :contacts, :through => :contact_sectors
has_many :contact_sectors
end
class ContactSector < ActiveRecord::Base
belongs_to :contact
belongs_to :sector
end
view
<%= form_for(#contact) do |f| %>
<% Sector.all.each do |sector| %>
<%= check_box_tag "contact[sector_ids][]", sector.id, f.object.sectors.include?(sector) %>
<%= sector.sector %>
<% end %>
<% end %>
controller
def update
#To make sure it updates when no boxes are ticked
#contact.attributes = {'sector_ids' => []}.merge(params[:contact] || {})
end
def contact_params
params.require(:contact).permit(:firstname, :lastname, sector_ids: [])
end
Recommended reading:
http://millarian.com/rails/quick-tip-has_many-through-checkboxes/
Rails 4 Form: has_many through: checkboxes
I am trying to sort comments into events using a has_many :through association using checkboxes however the selected events are not saved. Here are my models:
class Comment < ActiveRecord::Base
has_many :categorizations
has_many :events, :through => :categorizations
end
class Event < ActiveRecord::Base
has_many :categorizations
has_many :comments, :through => :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :comment
belongs_to :event
end
My comments form looks like this:
<%= simple_form_for [#post, #comment] do |f| %>
<%= f.input :title %>
<%= f.association :events, :as => :check_boxes %>
<%= f.submit "Save" %>
After reading this answer, I added this to my event and comment controllers with no luck:
def comment_params
params.require(:comment).permit(:post_id, :title, :categorization_ids => [])
end
Try:
def comment_params
params.require(:comment).permit(:post_id, :title, :event_ids => [])
end
It's hard to know what's going on though without recreating it or seeing server logs, Hopefully this will solve it.
I'm quite new to rails, so my question may seem noobish.
I have HABTM self association. A product can become a "COMBO" of products, having N products.
class Product < ActiveRecord::Base
has_many :combo_elements, class_name: "ComboElement", foreign_key: :combo_id
has_many :element_combos, class_name: "ComboElement", foreign_key: :element_id
has_many :combos, :through => :element_combos
has_many :elements, :through => :combo_elements
accepts_nested_attributes_for :elements
end
class ComboElement < ActiveRecord::Base
belongs_to :combo, :class_name => 'Product'
belongs_to :element, :class_name => 'Product'
end
With the above code, I can list all "elements" of a combo. Also I can list all "combos" a product is part of (the ComboElement table has a combo_id and an element_id)
Here is my form
<%= f.simple_fields_for :elements, #element do |b| %>
<%= b.input :element_id, :collection => Product.all.collect{ |t| [t.name, t.id ]}, selected: b.object.id %>
<%= b.link_to_remove "Remove this element" %>
<% end %>
I can successfully list all Products a combo has, but whenever I try to update, it simply doesn't work.
My Product controller has the following code
def product_params
params[:product].permit(:name, :price, elements_attributes: [:id, :element_id, :_destroy])
end
I appreciate any help.
Thanks in advance!
view (need help here) or do I need to change accepts_nested_attributes. I need a form that creates a property and associated address, beginning with City. At this point, I get the following error: unknown attribute: address. Further, I want to redirect back after create; not sure how to do that using inherited_resources. Thank you kindly.
= form_for Property.new do |f|
= f.fields_for :address do |builder|
= builder.label :city
= builder.text_field :city
= f.submit
class Property < ActiveRecord::Base
include ActiveRecord::Transitions
include ActsAsAsset
include Amendable
include Searchable
acts_as_taggable_on :tags
has_many :addresses, :as => :addressable
has_many :escrows
has_many :events, :as => :entity
has_many :phone_numbers, :as => :phoneable
accepts_nested_attributes_for :addresses
.
.
.
end
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
belongs_to :address_category
has_many :notes, :as => :notable
validate :not_blank
validates :addressable, :presence => true
private
def not_blank
self.errors.add :base, 'cannot be blank' if
self.attributes.values.all? {|attr| attr.blank? }
end
end
class PropertiesController < ApplicationController
inherit_resources
before_filter :authenticate_user!
def index
end
def show
end
end
Since it is a has_many relationship you need 'addresses' not 'address'.
Should be this:
= form_for Property.new do |f|
= f.fields_for :addresses do |builder|
= builder.label :city
= builder.text_field :city
= f.submit