创建时的NoMethodError和Rollback

时间:2017-04-28 02:43:35

标签: ruby-on-rails ruby

我正在创建一个用户有任务,文档,约会等的应用程序,但是当我尝试在/ users /创建一个新任务时,我一直遇到这个错误:id / tasks / new

任务#create中的NoMethodError

#<#:0x007ff9ed2fd200>的未定义方法`tasks_path' 你的意思是? asset_path

并在控制台日志中显示

Started POST "/users/2/tasks" for 127.0.0.1 at 2017-04-27 22:22:52 -0400
Processing by TasksController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"YG2qRnuHT92OsJ2OkMrU9ztNTD+zVb88jiRN+tr85sX5E68zeGsxkv++ecI76Txx0iNPmX/jw3rLlBRf1EJ0yQ==", "task"=>{"name"=>"d", "description"=>"d"}, "commit"=>"Create Task", "user_id"=>"2"}
   (0.1ms)  begin transaction
   (0.0ms)  rollback transaction
  Rendering tasks/new.html.erb within layouts/application
  Rendered tasks/new.html.erb within layouts/application (161.5ms)
Completed 500 Internal Server Error in 170ms (ActiveRecord: 0.1ms)



ActionView::Template::Error (undefined method `tasks_path' for #<#<Class:0x00555bf7a14598>:0x007ff9ed2fd200>
Did you mean?  asset_path):
    1: New Task:
    2: 
    3: <%= form_for [@user,  @task] do |f| %>
    4: 
    5: <div>
    6: <%= f.label :name %><br/>

这是我的控制器:

class TasksController < ApplicationController

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

  def create
    @user = current_user

    @task = Task.new(task_params)
    if @task.save
      redirect_to '/'
    else
      rendirect_to '/'
    end
  end

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

  def destroy
  end

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

  def index
    @task = Task.all
  end

  private

  def task_params
    params.require(:task).permit(:name, :description)
  end
end

(我为了测试目的而重定向到'/')

这是我的routes.rb

Rails.application.routes.draw do

  root 'home#index'
  resources :home, only: [:index]
  resources :users do
    resources :appointments, :tasks, :documents
  end

  resources :sessions, only: [:new, :create, :destroy]

end

task.rb

class Task < ApplicationRecord
  belongs_to :user
end

user.rb

class User < ApplicationRecord
  has_secure_password

  has_many :tasks
  has_many :appointments
  has_many :documents
end

最后是新任务的视图

<%= form_for [@user,  @task] do |f| %>

<div>
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %><br/>
<%= f.text_area :description %>
</div>

<%= f.submit %>

<% end %>

我已经把头发拉了好几个小时,所以感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

你有两个问题。回滚是由于验证失败。在这种情况下,它是隐式的:belongs_to默认验证是否存在。由于您实际上没有将用户分配给任务,因此nildef create @task = Task.new(task_params) @task.user = current_user # ... end 。试试这个:

task_path

至于rake routes不存在,我相信它是因为你把它嵌套了。您可以随时检查tasks或点击应用程序中不存在的页面,以获取可用路线助手的列表。在这种情况下,我认为你应该从users取消嵌套user_id,因为你实际上并没有在URL中使用resources :users resources :tasks param。

rendirect_to

然后你就开始了。

哦,请注意拼写错误:您的create操作else条款中有redirect_to。你几乎肯定是#header { position: relative; width: 100%; height: 50px; background: #CCC; } #formBox { position: absolute; left: calc(50% - 100px); bottom: -100px; width: 200px; height: 100px; background: orange; } #bodyStuff { width: 100%; height: 300px; background: lightBlue; }

相关问题