使用棘手的模型新建/创建编辑/更新控制器操作

问题描述 投票:0回答:1

我有一个 Rails 应用程序,它的模型有点棘手,如您所见。应用程序能够为给定用户创建任务。

我正在使用下面的控制器/表单。显然,当我为 sby 创建任务时,我必须填写 :executor 字段,因为默认情况下我将成为 :assigner 。一旦创建了任务,从我的角度来看,它将是一个 :signed_task 。 下面的表单非常适合 new/create 操作,并且使用正确的参数也能显示 edit.html.erb,但我收到错误(即使我尝试更改分配的任务而不是执行的任务):“没有路线匹配 [ PATCH]“/users/1/tasks”“当涉及到更新操作时。

我不确定是我的表单还是控制器出了问题。如果我点击 $rake 路线,一切看起来都很好。

我有 2 个问题: 1. 如何使更新操作适用于 :signed_tasks,以便 :assigner 可以编辑他们分配给 sby 的任务? 2. 这是一个更难的问题:我应该怎么做才能让 :executors 能够编辑他们分配给的任务 (:executed_tasks)?

任务模型:

belongs_to :assigner, class_name: "User"
belongs_to :executor, class_name: "User"

用户型号:

has_many :assigned_tasks, class_name: "Task", foreign_key: "assigner_id"
has_many :executed_tasks, class_name: "Task", foreign_key: "executor_id"

新建和编辑表格:

<%= form_for @task, url: user_tasks_path do |f| %>

控制器:

  def new
    @user = current_user
    @task = Task.new
  end

  def create
    @user = current_user
    @task = Task.new(task_params)
    if @task.save
      flash[:success] = "Task saved!"
      redirect_to user_tasks_path(current_user)
    else
      render action: :new
    end
  end

  def edit
    @user = current_user
    @task = Task.find(params[:id])
  end

  def update
    @user = current_user
    @task = @user.task.find(params[:id])
    if @task.update_attributes(task_params)
      flash[:success] = "Task updated!"
      redirect_to user_tasks_path(current_user)
    else
      render action: :edit
    end
  end

private

   def task_params
     params.require(:task).permit(:executor_id, :name, :content, :deadline).merge(assigner_id: current_user.id)
   end

路线

Prefix Verb   URI Pattern                                     Controller#Action
         static_pages_home GET    /static_pages/home(.:format)                    static_pages#home
        static_pages_about GET    /about(.:format)                                static_pages#about
         static_pages_help GET    /help(.:format)                                 static_pages#help
static_pages_privacypolicy GET    /privacypolicy(.:format)                        static_pages#privacypolicy
                  contacts GET    /contacts(.:format)                             contacts#index
                           POST   /contacts(.:format)                             contacts#create
               new_contact GET    /contacts/new(.:format)                         contacts#new
              edit_contact GET    /contacts/:id/edit(.:format)                    contacts#edit
                   contact GET    /contacts/:id(.:format)                         contacts#show
                           PATCH  /contacts/:id(.:format)                         contacts#update
                           PUT    /contacts/:id(.:format)                         contacts#update
                           DELETE /contacts/:id(.:format)                         contacts#destroy
          new_user_session GET    /users/sign_in(.:format)                        devise/sessions#new
              user_session POST   /users/sign_in(.:format)                        devise/sessions#create
      destroy_user_session DELETE /users/sign_out(.:format)                       devise/sessions#destroy
             user_password POST   /users/password(.:format)                       devise/passwords#create
         new_user_password GET    /users/password/new(.:format)                   devise/passwords#new
        edit_user_password GET    /users/password/edit(.:format)                  devise/passwords#edit
                           PATCH  /users/password(.:format)                       devise/passwords#update
                           PUT    /users/password(.:format)                       devise/passwords#update
  cancel_user_registration GET    /users/cancel(.:format)                         devise/registrations#cancel
         user_registration POST   /users(.:format)                                devise/registrations#create
     new_user_registration GET    /users/sign_up(.:format)                        devise/registrations#new
    edit_user_registration GET    /users/edit(.:format)                           devise/registrations#edit
                           PATCH  /users(.:format)                                devise/registrations#update
                           PUT    /users(.:format)                                devise/registrations#update
                           DELETE /users(.:format)                                devise/registrations#destroy
         user_confirmation POST   /users/confirmation(.:format)                   devise/confirmations#create
     new_user_confirmation GET    /users/confirmation/new(.:format)               devise/confirmations#new
                           GET    /users/confirmation(.:format)                   devise/confirmations#show
              user_profile POST   /users/:user_id/profile(.:format)               profiles#create
          new_user_profile GET    /users/:user_id/profile/new(.:format)           profiles#new
         edit_user_profile GET    /users/:user_id/profile/edit(.:format)          profiles#edit
                           GET    /users/:user_id/profile(.:format)               profiles#show
                           PATCH  /users/:user_id/profile(.:format)               profiles#update
                           PUT    /users/:user_id/profile(.:format)               profiles#update
                           DELETE /users/:user_id/profile(.:format)               profiles#destroy
        complete_user_task PATCH  /users/:user_id/tasks/:id/complete(.:format)    tasks#complete
      uncomplete_user_task PATCH  /users/:user_id/tasks/:id/uncomplete(.:format)  tasks#uncomplete
 incoming_tasks_user_tasks GET    /users/:user_id/tasks/incoming_tasks(.:format)  tasks#incoming_tasks
 outgoing_tasks_user_tasks GET    /users/:user_id/tasks/outgoing_tasks(.:format)  tasks#outgoing_tasks
completed_tasks_user_tasks GET    /users/:user_id/tasks/completed_tasks(.:format) tasks#completed_tasks
                user_tasks GET    /users/:user_id/tasks(.:format)                 tasks#index
                           POST   /users/:user_id/tasks(.:format)                 tasks#create
             new_user_task GET    /users/:user_id/tasks/new(.:format)             tasks#new
            edit_user_task GET    /users/:user_id/tasks/:id/edit(.:format)        tasks#edit
                 user_task GET    /users/:user_id/tasks/:id(.:format)             tasks#show
                           PATCH  /users/:user_id/tasks/:id(.:format)             tasks#update
                           PUT    /users/:user_id/tasks/:id(.:format)             tasks#update
                           DELETE /users/:user_id/tasks/:id(.:format)             tasks#destroy
                     users GET    /users(.:format)                                users#index
                           POST   /users(.:format)                                users#create
                  new_user GET    /users/new(.:format)                            users#new
                 edit_user GET    /users/:id/edit(.:format)                       users#edit
                      user GET    /users/:id(.:format)                            users#show
                           PATCH  /users/:id(.:format)                            users#update
                           PUT    /users/:id(.:format)                            users#update
                           DELETE /users/:id(.:format)                            users#destroy
                      root GET    /                                               static_pages#home
ruby-on-rails forms ruby-on-rails-4 model-view-controller crud
1个回答
0
投票

我把它拉下来了。我仍然不明白为什么前一个不起作用,为什么这个起作用。我将它用于 user.profile。尽管该个人资料与用户是一对一的关系,但这是事实。

控制器:

def edit
  @user = current_user
  @task = Task.find(params[:id])
end

def update
  @user = current_user
  @task = Task.find(params[:id])
  if @task.update_attributes(task_params)
    flash[:success] = "Task updated!"
    redirect_to user_tasks_path(current_user)
  else
    render action: :edit
  end
end

形式:

<%= form_for ([@user, @task]) do |f| %>
© www.soinside.com 2019 - 2024. All rights reserved.