nested_form gem rails 4在click link_to_add上显示两个嵌套的表单(字段)

时间:2014-11-14 14:01:25

标签: ruby-on-rails

点击"添加项目",添加两个' fields_for:item'嵌套表格(字段)

需要帮助:)))

Assosiation:

菜单(has_many:items), 项目(belongs_to:菜单)

<%= nested_form_for(@menu) do |f| %>
  <div class="form-group">
    <%= f.label :title %><br>
    <%= f.text_field :title, :class=>"form-control", :placeholder => "Title" %>
  </div>

  <div class="form-group">
    <%= f.label :description %><br>
    <%= f.text_area :description, :class=>"form-control", :placeholder => "Description" %>
  </div>

  <h3>Items</h3>
   <%= f.fields_for :items do |i| %>

    <div class="form-group">
     <%= i.label :name %><br>     
     <%= i.text_field :name, :class=>"form-control", :placeholder => "Name" %>
    </div>

    <div class="form-group">
     <%= i.label :price %><br>
     <%= i.text_field :price, :class=>"form-control", :placeholder => "Price" %>
    </div>

    <div class="form-group">
     <%= i.label :description %><br>
     <%= i.text_area :description, :class=>"form-control", :placeholder => "Description" %>
    </div>

    <%= i.link_to_remove "Remove this item", :class=>"btn btn-default" %>
    <% end %>

    <p><%= f.link_to_add "Add an Item", :items, :class=>"btn btn-default" %></p>

  <%= f.hidden_field :restaurant_id, :value => params[:restauranti_id]`enter code here`

  <div class="actions">
    <%= f.submit 'Create Menu', :class=>"btn btn-default" %>
  </div>
<% end %>

添加两次:

<%= f.fields_for :items do |i| %>

    <div class="form-group">
     <%= i.label :name %><br>     
     <%= i.text_field :name, :class=>"form-control", :placeholder => "Name" %>
    </div>

    <div class="form-group">
     <%= i.label :price %><br>
     <%= i.text_field :price, :class=>"form-control", :placeholder => "Price" %>
    </div>

    <div class="form-group">
     <%= i.label :description %><br>
     <%= i.text_area :description, :class=>"form-control", :placeholder => "Description" %>
    </div>

    <%= i.link_to_remove "Remove this item", :class=>"btn btn-default" %>
    <% end %>

我目前的版本是&#39; rails&#39;,&#39; 4.1.6&#39; ... 帮帮我:))))

2 个答案:

答案 0 :(得分:1)

删除&lt;%= javascript_include_tag:默认值,“nested_form”%&gt;在你的布局中(:

答案 1 :(得分:0)

我遇到了同样的问题。 Mi解决方案是下一个:

app/views/layouts/application.html.erb文件中,我有下一个代码

<!DOCTYPE html>
<html>
<head>
  <title>RoR 1</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

  <%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css", "application" %>
  <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", "application" %>
  <%= csrf_meta_tags %>

</head>
<body>
  <div class="container">
    <%= yield %>
  </div>

</body>
</html>

然后我删除了这段代码

<%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css", "application" %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", "application" %>

我解决了这个问题。