当改变元素的排列形式时,Rails神器

时间:2016-03-02 14:42:41

标签: ruby-on-rails forms

当您更改出现的工件形式的项目时,请帮助我理解是什么原因?

查看项目#show:

<div class="container">

    <h3><%= @project.name %></h3>

  <h2>Tasks</h2>
  <table class="table table-striped" id="tasks_list">
    <%= render @project.tasks %>
  </table>

  <h2>Add a task:</h2>
  <%= render 'tasks/form'  %>

  <%= link_to 'Back', projects_path %>

</div>

screenshot#1

如果您更改任务列表并添加:

<div class="container">

    <h3><%= @project.name %></h3>

  <h2>Add a task:</h2>
  <%= render 'tasks/form'  %>

  <h2>Tasks</h2>
  <table class="table table-striped" id="tasks_list">
    <%= render @project.tasks %>
  </table>

  <%= link_to 'Back', projects_path %>

</div>

screenshot#2

任务列表大小没有改变 - 但是有一个奇怪的工件。

更新

查看任务#_form.html.erb:

<%= form_for([@project, @project.tasks.build], remote: true) do |f| %>
    <div class="input-group">
      <div aria-describedby="add_project">
        <%= f.text_field :name, class: 'form-control ' %>
      </div>

                <span class="input-group-btn">
                  <%= f.submit 'Добавить', class: 'btn btn-success btn-secondary', id: "add_project" %>
                </span>

    </div>

<% end %>

任务控制器:

def create
    @project = Project.find(params[:project_id])
    @task = @project.tasks.create(task_params)

    respond_to do |format|
      if @task.save
        format.html { redirect_to @project, notice: 'Task was successfully created.' }
        format.js {}
        format.json { render json: @task, status: :created, location: @task }
      else
        format.html { render action: "new" }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end

  end

  def destroy
    @task = Task.find(params[:id])
    @task.destroy

    respond_to do |format|
      format.html { redirect_to products_path, success: 'Task destroyed successfully' }
      format.js {}
    end
  end

  private
  def task_params
    params.require(:task).permit(:name)
  end

项目管理员:

def index
    @projects = Project.all
    @project = Project.new
  end

  def show
    @project = Project.find(params[:id])
  end

  def edit
    @article = Project.find(params[:id])
  end

  def create
    @project = Project.new(project_params)

    respond_to do |format|
      if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.js {}
        format.json { render json: @project, status: :created, location: @project }
      else
        format.html { render action: 'new' }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

已添加到表单:<%= form_for([@project, @project.tasks.klass.build], remote: true) do |f| %>

现在错误:

NoMethodError in Projects#show
Showing /Users/alexandr.dmitrenko/todolist/app/views/tasks/_form.html.erb where line #1 raised:

undefined method `build' for #<Class:0x007ffde19efea8>

Extracted source (around line #1):
1<%= form_for([@project, @project.tasks.klass.build], remote: true) do |f| %>

1 个答案:

答案 0 :(得分:1)

原因是这一点:

@project.tasks.build

在您的表单中。它的作用是构建一个新的,未保存的Task记录并将其存储在关联中。每次进一步调用@project.tasks都会返回通常的结果(来自数据库)+这条新记录。您需要做的是将其替换为:

@project.tasks.scope.build

scope方法将返回关联的新实例,因此可以安全地在其上调用build,因为它不会存储在任何地方,也不会影响您的原始关联对象。