0

该项目是一个简单的锻炼创建者,您可以在其中将锻炼添加到锻炼计划中。

我一直在关注Railscast涵盖嵌套模型表单以允许动态添加和删除练习,但遇到了错误,需要作为新开发人员的第二意见。

我不断收到的错误是:Plans#show 中的 NoMethodError

这是提取的代码,带星号的行突出显示错误:

<fieldset>
  **<%= link_to_add_fields "Add Exercise", f, :exercise %>**
  <%= f.text_field :name %>
  <%= f.number_field :weight %>
  <%= f.number_field :reps %>

注意:我创建了运动模型,但没有创建运动控制器。练习只能存在于计划中,但我不确定是否仍需要在练习控制器中创建操作才能添加练习?

我几乎一字不差地关注了 Railscast(我稍微偏离了 _exercise_fields 部分),因此您可以对照他在笔记中共享的文件查看我的文件。

我的 schema.rb

create_table "exercises", force: true do |t|
  t.string   "name"
  t.integer  "weight"
  t.integer  "reps"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "plan_id"
end

create_table "plans", force: true do |t|
  t.string   "title"
  t.datetime "created_at"
  t.datetime "updated_at"
end

我的计划模型:

class Plan < ActiveRecord::Base
has_many :exercises
accepts_nested_attributes_for :exercises, allow_destroy: true
end

我的运动模型:

class Exercise < ActiveRecord::Base
belongs_to :plan
end

我的 _form.html.erb

<%= form_for @plan do |f| %>
  <% if @plan.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@plan.errors.count, "error") %> prohibited this plan from being saved:</h2>

      <ul>
      <% @plan.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>

  <%= f.fields_for :exercises do |builder| %>
    <%= render 'exercise_fields', f: builder %>
  <% end %>
  <%= link_to_add_fields "Add Exercise", f, :exercises %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我的 _exercise_fields.html.erb

<fieldset>
  <%= link_to_add_fields "Add Exercise", f, :exercise %>
  <%= f.text_field :name %>
  <%= f.number_field :weight %>
  <%= f.number_field :reps %>
  <%= f.hidden_field :_destroy %>
  <%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>

我的计划.js.coffee

jQuery ->
  $('form').on 'click', '.remove_fields', (event) ->
    $(this).prev('input[type=hidden]').val('1')
    $(this).closest('fieldset').hide()
    event.preventDefault()

  $('form').on 'click', '.add_fields', (event) ->
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()

我的 application_helper.rb

module ApplicationHelper
  def link_to_add_fields(name, f, association)
    new_object = f.object.send(association).klass.new
    id = new_object.object_id
    fields = f.fields_for(association, new_object, child_index: id) do |builder|
      render(association.to_s.singularize + "_fields", f: builder)
    end
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
  end
end

我对编程比较陌生,所以如果我很容易忽略某些事情,我会提前道歉。非常感谢您提供任何帮助、建议或线索来阅读我的问题。

谢谢!

4

1 回答 1

1

实现了您寻求的功能后,我将给出一些想法:


接受嵌套属性

accepts_nested_attributes_for如您所知,您可以使用函数将属性从父模型传递到嵌套模型

虽然相对简单,但它有一个学习曲线。所以我将在这里解释如何使用它:

#app/models/plan.rb
Class Plan < ActiveRecord::Base
    has_many :exercises
    accepts_nested_attributes_for :exercises, allow_destroy: true
end

如果正确显示,这为plan模型提供了发送任何额外数据的“命令”

要正确发送数据(在 Rails 4 中),有几个重要步骤:

1. Build the ActiveRecord Object
2. Use `f.fields_for` To Display The Nested Fields
3. Handle The Data With Strong Params

构建 ActiveRecord 对象

#app/controllers/plans_controller.rb
def new
    @plan = Plan.new
    @plan.exericses.build
end

使用 f.fields_for 显示嵌套字段

#app/views/plans/new.html.erb
<%= form_for @plans do |f| %>
    <%= f.fields_for :exercises do |builder| %>
        <%= builder.text_field :example_field %>
    <% end %>
<% end %>

使用强参数处理数据

#app/controllers/plans_controller.rb
def create
    @plan = Plan.new(plans_params)
    @plan.save
end

private
def plans_params
    params.require(:plan).permit(:fields, exerices_attributes: [:extra_fields])
end

这应该将所需的数据传递给您的嵌套模型。没有这个,您将无法传递数据,并且您的嵌套表单根本无法工作


附加额外字段

附加额外的字段是棘手的部分

问题是动态生成新f.fields_for对象只能在form对象内(仅存在于实例中)

Ryan Bates 通过将当前form对象发送给助手来解决这个问题,但这会导致问题,因为助手会将新字段的整个源代码附加到链接的on click事件中(效率低下)


我们发现本教程更贴切

它是这样工作的:

  1. 创建 2 个部分:f.fields_for&form部分(用于 ajax)
  2. 创建新路由(ajax 端点)
  3. 创建新的控制器动作(添加额外的字段)
  4. 创建 JS 以添加额外的字段

创建 2 个部分

#app/views/plans/add_exercise.html.erb
<%= form_for @plan, :url => plans_path, :authenticity_token => false do |f| %>
        <%= render :partial => "plans/exercises_fields", locals: {f: f, child_index: Time.now.to_i} %>
<% end %>


#app/views/plans/_exercise_fields.html.erb
<%= f.fields_for :exercises, :child_index => child_index do |builder| %>
     <%= builder.text_field :example %>
<% end %>

创建新路线

   #config/routes.rb
   resources :plans do
       collection do
           get :add_exercise
       end
   end

创建控制器动作

#app/controllers/plans_controller.rb
def add_exercise
     @plan = Plan.new
     @plan.exercises.build
     render "add_exericse", :layout => false
end

创建 JS 以添加额外字段

#app/assets/javascripts/plans.js.coffee
$ ->
   $(document).on "click", "#add_exercise", (e) ->
       e.preventDefault();

          #Ajax
          $.ajax
            url: '/messages/add_exercise'
            success: (data) ->
                 el_to_add = $(data).html()
                 $('#exercises').append(el_to_add)
            error: (data) ->
                 alert "Sorry, There Was An Error!"

为这个庞大的帖子道歉,但它应该可以工作并帮助向您展示更多信息

于 2013-12-18T10:33:08.213 回答