2015-10-05 67 views
-1

ERROR

Failure/Error: click_link "New Todo Item" 
NoMethodError: 
undefined method `permit' for nil:NilClass 
# ./app/controllers/todo_items_controller.rb:25:in `todo_item_params' 
# ./app/controllers/todo_items_controller.rb:13:in `new'  
# ./spec/features/todo_items/create_spec.rb:15:in `block (2 levels) in <top 
(required)>' 

我create_spec.rb

require 'spec_helper' 

describe "Adding todo items" do 
    let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries") } 

    def visit_todo_list(list) 
    visit "/todo_lists" 
    within "#todo_list_#{list.id}" do 
     click_link "List Items" 
    end 
    end 

    it "is successful with valid content" do 
    visit_todo_list(todo_list) 
    click_link "New Todo Item" 
    fill_in "Content", with: "Milk" 
    click_button "Save" 
    expect(page).to have_content("Added todo list item.") 
    within("ul.todo_items") do 
     expect(page).to have_content("Milk") 
    end 
    end 

end 

我todo_items_controller.rb

class TodoItemsController < ApplicationController 
    def index 
    @todo_list = TodoList.find(params[:todo_list_id]) 
    end 

    def new 
    @todo_list = TodoList.find(params[:todo_list_id]) 
    @todo_item = @todo_list.todo_items.new 
    end 

    def new 
    @todo_list = TodoList.find(params[:todo_list_id]) 
    @todo_item = @todo_list.todo_items.new(todo_item_params) 
    if @todo_item.save 
     flash[:success] = "Added todo list item." 
     redirect_to todo_list_todo_item_path 
    else 
     flash[:error] = "There was a problem adding that todo list item." 
     render action: :new 
    end 
    end 

    private 
    def todo_item_params 
    params[:todo_item].permit(:content) 
    end 
end 
+2

check params [:todo_item]它是'nil' –

+0

你的问题是什么? – sawa

+0

不能摆脱#tried错误 –

回答

1

试着改变你的todo_item_params到:

private 
def todo_item_params 
    params.require(:todo_item).permit(:content) 
end 
+0

,但仍收到错误故障/错误:click_link“新的待办事项” 的ActionController :: ParameterMissing: 参数是丢失或为空值:todo_item #./app/controllers/todo_items_controller .rb:25:in'todo_item_params' #./app/controllers/todo_items_controller.rb:13:in'new' #./spec/features/todo_items/create_spec.rb:15:in'block(2 levels)在<顶部(必填)>' –

+0

@RohitYadav您的表单看起来如何?你能否在问题中提交表单时发布日志中生成的参数? – Pavan

+0

<%= form_for [@todo_list,@todo_item] do | form | %> \t <%= form.lable:含量%> \t <%= form.text_field:含量%> \t \t <%= form.submit “保存” %> <% end %> –

1

我觉得这个错误有两个原因:

第一个是在你的todo_item_params方法中。您可以通过像帕万建议更改此:

def todo_item_params 
    params.require(:todo_item).permit(:content) 
end 

第二个问题是你有你的控制器配备了两个new方法。没有create,saveupdate方法。