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| -%>
<fieldset>
<legend><%=
I18n.t(''tog_social.profiles.member.profile_details'')
%></legend>
<div class="fields">
<!--Nombre del perfil-->
<p>
<label
for="profile_first_name"><%I18n.t(''tog_social.profiles.model.first_name'')
%></label>
<%= f.text_field :first_name, :class=>"fieldbox" %>
</p>
<!--Apellidos del perfil-->
<p>
<label
for="profile_last_name"><%I18n.t(''tog_social.profiles.model.last_name'')
%></label>
<%= f.text_field :last_name, :class=>"fieldbox" %>
</p>
<p>
<label
for="profile_situation"><%I18n.t(''tog_social.profiles.model.situation'')
%> </label>
<select name="profile[situation]" class="fieldbox">
<option value="">
<%=
I18n.t("tog_social.profiles.model.select_situation")
%>
</option>
<option value="Activo">Activo</option>
<option value="Desempleado">Desempleado</option>
<option value="Estudiante">Estudiante</option>
<option value="Jubilado">Jubilado</option>
</select>
</p>
<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'')
%></label>
<%= f.text_field :profession, :class=>"fieldbox" %>
</p>
<!--Lugar del usuario-->
<p>
<label
for="profile_place"><%I18n.t(''tog_social.profiles.model.place'')
%></label>
<select name="profile[place]" class="fieldbox">
<option value="">
<%= I18n.t("tog_social.profiles.model.select_place")
%>
</option>
<option value="Álava">Álava</option>
<option value="Albacete">Albacete</option>
<option value="Alicante">Alicante</option>
<option value="Almería">Almería</option>
<option value="Asturias">Asturias</option>
<option value="Ávila">Ávila</option>
<option value="Badajoz">Badajoz</option>
<option value="Barcelona">Barcelona</option>
<option value="Burgos">Burgos</option>
<option value="Cáceres">Cáceres</option>
<option value="Cádiz">Cádiz</option>
<option value="Cantabria">Cantabria</option>
<option value="Castellón">Castellón</option>
<option value="Ciudad Real">Ciudad Real</option>
<option value="Córdoba">Córdoba</option>
<option value="Cuenca">Cuenca</option>
<option value="Gerona">Gerona</option>
<option value="Granada">Granada</option>
<option value="Guadalajara">Guadalajara</option>
<option value="Guipúzcua">Guipúzcua</option>
<option value="Huelva">Huelva</option>
<option value="Huesca">Huesca</option>
<option value="Islas Baleares">Islas
Baleares</option>
<option value="Jaén">Jaén</option>
<option value="La Coruña">La Coruña</option>
<option value="Las Palmas">Las Palmas</option>
<option value="León">León</option>
<option value="Lérida">Lérida</option>
<option value="Lugo">Lugo</option>
<option value="Madrid">Madrid</option>
<option value="Málaga">Málaga</option>
<option value="Murcia">Murcia</option>
<option value="Navarra">Navarra</option>
<option value="Orense">Orense</option>
<option value="Palencia">Palencia</option>
<option value="Las Palmas">Las Palmas</option>
<option value="Pontevedra">Pontevedra</option>
<option value="La Rioja">La Rioja</option>
<option value="Salamanca">Salamanca</option>
<option value="Segovia">Segovia</option>
<option value="Sevilla">Sevilla</option>
<option value="Soria">Soria</option>
<option value="Tarragona">Tarragona</option>
<option value="Santa Cruz de Tenerife">Santa Cruz de
Tenerife</
option>
<option value="Teruel">Teruel</option>
<option value="Toledo">Toledo</option>
<option value="Valencia">Valencia</option>
<option value="Valladolid">Valladolid</option>
<option value="Vizcaya">Vizcaya</option>
<option value="Zamora">Zamora</option>
<option value="Zaragoza">Zaragoza</option>
</select>
</p>
<!--email del Usuario-->
<p>
<label
for="profile_contact_email"><%I18n.t(''tog_social.profiles.model.contact_email'')
%></label>
<%= f.text_field :contact_email, :class=>"fieldbox
wide" %>
</p>
<!--tlf del Usuario-->
<p>
<label
for="profile_tlf"><%I18n.t(''tog_social.profiles.model.tlf'')
%></label>
<%= f.text_field :tlf, :class=>"fieldbox wide" %>
</p>
<!--Web del usuario-->
<p>
<label
for="profile_website"><%I18n.t(''tog_social.profiles.model.website'')
%></label>
<%= f.text_field :website, :class=>"fieldbox wide"
%>
</p>
<!--CV del usuario-->
<p>
<label
for="profile_pdf"><%I18n.t(''tog_social.profiles.model.curriculum'')
%></label>
<%= file_field "profile", "pdf_file_name"
%><br />
<span
class="helpinfo"><%I18n.t(''tog_social.profiles.model.curriculum_help'')
%></span>
</p>
<!--Imagen del usuario-->
<p>
<label
for="profile_icon"><%I18n.t(''tog_social.profiles.model.avatar'')
%></label>
<%= icon_for_profile(@profile, ''big'')%>
<%= file_field "profile", "icon" %><br
/>
<span
class="helpinfo"><%I18n.t(''tog_social.profiles.model.avatar_help'')
%></span>
</p>
</div>
</fieldset>
<div class="actions">
<%= submit_tag
I18n.t(''tog_social.profiles.member.update_profile''),
{:accesskey =>
"s", :class=>"button mainaction"} %>
</div>
<% end -%><!-- /.formulario del usuario -->
<!--Ahora se trata de un usuario que tiene pyme(es_pyme ==
''true''),
en este caso para actualizar el perfil mostraríamos:
- Nombre de la empresa
- Localidad
- Direccion
- Actividad de la empresa
- Página Web
- Catálogo de productos si desea
- y una imagen para el perfil de la empresa.
-->
<!--Mostramos la posibilidad de añadir responsables de la empresa-->
<hr class="wide" />
<% end -%>
<div
class="titleB"><%I18n.t(''tog_social.profiles.member.add_responsibles'')
%></div>
<% form_for :responsible, :url =>
member_profile_path(@profile), :html => {:method
=> :put, :multipart=>true} do |f| -%>
<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%>>
<p><label
for="profile.responsibles_responsible_name"><
%= I18n.t("tog_social.profiles.model.responsible_name")
%></label> <%text_field "responsible[#{i}]", {:class
=> "fieldbox", :size =>
"40"} %></p>
<p><label
for="profile.responsibles_responsible_mail"><
%= I18n.t("tog_social.profiles.model.responsible_email")
%></label> <
%= text_field "responsible[#{i}]", {:class => "fieldbox",
:size =>
"40"} %></p>
<p><label
for="profile.responsibles_responsible_tlf"><
%= I18n.t("tog_social.profiles.model.responsible_tlf")
%></label> <%text_field "responsible[#{i}]", {:class
=> "fieldbox", :size =>
"40"} %></p>
</fieldset>
</div>
<%if i < 4 %>
<div id="link_fs_responsible_<%=i%>" <%if
i>0%>style="display:none"<%end%>>
<a href="javascript:new_responsible(<%=i%>,
<%=i+1%>)"
class="add"><%I18n.t("tog_social.profiles.member.add_one_more_responsible")
%></a>
</div>
<%end%>
<% end -%>
</div>
</fieldset>
<br />
<div class="actions">
<%= submit_tag
I18n.t(''tog_social.profiles.member.update_responsibles''),
{:accesskey
=> "s", :class=>"button mainaction"} %>
</div>
<% end -%>
and i want to know how can i add the second form about responsibles?
thanks
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
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| -%>
<fieldset>
<legend><%I18n.t(''tog_social.profiles.member.profile_details'')
%></legend>
<div class="fields">
<!--Nombre de la empresa-->
<p>
<label
for="profile_first_name"><%I18n.t(''tog_social.profiles.model.pyme_name'')
%></label>
<%= f.text_field :first_name, :class=>"fieldbox
wide" %>
</p>
<!--Tipo de empresa-->
<p>
<label
for="profile_type_of_pyme"><%I18n.t(''tog_social.profiles.model.type_of_pyme'')
%></label>
<select name="profile[type_of_pyme]"
class="fieldbox">
<option value="">
<%I18n.t("tog_social.profiles.model.select_type_of_pyme") %>
</option>
<option
value="Micro-empresa">Micro-empresa</option>
<option
value="Pequeña-empresa">Pequeña-empresa</option>
<option
value="Mediana-empresa">Mediana-empresa</option>
<option
value="Gran-empresa">Gran-empresa</option>
</select>
</p>
<p>
<label
for="profile_direction"><%I18n.t(''tog_social.profiles.model.pyme_direction'')
%></label>
<%= f.text_field :direction, :class=>"fieldbox wide"
%>
</p>
<!--email de la Pyme-->
<p>
<label
for="profile_contact_email"><%I18n.t(''tog_social.profiles.model.contact_email'')
%></label>
<%= f.text_field :contact_email, :class=>"fieldbox
wide"
%>
</p>
<!--tlf de la Pyme-->
<p>
<label
for="profile_tlf"><%I18n.t(''tog_social.profiles.model.tlf'')
%></label>
<%= f.text_field :tlf, :class=>"fieldbox wide" %>
</p>
<!--fax de la Pyme-->
<p>
<label
for="profile_fax"><%I18n.t(''tog_social.profiles.model.fax'')
%></label>
<%= f.text_field :fax, :class=>"fieldbox wide" %>
</p>
<!--Web de la Pyme-->
<p>
<label
for="profile_website"><%I18n.t(''tog_social.profiles.model.website'')
%></label>
<%= f.text_field :website, :class=>"fieldbox wide"
%>
</p>
<!--Catálogo de productos de la Pyme-->
<p>
<label
for="profile_pdf"><%I18n.t(''tog_social.profiles.model.catalogue'')
%></label>
<%= file_field "profile", "pdf_file_name"
%><br />
<span
class="helpinfo"><%I18n.t(''tog_social.profiles.model.catalogue_help'')
%></span>
</p>
<!--Imagen de la Pyme -->
</br>
<p>
<label
for="profile_icon"><%I18n.t(''tog_social.profiles.model.avatar'')
%></label>
<%= icon_for_profile(@profile, ''big'')%>
<%= file_field "profile", "icon" %><br
/>
<span
class="helpinfo"><%I18n.t(''tog_social.profiles.model.avatar_help'')
%></span>
</p>
</div>
</fieldset>
</br>
<!--Mostramos la posibilidad de añadir responsables de la
empresa-->
<hr class="wide" />
<div
class="titleB"><%I18n.t(''tog_social.profiles.member.add_responsibles'')
%></div>
<% 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%>>
<p><label
for="responsible_name"><%I18n.t("tog_social.profiles.model.responsible_name")
%></label> <%text_field :responsible_name,
"responsible[#{i}]", {:class =>
"fieldbox", :size => "40"} %></p>
<p><label
for="responsible_email"><%I18n.t("tog_social.profiles.model.responsible_email")
%></label> <%text_field :responsible_email,
"responsible[#{i}]", {:class =>
"fieldbox", :size => "40"} %></p>
<p><label
for="responsible_tlf"><%I18n.t("tog_social.profiles.model.responsible_tlf")
%></label> <
%=text_field :responsible_tlf, "responsible[#{i}]", {:class =>
"fieldbox", :size => "40"} %></p>
</fieldset>
</div>
<%if i < 4 %>
<div id="link_fs_responsible_<%=i%>"
<%if
i>0%>style="display:none"<%end%>>
<a
href="javascript:new_responsible(<%=i%>, <
%=i+1%>)"
class="add"><%I18n.t("tog_social.profiles.member.add_one_more_responsible")
%></a>
</div>
<%end%>
<% end -%>
</div>
</fieldset>
<br />
<div class="actions">
<%= submit_tag
I18n.t(''tog_social.profiles.member.update_profile''),
{:accesskey =>
"s", :class=>"button mainaction"} %>
</div>
<% end -%>
<% end -%><!-- /.formulario de la Pyme -->
Thanks sorry for so many inconveniences.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 13 oct, 11:11, h0bit <jsantana....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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| -%> > <fieldset> > <legend><%> I18n.t(''tog_social.profiles.member.profile_details'') %></legend> > <div class="fields"> > <!--Nombre de la empresa--> > <p> > <label for="profile_first_name"><%> I18n.t(''tog_social.profiles.model.pyme_name'') %></label> > <%= f.text_field :first_name, :class=>"fieldbox wide" %> > </p> > > <!--Tipo de empresa--> > <p> > <label for="profile_type_of_pyme"><%> I18n.t(''tog_social.profiles.model.type_of_pyme'') %></label> > <select name="profile[type_of_pyme]" class="fieldbox"> > <option value=""> > <%> I18n.t("tog_social.profiles.model.select_type_of_pyme") %> > </option> > <option value="Micro-empresa">Micro-empresa</option> > <option value="Pequeña-empresa">Pequeña-empresa</option> > <option value="Mediana-empresa">Mediana-empresa</option> > <option value="Gran-empresa">Gran-empresa</option> > </select> > </p> > > <p> > <label for="profile_direction"><%> I18n.t(''tog_social.profiles.model.pyme_direction'') %></label> > <%= f.text_field :direction, :class=>"fieldbox wide" %> > </p> > > <!--email de la Pyme--> > <p> > <label for="profile_contact_email"><%> I18n.t(''tog_social.profiles.model.contact_email'') %></label> > <%= f.text_field :contact_email, :class=>"fieldbox wide" > %> > </p> > > <!--tlf de la Pyme--> > <p> > <label for="profile_tlf"><%> I18n.t(''tog_social.profiles.model.tlf'') %></label> > <%= f.text_field :tlf, :class=>"fieldbox wide" %> > </p> > > <!--fax de la Pyme--> > <p> > <label for="profile_fax"><%> I18n.t(''tog_social.profiles.model.fax'') %></label> > <%= f.text_field :fax, :class=>"fieldbox wide" %> > </p> > > <!--Web de la Pyme--> > <p> > <label for="profile_website"><%> I18n.t(''tog_social.profiles.model.website'') %></label> > <%= f.text_field :website, :class=>"fieldbox wide" %> > </p> > > <!--Catálogo de productos de la Pyme--> > <p> > <label for="profile_pdf"><%> I18n.t(''tog_social.profiles.model.catalogue'') %></label> > <%= file_field "profile", "pdf_file_name" %><br /> > <span class="helpinfo"><%> I18n.t(''tog_social.profiles.model.catalogue_help'') %></span> > </p> > > <!--Imagen de la Pyme --> > </br> > <p> > <label for="profile_icon"><%> I18n.t(''tog_social.profiles.model.avatar'') %></label> > <%= icon_for_profile(@profile, ''big'')%> > <%= file_field "profile", "icon" %><br /> > <span class="helpinfo"><%> I18n.t(''tog_social.profiles.model.avatar_help'') %></span> > </p> > </div> > </fieldset> > > </br> > > <!--Mostramos la posibilidad de añadir responsables de la empresa-- > > <hr class="wide" /> > > <div class="titleB"><%> I18n.t(''tog_social.profiles.member.add_responsibles'') %></div> > > <% 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%>> > <p><label for="responsible_name"><%> I18n.t("tog_social.profiles.model.responsible_name") %></label> <%> text_field :responsible_name, "responsible[#{i}]", {:class => > "fieldbox", :size => "40"} %></p> > <p><label for="responsible_email"><%> I18n.t("tog_social.profiles.model.responsible_email") %></label> <%> text_field :responsible_email, "responsible[#{i}]", {:class => > "fieldbox", :size => "40"} %></p> > <p><label for="responsible_tlf"><%> I18n.t("tog_social.profiles.model.responsible_tlf") %></label> < > %=text_field :responsible_tlf, "responsible[#{i}]", {:class => > "fieldbox", :size => "40"} %></p> > </fieldset> > </div> > <%if i < 4 %> > <div id="link_fs_responsible_<%=i%>" <%if > i>0%>style="display:none"<%end%>> > <a href="javascript:new_responsible(<%=i%>, < > %=i+1%>)" class="add"><%> I18n.t("tog_social.profiles.member.add_one_more_responsible") %></a> > </div> > <%end%> > <% end -%> > > </div> > </fieldset> > > <br /> > <div class="actions"> > <%= submit_tag > I18n.t(''tog_social.profiles.member.update_profile''), {:accesskey => > "s", :class=>"button mainaction"} %> > </div> > > <% end -%> > > <% end -%><!-- /.formulario de la Pyme --> > > Thanks sorry for so many inconveniences.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.