Forum: Ruby on Rails form_tag + fields_for Rails 4

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ecb7b45ecd680695973b1804582fe682?d=identicon&s=25 Andre Mello (Guest)
on 2013-12-11 00:02
(Received via mailing list)
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 %>
>
>   <input type='submit' value='Save' >
> <% 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é
934391210ccaf36878b36eff13061f67?d=identicon&s=25 Guilherme Dallanol (Guest)
on 2013-12-12 12:27
(Received via mailing list)
Have you tried with form_for instead of form_tag?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2013-12-12 15:51
(Received via mailing list)
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 topic is locked and can not be replied to.