-1

我希望当用户创建项目时,他/她也可以向其中添加新任务。我已经想出了这个“解决方案”,并且遇到了这个我无法解决的错误。

我什至不知道我将任务操作与项目控制器混合在一起是“红宝石正确的”。我采用这种方法的原因是我无法在视图中找到解决方案并保存项目..

这是我的 projects_controler.rb

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

  def create
    @project = Project.new(project_params)
    @task = Task.new(task_params)

    respond_to do |format|
      if @project.save and @task.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        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 @project, notice: 'Project was successfully updated.' }
        format.json { render :show, status: :ok, location: @project }
      else
        format.html { render :edit }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    # @list = List.find(params[:id])
    @project.destroy
    respond_to do |format|
      format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
  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(:title, :description, :done, :user_id)
    end



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

    def task_params
      params.require(:task).permit(:title, :done, :project_id)
    end
end

项目.rb

class Project < ApplicationRecord
  belongs_to :user, foreign_key: "user_id"
  has_many :tasks , dependent: :destroy
  validates_presence_of :user
  validates_uniqueness_of :title, on: :create, message: "must be unique"
  validates :title, presence: true, length: { minimum: 2 }

    def done_tasks
     tasks.where(done: true).order("updated_at DESC")
  end
end

任务.rb

class Task < ApplicationRecord
  belongs_to :project, foreign_key: "project_id"#, class_name: "List"
  validates_presence_of :title

  def completed?
    !completed_at.blank?
  end 
end

路线.rb

Rails.application.routes.draw do

  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

    resources :projects do
        resources :tasks
    end


  authenticated :user do
    root to: 'projects#index'
  end
  unauthenticated :user do
    root 'welcome#home'
  end
end

新的.html.erb

<h1>New Project</h1>

<%= render 'form', project: @project, task: @task %>
<%# render 'form', project: @project, task: @task %>

  <!-- custom -->
  <!-- shall I ADD ID TO THE projrct t be trackable by the task????? and describe that in task.rb -->
  <!-- <h1>Tasks</h1> -->
  <%# f.button %>
<!-- button to add a new task -->
<%# f.button "add a new task to the current project" %>

<!-- if else mechanism for adding a new task via a button  -->
<!-- when button pressed, tasks/_form will appear down here -->
<!-- button to save the task while at the same moment  .....  override "create task" and "create project" to save "save project to task" -->

<%# render '../tasks/_form' %>

<%= link_to 'Back', projects_path %>

_form.html.erb

<%= simple_form_for(@project, @task) do |f| %>
<%# form_for([@post, @comment]) do |f| %>

  <% f.error_notification %>

  <div class="form-inputs">
    <%= f.input :title %>
    <%= f.input :description %>
    <%= f.input :done %>
    <%= f.association :user %>
  </div>



  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
4

1 回答 1

0

在 task_params

params.require(:task).permit(:title, :done)

和创建方法应该像

def create
  @project = Project.new(project_params)
  @project.tasks.build(task_params)

  respond_to do |format|
    if @project.save and @task.save
      format.html { redirect_to @project, notice: 'Project was successfully created.' }
      format.json { render :show, status: :created, location: @project }
    else
      format.html { render :new }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end
于 2017-08-21T10:03:57.647 回答