embedded polymorphic association model form - ruby-on-rails

I have these models:
model 1:
module Ems
class Article < ActiveRecord::Base
has_many :images, :as => :imageable
accepts_nested_attributes_for :images
end
end
model 2:
module Ems
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
end
view:
= form_for #article do |f|
%div
= f.label :title
= f.text_field :title
%div
- f.fields_for :images do |builder|
= builder.label :title
= builder.text_field :title
I don't get any errors however I also dont get the form fields for the embedded image form. All I get is an empty DIV.
Can anyone point me in the right direction?
Thanks

try after replacing - with = at where you are calling fields_for
= f.fields_for :images do |builder|
The fields_for returns form HTML, so you need to render it on UI.

Related

Rails - Set additional attribute on has_many through record

Using Rails 6.0.3.3 and ruby '2.6.0'.
I have these 3 models connected via a has_many through relation.
class Recipe < ApplicationRecord
has_many :layers
has_many :glazes, through: :layers
end
class Glaze < ApplicationRecord
has_many :layers
has_many :recipes, through: :layers
end
class Layer < ApplicationRecord
belongs_to :recipe
belongs_to :glaze
# there is a column named "coat_type" here that I would like to set
end
Everything is working great for creating a new Recipe and it automagically creating its related Layer record. But now I would like to also set a coat_type attribute on the Layer record when it's created, but I can't seem to figure out how I could do something like that.
The Form View Partial
= form_with model: recipe, local: true do |form|
%p
= form.label :name
%br
= form.text_field :name
%p
= form.label :description
%br
= form.text_area :description
.recipe-glaze
= form.collection_select(:glaze_id, Glaze.all, :id, :name, { prompt: "Select Glaze" }, { name: 'recipe[glaze_ids][]' })
.recipe-glaze
= form.collection_select(:glaze_id, Glaze.all, :id, :name, { prompt: "Select Glaze" }, { name: 'recipe[glaze_ids][]' })
%p
= form.submit
The Controller Create Action (and strong params acton)
def create
#recipe = Recipe.new(recipe_params)
if #recipe.save
redirect_to #recipe
else
render 'new'
end
end
private
def recipe_params
params.require(:recipe).permit(:name, :description, glaze_ids: [])
end
Ideally, I would be able to add another select box in the form and the user would use that to set the coat_type of the Layer record that will be created. But I can't figure out how I could pass that into the controller and have it know what to do with that value.
Is this something that is possible, or am I approaching this incorrectly?
So I actually ended up stumbling upon the "cocoon" gem thanks to this comment. By following the setup instructions for "cocoon", I was able to tweak my code to do what I needed.
My Recipe model changed to this ::
class Recipe < ApplicationRecord
has_many :layers, inverse_of: :recipe
has_many :glazes, through: :layers
accepts_nested_attributes_for :layers, reject_if: :all_blank, allow_destroy: true
end
My controller's strong params action changed to this ::
private
def recipe_params
params.require(:recipe).permit(:name, :description, layers_attributes: [:id, :glaze_id, :coat_type, :_destroy])
end
My form view partial changed to ::
- if recipe.errors.any?
= render partial: 'errors', locals: { recipe: recipe }
%p
= form.label :name
%br
= form.text_field :name
%p
= form.label :description
%br
= form.text_area :description
%h3 Layers
#layers
= form.fields_for :layers do |layer|
= render 'layer_fields', f: layer
.links
= link_to_add_association 'add layer', form, :layers
%p
= form.submit
and the "layer_fields" partial referenced in the form looks like this ::
.nested-fields
.field
= f.label :glaze_id
%br
= f.collection_select(:glaze_id, Glaze.all, :id, :name, { prompt: "Select Glaze" } )
.field
= f.label :coat_type
%br
= f.text_field :coat_type
= link_to_remove_association 'remove layer', f
Making those changes using the "Cocoon" gem, I was able to accomplish what I needed. Hopefully this helps someone else in the future.

ActiveRecord::AssociationTypeMismatch in RoastsController#create

