在rails中创建唯一对象

时间:2016-03-17 12:17:14

标签: ruby-on-rails ruby

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

这' 123'通过简单地将任务添加到项目'创建任务。但是同样的任务仍然出现在另一个项目中。如何修复这个bug并使我的任务与众不同?我认为必须在任务或项目模型中添加一些内容 但我不知道应该添加什么。

以下是相应的型号和控制器:

Task.rb

class Task < ActiveRecord::Base
  belongs_to :project
end

Project.rb

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

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

1 个答案:

答案 0 :(得分:1)

要在创建任务时设置project_id,请在表单中添加以下内容:

<%= f.select :project_id, options_for_select(Project.choices) %>

然后在project.rb

def self.choices
  all_projects = []
  Project.find_each do |project|
    # show the name but save the id
    all_projects << [project.name, project.id]
  end
  all_projects
end

然后更改显示Task.where(project_id: project_id)而不是Task.all的任务,如前两条评论所示。