在Rails 4

时间:2016-04-02 15:53:43

标签: ruby-on-rails ruby

所以我通过编写一些简单的应用程序来学习rails,在那个应用程序中我有两种类型的对象:Tasks和Projects.Sense是所有这些对象都可以由用户创建,用户必须能够操作它们每个任务belongs_to项目和项目has_many任务。但是当我创建新项目时 - 它已经拥有来自所有其他项目的所有任务,所以它看起来像: enter image description here

这' 123'通过简单地将任务添加到项目'创建任务。但同样的任务仍然出现在另一个项目中。如何解决这个问题并使我的任务独一无二?我认为必须在任务或项目模型中添加一些内容但我不知道应该怎么做添加。

以下是相应的控制器: tasks_controller.rb

class TasksController < ApplicationController
  before_action :set_task, only: [:show, :edit, :update, :destroy]

  # GET /tasks
  # GET /tasks.json
  def index
    @tasks = Task.all
  end

  # GET /tasks/1
  # GET /tasks/1.json
  def show
  end

  # GET /tasks/new
  def new
    @task = Task.new
  end

  # GET /tasks/1/edit
  def edit
  end

  # POST /tasks
  # POST /tasks.json
  def create
    @task = Task.new(task_params)

    respond_to do |format|
      if @task.save
        format.html { redirect_to home_url }
        format.json { render :show, status: :created, location: @task }
      else
        format.html { render :home_url }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /tasks/1
  # PATCH/PUT /tasks/1.json
  def update
    respond_to do |format|
      if @task.update(task_params)
        format.html { redirect_to home_url }
        format.json { render :home_url, status: :ok, location: @task }
      else
        format.html { render :home_url }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tasks/1
  # DELETE /tasks/1.json
  def destroy
    @task.destroy
    respond_to do |format|
      format.html { redirect_to home_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_task
      @task = Task.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def task_params
  params.require(:task).permit(:deadline, :name, :project_id)
end
end

projects_controller.rb

   class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]

  # GET /projects
  # GET /projects.json
  def index
    @projects = Project.all
  end

  # GET /projects/1
  # GET /projects/1.json
  def show
  end

  # GET /projects/new
  def new
    @project = Project.new
  end

  # GET /projects/1/edit
  def edit
  end

  # POST /projects
  # POST /projects.json
  def create
    @project = Project.new(project_params)

    respond_to do |format|
      if @project.save
        format.html { redirect_to home_url }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :home_url }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /projects/1
  # PATCH/PUT /projects/1.json
  def update
    respond_to do |format|
      if @project.update(project_params)
        format.html { redirect_to home_url }
        format.json { render :show, status: :ok, location: @project }
      else
        format.html { render :home_url }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    @project.destroy
    respond_to do |format|
      format.html { redirect_to home_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_project
      @project = Project.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
      params.require(:project).permit(:name)
    end
end

以及相应的观点: 任务

<% @tasks.each do |task| %>
  <div  class="row task">
    <div class="col-xs-12">
      <div class="col-xs-1 checkbox">
         <%= check_box_tag 'accept' %>
      </div>
      <div class="col-xs-8 taskbody">
        <%= task.name %>
      </div>
    <div class="mini-glyph">
      <div class="col-xs-1">
        <span class="glyphicon glyphicon-arrow-up"></span>
        <span class="glyphicon glyphicon-arrow-down"></span>
      </div>
      <div class="col-xs-1">
        <%= link_to edit_task_path(task) do %>
          <span class="glyphicon glyphicon-pencil"></span>
        <% end %>
      </div>
      <div class="col-xs-1">
        <span><%= link_to " ", task, method: :delete, data: { confirm: 'Are you sure?' }, class:"glyphicon glyphicon-trash" %></span>
      </div>
    </div>
  </div>
</div>
<% end %>

对于项目:

<div class="container">
  <% @projects.each do |project| %>
  <div  class="projectblock">
    <div class="row project-bar">
      <div class="col-xs-12 ">
        <div class="col-xs-1">
          <span class="glyphicon glyphicon-list-alt"></span>
        </div>
        <div  class="col-xs-9 prname">
          <%= project.name %></td>
        </div>
        <div class="col-xs-1">
          <%= link_to edit_project_path(project) do %>
            <span class="glyphicon glyphicon-pencil"></span>
          <% end %>
        </div>
        <div class="col-xs-1">
          <span><%= link_to " ", project, method: :delete, data: { confirm: 'Are you sure?' }, class:"glyphicon glyphicon-trash" %></span>
        </div>
      </div>
    </div>
      <div class="row add-task-bar">
        <div class="input-bar col-xs-12 ">
        <div class="glyphicon glyphicon-plus col-xs-1 left_plus" ></div>
        <div class="col-xs-10" >
          <div class="input-group">
            <input type="text" class="form-control" placeholder="Start typing here to create a task...">
            <span class="input-group-btn">
                <button class="btn btn-default" type="button"><%= link_to 'Add task', new_task_path %></button>
            </span>
          </div>
        </div>
      </div>
    </div>
    <div class="col-xs-8 taskbody">
<% project.tasks.each do |task| %>
  <%= @task.name %>
<% end %>
</div>
</div>
    <% end %>
</div>
  <div class="row text-center">
    <div class="col-xs-4"></div>
      <div class="todo-btn col-xs-4">
        <a href="projects/new" class="btn">
            <span class="glyphicon glyphicon-plus"></span>
            Add TODO list
      </a>
    </div>
</div>

我知道我第二次问这个问题,但由于我的错误,最后一个问题没有完全回答 - 我根本无法理解我应该在哪里插入&#34 ; Task.where(project_id:project_id)&#34; 所以我不得不再次征求意见

Project.rb

class Project < ActiveRecord::Base
  belongs_to :user
  has_many :tasks, dependent: :destroy
  validates :name, presence: true, uniqueness: true
end

Task.rb

class Task < ActiveRecord::Base
  belongs_to :project
end

1 个答案:

答案 0 :(得分:1)

你的问题似乎在这里:

<%= render :template => 'tasks/index' %>

你没有显示你的代码,但基本上你是从我想象的只是调用所有任务的索引中渲染你的所有任务。

您要做的是用以下内容替换它:

<% project.tasks.each do |task| %>
  <%= task.name %> <%= task.whatever_else_xx %>
<% end %>

我没有把它们放到格式中,但你可以根据需要包装html,而且我没有使用你可能想要做的部分,而且我已经认为project是你的项目对象被填充的,它包含has_many:tasks,但你应该明白这一点。

你实际上并不需要做任务。其中,rails active record会为你处理它,但是我注意到这一行可能有点问题:

params.require(:task).permit(:deadline, :name)

创建任务时,如果您已使用迁移,则需要设置数据库应具有的project_id列 - 这将为给定任务设置父项目。

params.require(:task).permit(:deadline, :name, :project_id)

并且您需要确保将其作为隐藏字段在表单中发送。您可以通过手动将所需的project_id添加到任务行来手动检查上述代码是否正常工作,但是对于新的应该发送id的任务。