form_tag + fields_for Rails 4

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 %>

<% 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é

Have you tried with form_for instead of form_tag?

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 individual keys in it (much as you have to permit the individual campus keys)

Fred