Hi There, I''m trying to user fields_for inside a form_tag, but i can''t catch it on my controller. I just take some html code off to makes it easy to read my view... i''m using campuses_path(current_church, @campus) instead form_for @campus, because my route is like resources :campuses, :path => ":church/campuses" <%= form_tag campuses_path(current_church, @campus), :class =>> ''form-horizontal'', :method => :post do %> > <%= text_field :campus, :name %> > <%= text_area :campus, :description %> > > <%= fields_for :addresses do |b| %> > <%= b.text_field :street %> > <%= b.text_field :street2 %> > <%= b.text_field :city %> > <%= b.text_field :state %> > <%= b.text_field :zip_code %> > <% end %> > > <input type=''submit'' value=''Save'' > > <% end %>my controller> class CampusesController < ApplicationController > def new > @campus = Campus.new > end > > def create > @campus = Campus.new(campus_params) > @campus.church_id = session[:church_id] > # i''m just trying this way, because it''s not creating automatic > (accepts_nested_attributes_for) > * @campus.addresses.new(params[:addresses]) # the error line* > > if @campus.save! > ... > else > ... > end > > private > > # Never trust parameters from the scary internet, only allow the white > list through. > def campus_params > params.require(:campus).permit(:name, :description, :cover, > :addesses) > # i''ve tried instead :addresses > # {:addresses => []} > # :addresses [] > end > > endmy model class Campus < ActiveRecord::Base> belongs_to :church > has_many :addresses, :as => :addressable > > > accepts_nested_attributes_for :addresses > > endRequest parameters {"utf8"=>"✓", "authenticity_token"=>"B0OBKKM3PQ/Jkh1ebcKyrKZGJuauJeN3O/BWv8Vc7qE=", "campus"=>{"name"=>"Campus name", "description"=>"campus description"}, "addresses"=>{"street"=>"Street name", "street2"=>"", "city"=>"city name", "state"=>"state name", "zip_code"=>"00000"}, "action"=>"create", "controller"=>"campuses", "church"=>"greenhouse-church"} Params at Controller: params[:addresses] => {"street"=>"Street name", "street2"=>"", "city"=>"city name", "state"=>"state name", "zip_code"=>"00000"}>> params[:campus]=> {"name"=>"Campus name", "description"=>"campus description"}>>With this code the error is: ActiveModel::ForbiddenAttributesError @campus.addresses.new(params[:addresses]) Let me know if you need more information. Thank You, André -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f837fd60-7bcd-49dd-afb2-bd597d02330d%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Have you tried with form_for instead of form_tag? On Tuesday, December 10, 2013 7:45:15 PM UTC-2, Andre Mello wrote:> > Hi There, > I''m trying to user fields_for inside a form_tag, but i can''t catch it > on my controller. > > I just take some html code off to makes it easy to read > > my view... > > i''m using campuses_path(current_church, @campus) instead form_for > @campus, because my route is like > resources :campuses, :path => ":church/campuses" > > <%= form_tag campuses_path(current_church, @campus), :class => >> ''form-horizontal'', :method => :post do %> >> <%= text_field :campus, :name %> >> <%= text_area :campus, :description %> >> >> <%= fields_for :addresses do |b| %> >> <%= b.text_field :street %> >> <%= b.text_field :street2 %> >> <%= b.text_field :city %> >> <%= b.text_field :state %> >> <%= b.text_field :zip_code %> >> <% end %> >> >> <input type=''submit'' value=''Save'' > >> <% end %> > > > my controller > > >> class CampusesController < ApplicationController >> def new >> @campus = Campus.new >> end >> >> def create >> @campus = Campus.new(campus_params) >> @campus.church_id = session[:church_id] >> # i''m just trying this way, because it''s not creating automatic >> (accepts_nested_attributes_for) >> * @campus.addresses.new(params[:addresses]) # the error line* >> >> if @campus.save! >> ... >> else >> ... >> end >> >> private >> >> # Never trust parameters from the scary internet, only allow the white >> list through. >> def campus_params >> params.require(:campus).permit(:name, :description, :cover, >> :addesses) >> # i''ve tried instead :addresses >> # {:addresses => []} >> # :addresses [] >> end >> >> end > > > my model > > class Campus < ActiveRecord::Base >> belongs_to :church >> has_many :addresses, :as => :addressable >> >> >> accepts_nested_attributes_for :addresses >> >> end > > > > Request parameters > > {"utf8"=>"✓", "authenticity_token"=>"B0OBKKM3PQ/Jkh1ebcKyrKZGJuauJeN3O/BWv8Vc7qE=", "campus"=>{"name"=>"Campus name", "description"=>"campus description"}, "addresses"=>{"street"=>"Street name", "street2"=>"", "city"=>"city name", "state"=>"state name", "zip_code"=>"00000"}, "action"=>"create", "controller"=>"campuses", "church"=>"greenhouse-church"} > > > Params at Controller: > > params[:addresses] > => {"street"=>"Street name", "street2"=>"", "city"=>"city name", "state"=>"state name", "zip_code"=>"00000"} > >> params[:campus] > => {"name"=>"Campus name", "description"=>"campus description"} > > >> > > > > With this code the error is: > > ActiveModel::ForbiddenAttributesError > > @campus.addresses.new(params[:addresses]) > > > > Let me know if you need more information. > > Thank You, > > André > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3a8e8ffc-ba3d-44d1-b3e0-ad7c6125d2a6%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
On Tuesday, December 10, 2013 9:45:15 PM UTC, Andre Mello wrote:> > def create >> @campus = Campus.new(campus_params) >> @campus.church_id = session[:church_id] >> # i''m just trying this way, because it''s not creating automatic >> (accepts_nested_attributes_for) >> * @campus.addresses.new(params[:addresses]) # the error line* >> >>The parameters being submitted aren''t the same as if you had done <%= form_for @campus do |form|%> <%= form.fields_for(:addresses) do |addresses_form| %> ... <% end %> <% end %> For one, addresses is being submitted at the top level, not nested. params[:campus][:addresses] would be a hash of hashes (one hash for each address). If you''re dead set on use form_tag, <%= fields_for @campus do |form|%> <%= form.fields_for(:addresses) do |addresses_form| %> ... <% end %> <% end %> should do it> Params at Controller: > > params[:addresses] > => {"street"=>"Street name", "street2"=>"", "city"=>"city name", "state"=>"state name", "zip_code"=>"00000"} > >> params[:campus] > => {"name"=>"Campus name", "description"=>"campus description"} > > >> > > Because params[:addresses] is a hash, you need to permit the individualkeys in it (much as you have to permit the individual campus keys) Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/029b8cb5-79d5-43ae-b57f-fb5ff5b71328%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.