【问题标题】:nested_form gem rails 4 shows two nested form(fields)on click link_to_addnested_form gem rails 4 在点击 link_to_add 时显示了两个嵌套的表单(字段)
【发布时间】:2014-11-14 14:01:24
【问题描述】:

点击“添加项目”后,添加两个 'fields_for :item' 嵌套表单(字段)

需要帮助:)))

协会:

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

<%= 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 %>

我当前的版本是'rails','4.1.6'... 帮帮我:)))

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    删除布局中的 (:

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。 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" %>
      

      我解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多