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

<% end %>

<% end %>

my controller

  • @campus.addresses.new(params[:addresses]) # the error line*
    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?

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

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs