Problem with has_many

Hello everybody i’m new working with rails.

I want to creata new relation in my app but it fail. This is the code:

model profile.rb

class Profile < ActiveRecord::Base

has_many :responsibles

end

model reponsible.rb
class Responsible < ActiveRecord::Base

belongs_to :profile, :class_name => ‘Profile’, :foreign_key =>
‘profile_id’

end

in the controller responsibles_controller.rb

class Member::ResponsiblesController < Member::BaseController

def create
@responsibles =
current_user.responsibles.build(params[:responsible].values)
@responsible.save!

flash[:ok] = I18n.t("tog_picto.member.responsibles_created")
redirect_to profile_path(profile)

end

def update
responsible.update_attributes!(params[:responsibles])
responsible.save
flash[:ok] =
I18n.t(“tog_social.profiles.member.responsibles_created”)
redirect_to profile_path(profile)
end

end

and in the view to edit the profile i want to add if it’s possible
update the responsibles:

<%= error_messages_for :profile %>

<% form_for :profile, :url => member_profile_path(@profile), :html =>
{:method => :put, :multipart=>true} do |f| -%>

<%= I18n.t(‘tog_social.profiles.member.profile_details’)
%>




<%=
I18n.t(‘tog_social.profiles.model.first_name’) %>
<%= f.text_field :first_name, :class=>“fieldbox” %>




<%=
I18n.t(‘tog_social.profiles.model.last_name’) %>
<%= f.text_field :last_name, :class=>“fieldbox” %>

  <p>
    <label for="profile_situation"><%=

I18n.t(‘tog_social.profiles.model.situation’) %>


<%= I18n.t(“tog_social.profiles.model.select_situation”)
%>

Activo
Desempleado
Estudiante
Jubilado

  <p>
    <%= check_box_tag 'profile_demandante_de_empleo'%>
  <label for="profile_demandante_de_empleo">
    <%= I18n.t('tog_social.profiles.model.demandante_de_empleo') %>

  </label>
  </p>

   <p>
    <label for="profile_profession"><%=

I18n.t(‘tog_social.profiles.model.profession’) %>
<%= f.text_field :profession, :class=>“fieldbox” %>

  <!--Lugar del usuario-->
  <p>
    <label for="profile_place"><%=

I18n.t(‘tog_social.profiles.model.place’) %>


<%= I18n.t(“tog_social.profiles.model.select_place”) %>

lava
Albacete
Alicante
Almera
Asturias
vila
Badajoz
Barcelona
Burgos
Cceres
Cdiz
Cantabria
Castelln
Ciudad Real
Crdoba
Cuenca
Gerona
Granada
Guadalajara
Guipzcua
Huelva
Huesca
Islas Baleares
Jan
La Corua
Las Palmas
Len
Lrida
Lugo
Madrid
Mlaga
Murcia
Navarra
Orense
Palencia
Las Palmas
Pontevedra
La Rioja
Salamanca
Segovia
Sevilla
Soria
Tarragona
Santa Cruz de Tenerife</
option>
Teruel
Toledo
Valencia
Valladolid
Vizcaya
Zamora
Zaragoza

  <!--email del Usuario-->
    <p>
      <label for="profile_contact_email"><%=

I18n.t(‘tog_social.profiles.model.contact_email’) %>
<%= f.text_field :contact_email, :class=>“fieldbox wide” %>

  <!--tlf del Usuario-->
    <p>
      <label for="profile_tlf"><%=

I18n.t(‘tog_social.profiles.model.tlf’) %>
<%= f.text_field :tlf, :class=>“fieldbox wide” %>

  <!--Web del usuario-->
    <p>
      <label for="profile_website"><%=

I18n.t(‘tog_social.profiles.model.website’) %>
<%= f.text_field :website, :class=>“fieldbox wide” %>

  <!--CV del usuario-->
  <p>
      <label for="profile_pdf"><%=

I18n.t(‘tog_social.profiles.model.curriculum’) %>
<%= file_field “profile”, “pdf_file_name” %>

<%=
I18n.t(‘tog_social.profiles.model.curriculum_help’) %>

  <!--Imagen del usuario-->
    <p>
      <label for="profile_icon"><%=

I18n.t(‘tog_social.profiles.model.avatar’) %>
<%= icon_for_profile(@profile, ‘big’)%>
<%= file_field “profile”, “icon” %>

<%=
I18n.t(‘tog_social.profiles.model.avatar_help’) %>



<div class="actions">
    <%= submit_tag

I18n.t(‘tog_social.profiles.member.update_profile’), {:accesskey =>
“s”, :class=>“button mainaction”} %>

<% end -%>

<!--Mostramos la posibilidad de aadir responsables de la empresa-->
<hr class="wide" />

<% end -%>

<%= I18n.t('tog_social.profiles.member.add_responsibles') %>
<% form_for :responsible, :url =>

member_profile_path(@profile), :html => {:method
=> :put, :multipart=>true} do |f| -%>

         <script type="text/javascript">
      function new_responsible(i, n) {
        $('fs_responsible_' + n).toggle();
        $('link_fs_photo_' + i).toggle();
        $('link_fs_photo_' + n).toggle();
      }
    </script>


        <% 5.times do |i| -%>
      <div class="fields">
            <fieldset id="fs_responsible_<%=i%>" <%if

i>0%>style=“display:none”<%end%>>

<
%= I18n.t(“tog_social.profiles.model.responsible_name”) %> <%=
text_field “responsible[#{i}]”, {:class => “fieldbox”, :size =>
“40”} %>


<
%= I18n.t(“tog_social.profiles.model.responsible_email”) %> <
%= text_field “responsible[#{i}]”, {:class => “fieldbox”, :size =>
“40”} %>


<
%= I18n.t(“tog_social.profiles.model.responsible_tlf”) %> <%=
text_field “responsible[#{i}]”, {:class => “fieldbox”, :size =>
“40”} %>




<%if i < 4 %>
<div id=“link_fs_responsible_<%=i%>” <%if
i>0%>style=“display:none”<%end%>>
<%=
I18n.t(“tog_social.profiles.member.add_one_more_responsible”) %>


<%end%>
<% end -%>
    </div>
  </fieldset>

  <br />
<div class="actions">
      <%= submit_tag

I18n.t(‘tog_social.profiles.member.update_responsibles’), {:accesskey
=> “s”, :class=>“button mainaction”} %>

<% end -%>

and i want to know how can i add the second form about responsibles?

thanks

What you want to use is :accept_nested_attributes in your models

class Profile < ActiveRecord::Base

has_many :responsibles
accepts_nested_attributes :responsibles

end

And then you’ll want to do something like this in your view

<% form_for @profile do |profile_form| %>

<%= profile_form.label :name %>
<%= profile_form.text_field :name %>

<% profile_form.fields_for :responsibles do |child_form| %>

<%= child_form.label :name %>
<%= child_form.text_field :name %>

<% unless child_form.object.new_record? %>
  <%= child_form.check_box '_delete' %>
  <%= child_form.label '_delete', 'Remove' %>
<% end %>

<% end %>

<%= submit_tag %>
<% end %>

That’s all

In order to use this, you have to ensure you’re using rails 2 but I’m
sure you are.

Here’s more info
http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

Thanks Luis

I did what you said me, but when I submit the form I see that the
field send well, but in the table of responsibles don’t appear the
responsibles, Could you tell me why? Is necessary to do something in
the profiles_controller?

I have the next code for your help:

<% form_for :profile, :url => member_profile_path(@profile), :html =>
{:method => :put, :multipart=>true} do |f| -%>

<%=
I18n.t(‘tog_social.profiles.member.profile_details’) %>




<%=
I18n.t(‘tog_social.profiles.model.pyme_name’) %>
<%= f.text_field :first_name, :class=>“fieldbox wide” %>

      <!--Tipo de empresa-->
      <p>
          <label for="profile_type_of_pyme"><%=

