I'm trying to get the product_suppliers to update via the product form. The form displays all suppliers in the supplier table but it doesn't update the join table. Not sure where the error lies. Index and show show correct details but edit is not updating the join table. Starting to go around and around in circles on this one.
Update: Changing the form to the below has got me close. But still not updating the join table. However delete works as expected if i manually add rows to the join table. They display and can be deleted. Saving adds new product_id into the row just not the associated supply_company_id value. I figure its an attribute issue but i cant see it.
app/models/product.rb
class Product < ActiveRecord::Base
### shortned for clarity
has_many :product_suppliers, :foreign_key => 'product_id'
has_many :supply_companies, :through => :product_suppliers
accepts_nested_attributes_for :product_suppliers, :allow_destroy => true
end
app/models/supply_company.rb
class SupplyCompany < ActiveRecord::Base
has_many :products, :through => :product_suppliers
has_many :product_suppliers, :foreign_key => 'supply_company_id'
end
app/models/product_supplier.rb
class ProductSupplier < ActiveRecord::Base
belongs_to :product
belongs_to :supply_company
accepts_nested_attributes_for :product
accepts_nested_attributes_for :supply_company
end
/app/admin/product.rb
ActiveAdmin.register Product do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
permit_params :id, :product_name, :product_description, :product_type_id, :product_category_id, :product_colour_id, :product_size_id,
product_images_attributes: [:id, :product_id, :product_image, :_destroy],
product_types_attributes: [:id, :product_type],
product_catergories_attributes: [:id, :product_category],
product_colour_attributes: [:id, :product_colour],
product_size_attributes: [:id, :product_size],
product_suppliers_attributes: [:id, :product_id, :supply_company_id, :_destroy],
supply_companies_attributes: [:id, :company_name]
form(:html => {:multipart => true}) do |f|
f.inputs "Product Details" do
f.input :id
f.input :product_name
f.input :product_description
#######################################################################
# Problem Lies with this piece of code Not saving the supply_company_id
# when adding a new row or updating the old rows. Delete works fine.
# cant see the error in models or permited_params.......
#######################################################################
f.inputs "Suppliers" do
f.has_many :product_suppliers do |ff|
ff.input :supply_company_id, as: :select, multiple: true, collection: SupplyCompany.all.map {|u| [u.company_name.to_s, u.id]}
ff.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove supplier'
end
end
########################################################
f.input :product_type_id, :as => :select, :collection => ProductType.all.map {|u| [u.product_type.to_s, u.id]}
f.input :product_category_id, :as => :select, :collection => ProductCategory.all.map {|u| [u.product_category.to_s, u.id]}
f.input :product_colour_id, :as => :select, :collection => ProductColour.all.map {|u| [u.product_colour.to_s, u.id]}
f.input :product_size_id, :as => :select, :collection => ProductSize.all.map {|u| [u.product_size.to_s, u.id]}
end
f.inputs "Product images" do
f.has_many :product_images do |p|
p.input :product_image, :as => :file, :label => "Image",:hint => image_tag(p.object.product_image.url(:thumb))
p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
end
end
f.actions
end
product_suppliers_schema
create_table "product_suppliers", force: true do |t|
t.integer "product_id"
t.integer "supply_company_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Update:
Changing the form to the below has got me close. But still not updating the join table. However delete works as expected if i manually add rows to the join table. They display and can be deleted.
Saving adds new product_id into the row just not the associated supply_company_id value. I figure its an attribute issue but i cant see it.
f.inputs "Suppliers" do
f.has_many :product_suppliers do |ff|
ff.input :supply_company_id, as: :select, multiple: true, collection: SupplyCompany.all.map {|u| [u.company_name.to_s, u.id]}
ff.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove supplier'
end
end
Turns out it was the multipart: :true in the code. Once I removed it from the below code everything worked as expected.
form(:html => {:multipart => true}) do |f|
f.inputs "Product Details" do
f.input :id
f.input :product_name
f.input :product_description
f.has_many :product_supply_companies do |ff|
###############################################
#REMOVED multipart: :true from the line below
###############################################
ff.input :supply_company_id, as: :select, collection: SupplyCompany.all.map {|u| [u.company_name.to_s, u.id]}
ff.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove supplier'
end
f.input :product_type_id, :as => :select, :collection => ProductType.all.map {|u| [u.product_type.to_s, u.id]}
f.input :product_category_id, :as => :select, :collection => ProductCategory.all.map {|u| [u.product_category.to_s, u.id]}
f.input :product_colour_id, :as => :select, :collection => ProductColour.all.map {|u| [u.product_colour.to_s, u.id]}
f.input :product_size_id, :as => :select, :collection => ProductSize.all.map {|u| [u.product_size.to_s, u.id]}
end
f.inputs "Product images" do
f.has_many :product_images do |p|
p.input :product_image, :as => :file, :label => "Image",:hint => image_tag(p.object.product_image.url(:thumb))
p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
end
end
f.actions
end
Related
this problem is across my rails app... The text input boxes are huge instead of simply being line ones.
in app/admin/restaurant.rb
form do |f|
f.inputs do
f.input :name, required: true
f.input :servesCuisine
f.input :description
f.input :currenciesAccepted, as: :select, collection:Restaurant.available_currencies
f.input :priceRange, as: :select, collection: Restaurant.price_range
f.input :paymentAccepted
f.input :email, as: :email
f.input :telephone, as: :phone
f.input :faxNumber, as: :phone
f.input :longitude
f.input :latitude
f.input :image, as: :file
end
f.actions
end
Huge texbox
The problem also occurs on my login page where my email field is also gigantic. How can I fix this?
Thanks
You should use as option with f.input like -
f.input :servesCuisine, as: :string
f.input :description, as: :string
You can edit your admin_users.rb file
f.input :email, as: :string
Hope this helps!!
LHH is 100% spot-on, however if you need a bit more control but don't want to involve CSS, you can do:
f.input :title, label: 'Title tag', input_html: {cols: "5", rows: "1"}
Hey guys I have this form for the NEW action that works with tinyMce
<%= f.input :titulo, :label => "Título" %>
<%= f.input :sumario, :label => "Sumário", :input_html => { :class => 'tinymce' } %>
<%= f.input :texto, :label => "Texto", :input_html => { :class => 'tinymce' } %>
<%= f.input :imagem, :label => "Imagem" %>
<%= f.input :ativa, :label => "Ativar/Desativar?", as: :boolean, boolean_style: :inline %>
<%= f.input :destaque, :label => "Em Destaque?", as: :boolean, boolean_style: :inline %>
It's actually a partial that the EDIT action will use aswell. The thing is that the tinyMce editor only shows on NEW and never shows on EDIT.
!!!EDIT: NEVER MIND! RESOLVED
NEVER MIND!
dumb question!
just needed to add this on top of the page:
<%= tinymce_assets %>
and this on the bottom:
<%= tinymce %>
I didn't have this on EDIT xD SORRY FOR THE QUESTION
I have a problem with my following setup. It is a polymorphic n:m association between the feature, page and slide model. the join model is the slideshow model.
feature.rb - a model in a refinerycms extension
module Refinery
module MyExtension
class Feature < Refinery::Core::BaseModel
# associations
has_many :slideshows, :as => :slideable, :class_name => "::Refinery::MyExtension::Slideshow"
has_many :slides, :through => :slideshows, :class_name => "::Refinery::MyExtension::Slide"
accepts_nested_attributes_for :slides, :allow_destroy => :false
end
end
end
page_decorator.rb - a decorator for the refinery page model
Refinery::Page.class_eval do
# associations
has_many :slideshows, :as => :slideable, :class_name => "Refinery::MyExtension::Slideshow"
has_many :slides, :through => :slideshows, :class_name => "Refinery::MyExtension::Slideshow"
end
slide.rb - a model in a refinerycms extension
module Refinery
module MyExtension
class Slide < Refinery::Core::BaseModel
# associations
has_many :slideshows, :class_name => "::Refinery::MyExtension::Slideshow"
has_many :pages, :through => :slideshows, :source => :slideable, :source_type => "::Refinery::Page"
has_many :features, :through => :slideshows, :source => :slideable, :source_type => "::Refinery::MyExtension::Feature"
end
end
end
slideshow.rb - a model in a refinerycms extension
module Refinery
module MyExtension
class Slideshow < Refinery::Core::BaseModel
# associations
belongs_to :slide, :class_name => "::Refinery::MyExtension::Slide"
belongs_to :slideable, :polymorphic => true
accepts_nested_attributes_for :slide, :allow_destroy => :false
end
end
end
I think the associations are correct, but I have a problem creating new slides within the feature#form.
feature-form:
<%= form_for [refinery, :my_extension_admin, #feature] do |f| -%>
<%= f.text_field :field1 %>
<%= f.text_field :field2 %>
<%= "some-more-fields..." %>
<% slideshows = #feature.slideshows.empty? ? #feature.slideshows.build : #feature.slideshows %>
<%= f.fields_for(:slideshows, slideshows) do |slideshow_form| %>
<%= slideshow_form.hidden_field :position, :value => 0 %>
<% slide = slideshow_form.object.slide.nil? ? slideshow_form.object.build_slide : slideshow_form.object.slide %>
<%= slideshow_form.fields_for(:slide, slide) do |slide_form| %>
<%= slide_form.text_field :a_field -%>
<%= slide_form.text_field :another_field -%>
<% end %>
<% end %>
<% end %>
If I hit "save" I get following error:
::Refinery::MyExtension::Slideshow(#70206105711540) expected, got Array(#70206014033940)
My params hash looks as following:
{"utf8"=>"✓", "authenticity_token"=>"my-token", "switch_locale"=>"de", "feature"=>{"field1"=>"a value", "field2"=>"some value", "slideshows"=>{"position"=>"0", "slide_attributes"=>{"a_field"=>"some value", "another_field"=>"some value"}}}, "action"=>"create", "controller"=>"refinery/my_extension/admin/features", "locale"=>:de}
My controller looks as following:
module Refinery
module MyExtension
module Admin
class FeaturesController < ::Refinery::AdminController
crudify :'refinery/my_extension/feature', :title_attribute => 'name', :xhr_paging => true
def feature_params
params.require(:feature).permit(:field1, :field2, :slideshows => [:position, :slide => [:a_field, :anothter_field]])
end
end
end
end
end
I use Rails 4.1.5 and refinerycms. But I think it has nothing to do with refinerycms, just with normal Rails behavior.
I already took a look at following resources:
ActiveRecord::AssociationTypeMismatch when attempting to save nested attributes in Rails
Has Many Through Association Polymorphic Error
accepts_nested_attributes_for with has_many => :through Options
Anybody an idea what I am missing?
try to create method "new" something like this
def new
#feature = ::Refinery::MyExtension::Feature.new
#feature.slideshows.build
end
and edit method
def edit
#feature = ::Refinery::MyExtension::Feature.find(params[:id])
#feature.slideshows.build
end
I have a rails app where users can upload pins, I want to use the [acts_as_commentable_gem][1] to allow users to comment pins, here is my config:
app/models/pin.rb
class Pin < ActiveRecord::Base
acts_as_commentable
end
app/controlers/pins_controller.rb
def show
#pin.find params[:id]
#comment = #pin.comments.new
end
app/views/pins/show.html.erb
<%= form_tag "/pins/add_new_comment" do %>
<%= hidden_field_tag "id", post.id %>
<%= text_area_tag "comment[comment]" %>
<%= submit_tag "Pin Comment" %>
<% end %>
app/models/comment.rb
class Comment < ActiveRecord::Base
include ActsAsCommentable::Comment
belongs_to :commentable, :polymorphic => true
default_scope -> { order('created_at ASC') }
# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_voteable
# NOTE: Comments belong to a user
belongs_to :user
end
app/controllers/pin_controller.rb
def add_new_comment
pin = Pin.find(params[:id])
pin.comments << Pin.new(params[:comment])
redirect_to :action => :show, :id => pin
end
finally in my config/routes
get "/pins/add_new_comment" => "pins#add_new_comment", :as => "add_new_comment_to_pins", :via => [:pin]
But I run to a routing error:
undefined local variable or method `acts_as_commentable' for PinsController:Class
I am really not sure where this error come from, any ideas?
I am not really sure but you route shouldn't be like
get "/pins/:id/add_new_comment" => "pins#add_new_comment", :as => "add_new_comment_to_pins"
When using the select_recurring form helper for the Ice_Cube gem in Formtastic with ActiveAdmin I receive a unable to find input class for select_recurring error.
form do |f|
#Debugging
f.semantic_errors *f.object.errors.keys
f.inputs do
f.input :title
f.input :description, :hint => "Full description for the event"
f.input :short_description, :hint => "Shot description for the event that will be displayed as a thumbnail"
f.input :published
f.input :event_date
f.input :recurring_rule, :as => :select_recurring
end
f.actions
end
Accessing it directly (below) works for the recurring schedule element but breaks all of the rest of the formtastic f.input elements.
f.select_recurring :recurring_schedule
How can I incorporate select_recurring into ActiveAdmin?
worked for me (AA 1.0.0.pre1)
form do |f|
f.inputs do
#...
li do
f.label :recurring_rule_column
f.select_recurring :recurring_rule_column, nil, :allow_blank => true
end
#...
end
f.submit
end