Do not use hidden_field fields in nested forms

Asked

Viewed 138 times

1

I wonder if there’s a better way to work with nested attributes without having to send the id of the parent object through the field hidden_field, I find it unsafe to leave it to the page.

Suggestions? Follow the code used:

<%= form_for(@responsible, :url => (@responsible.new_record? ? enterprise_responsibles_path(@responsible.enterprise) : enterprise_responsible_path(@responsible.enterprise, @responsible))) do |f| %>
<% if @responsible.errors.any? %>
<div id="error_explanation">
    <h2><%= pluralize(@responsible.errors.count, "error") %> prohibited this responsible from being saved:</h2>
    <ul>
        <% @responsible.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
        <% end %>
    </ul>
</div>
<% end %>

<%= f.hidden_field :enterprise_id %> #ID DO OBJETO PAI ENVIADO VIA HIDDEN**
<div class="form-group">
    <%= f.label :name %>
    <%= f.text_field :name, :class => "form-control" %>
</div>
<div class="form-group">
    <%= f.label :cpf %>
    <%= f.text_field :cpf, :class => "form-control" %>
</div>

<div class="form-group">
    <%= f.label :occupation %>
    <%= f.text_field :occupation, :class => "form-control" %>
</div>

<%= f.check_box :active %>&nbsp;<%= f.label :active %>

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">Contacts
            <span class="btn-group btn-group-xs pull-right"> 
                <%= link_to_add_fields content_tag(:i, "", class:"glyphicon glyphicon-plus") + " add contact", f, :contacts, "btn btn-xs btn-success pull-right" %>
            </span>
        </h3>
    </div>
    <div class="panel-body">
        <%= f.fields_for :contacts do |builder| %>
        <%= render 'contact_fields', f: builder %>
        <% end %>
    </div>
</div>

<div class="actions">
    <%= f.submit class: "btn btn-sm btn-primary" %>
</div>
<% end %>

Routes.Rb

resources :enterprises do
    resources :responsibles do
      resources :contacts  
    end
    resources :proposals
end
  • nested_form would be this Gem? https://github.com/ryanb/nested_form

  • No. no Gem but programming me expressed wrong with nested Attributes

  • If you are using nested routes and the helpers generated from the urls (for example, user_cars_url(@user)), no Hidden field is required. If you show the code we can help more, if not, it becomes difficult...

  • I updated the question with the source, this code is working 100% only I think it is unsafe to pass this reference via field Hidden q can be manipulated

  • What happens if you change @responsible for [@entreprise, @responsible] as the first parameter of form_for? It seems to me you have another route resources :responsibles outside the enterprises, nay?

  • @fotanus worked like this the way you said I removed Hidden and put the attributes like this, now I remembered that if I have objects below the form_for can navigate the structure and find! Thank you!

Show 1 more comment

1 answer

1


So, formally responding to,

Change

<%= form_for(@responsible, :url => (@responsible.new_record? ? enterprise_responsibles_path(@responsible.enterprise) : enterprise_responsible_path(@responsible.enterprise, @responsible))) do |f| %>

For

<%= form_for([@enterprise, @responsible], :url => (@responsible.new_record? ? enterprise_responsibles_path(@responsible.enterprise) : enterprise_responsible_path(@responsible.enterprise, @responsible))) do |f| %>

and remove the hidden field.

  • Thank you! That’s just what I needed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.