This is a new error to me, and struggling to resolve it. It also states: Roaster(#70130698993440) expected, got "1" which is an instance of String(#70130675908140)
It's highlighting my create method in my Roasts Controller:
def create
#roast = Roast.new(roast_params)
The scenario is that I'm trying to create a triple nested form. for three models Roasts Countries and Regions where roasts has many countries and countries has many regions.
I'm assuming there is something wrong with the roast params, but I can see what it is. I have added the associations there for the nested models
def roast_params
params.require(:roast).permit(:roaster, :name, :bestfor, :beans, :roast, :tastingnotes, :notes, :slug, :avatar, :countries_attributes => [:country_name, :regions_attributes => [:region_name]])
end
my form
<div class="form-group">
<%= form.fields_for :countries do |countries_form| %>
<%= countries_form.label :country %>
<%= countries_form.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= form.fields_for :regions do |regions_form| %>
<%= regions_form.label :region %>
<%= regions_form.text_field :region_name, class: "form-control" %>
<% end %>
<% end %>
</div>
Roast Controller
...
def new
#roast = Roast.new
#roast.countries.build.regions.build
end
...
roast model
class Roast < ApplicationRecord
has_many :tastings
has_many :countries
has_many :notes, through: :tastings
has_many :comments, as: :commentable
belongs_to :roaster
accepts_nested_attributes_for :countries
country model
class Country < ApplicationRecord
has_many :regions, inverse_of: :country
accepts_nested_attributes_for :regions
belongs_to :roasts
region model
class Region < ApplicationRecord
belongs_to :country
I've nested the regions params in the country params, is that correct? I also saw on SO other issues with suggestions for setting config.cache_classes to true in development.rb but that didn't help here.
Update
So looking at this further, I believe it's not related to the nested forms, but rather a collection_select I'm using.
<%= form.label :roaster, class: 'control-label' %>
<%= form.collection_select(:roaster, Roaster.order(:roaster_name).all, :id, :roaster_name, prompt: true, class: "form-control") %>
So this select is pulling the roaster_name from a model called Roaster.
My params now look like the below:
params.require(:roast).permit(:roaster_name, :roaster, :name, :bestfor, :beans, :roast, :tastingnotes, :notes, :slug, :avatar, :countries_attributes => [:country_id, :country_name, :regions_attributes => [:region_id, :region_name]])
And looking at the console when submitting the form, it seems that just the :id of Roaster is getting passed, rather than the value of :roaster_name.
{"utf8"=>"✓",
"authenticity_token"=>"EG+zty85IiVsgipm1pjSAEZ7M66ELWefLq8Znux+cf89sSnVXxielRr1IaSS9+cJvdQD8g1D4+v2KqtKEwh6gw==",
"roast"=>{"roaster"=>"1", "name"=>"Espress", "countries_attributes"=>{"0"=>{"country_name"=>"UK"}}, "regions"=>{"region_name"=>"Highlands"}, "bestfor"=>"", "roast"=>"", "tastingnotes"=>""},
"commit"=>"Create Roast"}
Can't work this out
ActiveRecord::AssociationTypeMismatch is raised when an association-setter (Roast#roaster= in this case) is called with a value that is not an instance of the expected class. Roaster was expected, got String.
The issue seems to be with passing roaster in as a param, which is "1" (String) in your example. I'm guessing this is actually an ID of a Roaster, the form code in the question does not show it.
Perhaps you meant to permit and pass a roaster_id param?
def roast_params
params.require(:roast).permit(:roaster_id, # ...
end

Plural for fields_for has_many association not showing in view

Currently, an Item belongs_to a Company and has_many ItemVariants.
I'm trying to use nested fields_for to add ItemVariant fields through the Item form, however using :item_variants does not display the form. It is only displayed when I use the singular.
I have check my associations and they seem to be correct, could it possibly have something to do with item being nested under Company, or am I missing something else?
Thanks in advance.
Note: Irrelevant code has been omitted from the snippets below.
EDIT: Don't know if this is relevant, but I'm using CanCan for Authentication.
routes.rb
resources :companies do
resources :items
end
item.rb
class Item < ActiveRecord::Base
attr_accessible :name, :sku, :item_type, :comments, :item_variants_attributes
# Associations
#-----------------------------------------------------------------------
belongs_to :company
belongs_to :item_type
has_many :item_variants
accepts_nested_attributes_for :item_variants, allow_destroy: true
end
item_variant.rb
class ItemVariant < ActiveRecord::Base
attr_accessible :item_id, :location_id
# Associations
#-----------------------------------------------------------------------
belongs_to :item
end
item/new.html.erb
<%= form_for [#company, #item] do |f| %>
...
...
<%= f.fields_for :item_variants do |builder| %>
<fieldset>
<%= builder.label :location_id %>
<%= builder.collection_select :location_id, #company.locations.order(:name), :id, :name, :include_blank => true %>
</fieldset>
<% end %>
...
...
<% end %>
You should prepopulate #item.item_variants with some data:
def new # in the ItemController
...
#item = Item.new
3.times { #item.item_variants.build }
...
end
Source: http://rubysource.com/complex-rails-forms-with-nested-attributes/
try this way
in your item controller new action write
def new
...
#item = # define item here
#item.item_variants.build if #item.item_variants.nil?
...
end
and in item/new.html.erb
<%= form_for #item do |f| %>
...
...
<%= f.fields_for :item_variants do |builder| %>
...
<% end %>
...
...
<% end %>
for more see video - Nested Model Form

Correct way to nest form for join model?

I'm trying to nest a form for my Producttracklisting has_many through join model in my product show view. What is the correct way to do this? I'm getting various errors for my various failed attempts.
The models are as follows:
class Product < ActiveRecord::Base
has_many :producttracklistings
has_many :tracks, :through => :producttracklistings
end
class Track < ActiveRecord::Base
has_many :producttracklistings
has_many :products, :through => :producttracklistings
end
class Producttracklisting < ActiveRecord::Base
belongs_to :product
belongs_to :track
end
The form is as follows:
<%= form_for(#producttracklisting) do |f| %>
<%= f.label :track_id %>
<%= f.text_field :track_id %>
<%= f.label :product_id %>
<%= f.text_field :product_id %>
<%= f.submit %>
<% end %>
And i'm trying to bring this into product/show using:
<%= render 'producttracklistings/form' %>
With all of the above I get a "undefined method `model_name' for NilClass:Class"
Thanks in advance.
What you're looking for is accepts_nested_attributes in combination with fields_for.
See this RailsCast Part 1 and part 2 for a detailed tutorial.
PS:
I'd suggest to name your model ProductTrackListing, which results in a table named product_track_listings. This is far more readable and "the rails way"

Problem with polymorphic association in Rails

I am trying to follow Ryan Bates screencast but have an error message. I did the following:
1) Create table
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.references :commentable, :polymorphic => true
2) Setup models
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
has_many :comments, :as => :commentable
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
has_many :comments, :as => :commentable
3) Change controller show action
class CategoriesController < ApplicationController
def show
#category = Category.find_by_permalink(params[:id])
#commentable = #category
#comment = Comment.new(:commentable => #category)
end
4) Add a form to template views/categories/show.html.erb
<% form_for [#commentable, Comment.new] do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
5) After that I get error message by accessing /categories/my-category-permalink
NoMethodError in Categories#show
undefined method `category_comments_path' for #<ActionView::Base:0x69a9254>
Could you help me to understand what I did wrong?
In the original screencast Ryan accesses comments by /categories/permalink/comments using nested associations, but I don't need that. I want to write comments directly from my polymorphic objects.
Thanks
The problem was in routes settings. I thought that since I don't use nested resources, I can keep routes unchanged. Well, now I know that I was wrong... :) Add this to fix the problem:
map.resources :categories :has_many => :comments
map.resources :products, :has_many => :comments

Resources