Rails编辑操作无法找到带有ID的“模型”

时间:2014-08-21 19:59:56

标签: ruby-on-rails ruby-on-rails-3 activerecord routes

我一直在研究各种Rails应用程序,但是今天我们决定开发一个快速的项目/任务应用程序以获得乐趣。

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

这是我的代码的样子

的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索引,编辑和更新操作中的实例变量存在问题,但我不确定。我编写的代码比以前更复杂,但出于某种原因我磕磕绊绊。我已经有一段时间了,因为我从头开始编写一个简单的应用程序,所以我非常缺乏实践。

如果有人能在我的代码中指出错误(或其中许多错误),我会很感激。

更新 耙路线的输出

             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

3 个答案:

答案 0 :(得分:2)

任务是Projects

下的嵌套资源

因此,在edit_task_path中,您必须传递项目ID和任务ID

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

答案 1 :(得分:0)

您的 TasksController 上的编辑方法似乎已被调用,并且您使用相同的参数作为ID来查找项目和任务。在那种方法中你应该是probalby:

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

答案 2 :(得分:0)

在@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 %>

通过调整我的routes.rb文件,编辑正确的路径,将正确的参数传递给编辑动作`(@ project,t),使用Rails传递的正确参数更新我的动作,并设置数组我的form_for语句中的模型我得到了这个基本的应用程序。现在是时候添加一些真正的功能了。谢谢大家的帮助。去表明我需要建立更多的应用程序,所以我不会像以前那样生锈。我已经编写了Rails一段时间并编辑更复杂的代码但是出于某些原因,当我引导一个新的应用程序时,有时候我会得到脑力激荡并忘记我正在做的事情,基本原理。