Rails 编辑操作找不到 ID "Model"



我已经在各种Rails应用程序上工作了一段时间了,但是就在今天,我决定做一个快速的项目/任务应用程序。

我能够创建项目和任务,分配用户和截止日期。我使用成员路由来嵌套每个项目的任务,并且可以毫无问题地创建一个新任务。当我试图从任务索引视图编辑任务时,我的问题出现了。我认为我有我的实例变量错误的任务控制器。它会引发一个异常:找不到id=12的项目(其中12应该是项目id,但不存在),并重定向到url: http://todoapp.dev/projects/12/tasks/12/edit,其中12实际上是实际的任务id。

下面是我的代码

routes.rb

Todoapp::Application.routes.draw do

  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
  devise_for :users
resources :projects do
  member do
  resources :tasks
end
end
root :to => "projects#index"
end

project.rb

class Project < ActiveRecord::Base
  attr_accessible :project_name, :user_id, :due_date
  has_many :tasks
  belongs_to :user
end

task.rb

class Task < ActiveRecord::Base
  attr_accessible :completed, :detail, :due_date, :user_id, :project_id
  belongs_to :project
  belongs_to :user
end

projects_controller.rb

class ProjectsController < ApplicationController
  def index
    @projects = Project.order('due_date ASC')
  end
  def new
    @project = Project.new
  end
  def create
    @project = Project.new(params[:project])
    if @project.save
        redirect_to projects_path, notice: "Project #{@project.project_name} was successfully created.".html_safe
    else
        render :new
    end
  end
  def show
    @project = Project.find(params[:id])
    @tasks = @project.tasks
  end
  def edit
    @project = Project.find(params[:id])
  end
  def update
    @project = Project.find(params[:id])
    if @project.update_attributes(params[:project])
        redirect_to projects_path
    else
        render :edit
    end
  end
end

tasks_controller.rb

class TasksController < ApplicationController
  def index
    @project = Project.find(params[:id])
    @tasks = @project.tasks
  end
  def new
    @project = Project.find(params[:id])
    @task = @project.tasks.new
  end
  def create
    @project = Project.find(params[:id])
    @task = @project.tasks.new(params[:task])
    if @task.save
        redirect_to tasks_path, notice: "Task #{@task.detail} was successfully created.".html_safe
    else
        render :new
    end
  end
  def edit
    @project = Project.find(params[:id])
    @task = Task.find(params[:id])
  end
  def update
    @task = Task.find(params[:id])
    if @task.update_attributes(params[:task])
        redirect_to tasks_path
    else
        render :edit
    end
  end
end

任务/index.html.erb

<h2><%= @project.project_name %> tasks</h2>
<% @tasks.each do |t| %>
<%= t.detail %> - <%= t.user.full_name %> - <%= t.due_date.try(:strftime, "%m/%d/%y") %> <%= link_to "Edit", edit_task_path(t), class: 'btn btn-inverse btn-mini' %></br>
<% end %>
<%= link_to "Add task", new_task_path, class: 'btn btn-inverse btn-mini' %>

任务/_form.html.erb

<%= form_for(@task, :html => { :class => "well"}) do |f|  %>
  <%= f.label 'detail'%>
  <%= f.text_field :detail, :placeholder => 'Buy tool'%>
  <%= f.label 'Assigned To'%>
  <%= f.collection_select(:user_id, User.all, :id, :full_name)%>
  <%= f.label :due_date %>
  <%= f.text_field :due_date %>
   <%= f.button "Add Task", class: 'btn btn-info btn-mini', data: {disable_with: "Adding Task"} %> 
<% end %>

任务/edit.html.erb

Edit Task
<%= render 'form' %>

我想我在tasks_controller中的实例变量有问题。rb索引,编辑和更新的行动,但我不确定。我写过比这更复杂的代码,但由于某种原因,我绊倒了。我已经有一段时间没有从头开始编写一个简单的应用程序了,所以我非常缺乏实践。

如果有人能指出我代码中的错误(或许多错误),我将不胜感激。

rake路由输出

             rails_admin        /admin                                 RailsAdmin::Engine
        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
                         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
                         PUT    /users(.:format)                       devise/registrations#update
                         DELETE /users(.:format)                       devise/registrations#destroy
                   tasks GET    /projects/:id/tasks(.:format)          tasks#index
                         POST   /projects/:id/tasks(.:format)          tasks#create
                new_task GET    /projects/:id/tasks/new(.:format)      tasks#new
               edit_task GET    /projects/:id/tasks/:id/edit(.:format) tasks#edit
                    task GET    /projects/:id/tasks/:id(.:format)      tasks#show
                         PUT    /projects/:id/tasks/:id(.:format)      tasks#update
                         DELETE /projects/:id/tasks/:id(.:format)      tasks#destroy
                projects GET    /projects(.:format)                    projects#index
                         POST   /projects(.:format)                    projects#create
             new_project GET    /projects/new(.:format)                projects#new
            edit_project GET    /projects/:id/edit(.:format)           projects#edit
                 project GET    /projects/:id(.:format)                projects#show
                         PUT    /projects/:id(.:format)                projects#update
                         DELETE /projects/:id(.:format)                projects#destroy
                    root        /                                      projects#index