I18n.t(‘tog_social.profiles.model.type_of_pyme’) %>


<%=
I18n.t(“tog_social.profiles.model.select_type_of_pyme”) %>

Micro-empresa
Pequea-empresa
Mediana-empresa
Gran-empresa

      <p>
        <label for="profile_direction"><%=

I18n.t(‘tog_social.profiles.model.pyme_direction’) %>
<%= f.text_field :direction, :class=>“fieldbox wide” %>

      <!--email de la Pyme-->
      <p>
        <label for="profile_contact_email"><%=

I18n.t(‘tog_social.profiles.model.contact_email’) %>
<%= f.text_field :contact_email, :class=>“fieldbox wide”
%>

      <!--tlf de la Pyme-->
      <p>
        <label for="profile_tlf"><%=

I18n.t(‘tog_social.profiles.model.tlf’) %>
<%= f.text_field :tlf, :class=>“fieldbox wide” %>

      <!--fax de la Pyme-->
      <p>
        <label for="profile_fax"><%=

I18n.t(‘tog_social.profiles.model.fax’) %>
<%= f.text_field :fax, :class=>“fieldbox wide” %>

      <!--Web de la Pyme-->
      <p>
        <label for="profile_website"><%=

I18n.t(‘tog_social.profiles.model.website’) %>
<%= f.text_field :website, :class=>“fieldbox wide” %>

      <!--Catlogo de productos de la Pyme-->
      <p>
        <label for="profile_pdf"><%=

I18n.t(‘tog_social.profiles.model.catalogue’) %>
<%= file_field “profile”, “pdf_file_name” %>

<%=
I18n.t(‘tog_social.profiles.model.catalogue_help’) %>

      <!--Imagen de la Pyme -->
      </br>
      <p>
        <label for="profile_icon"><%=

I18n.t(‘tog_social.profiles.model.avatar’) %>
<%= icon_for_profile(@profile, ‘big’)%>
<%= file_field “profile”, “icon” %>

<%=
I18n.t(‘tog_social.profiles.model.avatar_help’) %>



  </br>

<!--Mostramos la posibilidad de aadir responsables de la empresa--
<hr class="wide" />

<div class="titleB"><%=

I18n.t(‘tog_social.profiles.member.add_responsibles’) %>

  <% f.fields_for :responsibles do |responsible_form| %>
    <fieldset>
      <div class="fields">

           <script type="text/javascript">
            function new_responsible(i, n) {
              $('fs_responsible_' + n).toggle();
              $('link_fs_photo_' + i).toggle();
              $('link_fs_photo_' + n).toggle();
            }
          </script>

          <% 5.times do |i| -%>
              <div class="fields">
              <fieldset id="fs_responsible_<%=i%>" <%if

i>0%>style=“display:none”<%end%>>

<%=
I18n.t(“tog_social.profiles.model.responsible_name”) %> <%=
text_field :responsible_name, “responsible[#{i}]”, {:class =>
“fieldbox”, :size => “40”} %>


<%=
I18n.t(“tog_social.profiles.model.responsible_email”) %> <%=
text_field :responsible_email, “responsible[#{i}]”, {:class =>
“fieldbox”, :size => “40”} %>


<%=
I18n.t(“tog_social.profiles.model.responsible_tlf”) %> <
%=text_field :responsible_tlf, “responsible[#{i}]”, {:class =>
“fieldbox”, :size => “40”} %>




<%if i < 4 %>
<div id=“link_fs_responsible_<%=i%>” <%if
i>0%>style=“display:none”<%end%>>
<%=
I18n.t(“tog_social.profiles.member.add_one_more_responsible”) %>


<%end%>
<% end -%>
      </div>
      </fieldset>

      <br />
    <div class="actions">
      <%= submit_tag

I18n.t(‘tog_social.profiles.member.update_profile’), {:accesskey =>
“s”, :class=>“button mainaction”} %>

  <% end -%>

<% end -%><!-- /.formulario de la Pyme -->

Thanks sorry for so many inconveniences.

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