Routes for RailsAdmin::Engine:
    dashboard GET         /                                      rails_admin/main#dashboard
        index GET|POST    /:model_name(.:format)                 rails_admin/main#index
          new GET|POST    /:model_name/new(.:format)             rails_admin/main#new
       export GET|POST    /:model_name/export(.:format)          rails_admin/main#export
  bulk_delete POST|DELETE /:model_name/bulk_delete(.:format)     rails_admin/main#bulk_delete
history_index GET         /:model_name/history(.:format)         rails_admin/main#history_index
  bulk_action POST        /:model_name/bulk_action(.:format)     rails_admin/main#bulk_action
         show GET         /:model_name/:id(.:format)             rails_admin/main#show
         edit GET|PUT     /:model_name/:id/edit(.:format)        rails_admin/main#edit
       delete GET|DELETE  /:model_name/:id/delete(.:format)      rails_admin/main#delete
 history_show GET         /:model_name/:id/history(.:format)     rails_admin/main#history_show
  show_in_app GET         /:model_name/:id/show_in_app(.:format) rails_admin/main#show_in_app

任务是嵌套在项目下的资源

在edit_task_path中需要同时传递项目id和任务id

<%= link_to "Edit", edit_task_path(@project, t), class: 'btn btn-inverse btn-mini' %>

看起来你的TasksController上的edit方法正在被调用,你正在使用相同的参数作为id来查找项目和任务。在这个方法中,你可能应该:

@task = Task.find(params[:id])
@project = Project.find(@task.project_id)

经过@SteveTurczyn和@Chris Piazza的一些有用的输入,我能够让这个应用程序工作。问题基本上是我设置路线的方式出了问题。我没有正确嵌套,并在项目内部做一个成员路由,它将不同的参数传递到我的控制器动作。

这是我修改后的代码

routes.rb

Todoapp::Application.routes.draw do
  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
  devise_for :users
resources :projects do
  resources :tasks
end
root :to => "projects#index"
end

projects_controller.rb

class ProjectsController < ApplicationController
  def index
    @projects = Project.order('due_date ASC')
  end
  def new
    @project = Project.new
  end
  def create
    @project = Project.new(params[:project])
    if @project.save
        redirect_to projects_path, notice: "Project #{@project.project_name} was successfully created.".html_safe
    else
        render :new
    end
  end
  def show
    @project = Project.find(params[:id])
    @tasks = @project.tasks
  end
  def edit
    @project = Project.find(params[:id])
  end
  def update
    @project = Project.find(params[:id])
    if @project.update_attributes(params[:project])
        redirect_to projects_path
    else
        render :edit
    end
  end
end

tasks_controller.rb

class TasksController < ApplicationController
  def index
    @project = Project.find(params[:project_id])
    @tasks = @project.tasks.order("due_date ASC")
  end
  def new
    @project = Project.find(params[:project_id])
    @task = @project.tasks.new
  end
  def create
    @project = Project.find(params[:project_id])
    @task = @project.tasks.new(params[:task])
    if @task.save
        redirect_to project_tasks_path, notice: "Task #{@task.detail} was successfully created.".html_safe
    else
        render :new
    end
  end
  def edit
    @project = Project.find(params[:project_id])
    @task = Task.find(params[:id])
  end
  def update
    @task = Task.find(params[:id])
    if @task.update_attributes(params[:task])
        redirect_to project_tasks_path
    else
        render :edit
    end
  end
end

视图/任务/index.html.erb

<h2><%= @project.project_name %> tasks</h2>
<% @tasks.each do |t| %>
<%= t.project_id %> <%= t.id %>  <%= t.detail %> - <%= t.user.full_name %> - <%= t.due_date.try(:strftime, "%m/%d/%y") %> <%= link_to "Edit", edit_project_task_path(@project, t), class: 'btn btn-inverse btn-mini' %></br>
<% end %>
<%= link_to "Add task", new_project_task_path, class: 'btn btn-inverse btn-mini' %><%= link_to "Projects", projects_path, class: 'btn btn-inverse btn-mini' %>

视图/任务/_form.html.erb

<%= form_for([@project,@task]) do |f|  %>
  <%= f.label 'detail'%>
  <%= f.text_field :detail, :placeholder => 'Buy tool'%>
  <%= f.label 'Assigned To'%>
  <%= f.collection_select(:user_id, User.all, :id, :full_name)%>
  <%= f.label :due_date %>
  <%= f.text_field :due_date %>
   <%= f.button "Add Task", class: 'btn btn-info btn-mini', data: {disable_with: "Adding Task"} %> 
<% end %>

所以通过调整我的路线。rb文件,编辑适当的路径,传递适当的参数到编辑动作' (@project, t),用Rails传递的正确参数更新我的动作,并在我的form_for语句中设置模型数组,我得到了这个基本的应用程序工作。现在是时候给它添加一些真正的功能了。谢谢大家的帮助。这表明我需要开发更多的应用程序,这样我才不会像以前那样生疏。我编写Rails代码已经有一段时间了,也编辑了一些更复杂的代码,但由于某种原因,当我启动一个新应用程序时,有时会出现脑雾,忘记我在做什么,基本原理就是。

最新更新