2017-03-05 1 views
-1

todos # index에 NoMethodError가 표시되어 그 이유를 알 수 없습니다.Ruby on Rails : NoMethodError in todos # index

<h1>Listing all Todos</h1> 

<p> 

    <%= link_to "Create a Todo", new_todo_path %> 
</p> 

<table> 
    <tr> 
    <th>Name</th> 
    <th>Description</th> 
    </tr> 
    <% @todos.each do |todo| %> 
    <tr> 
     <td><%= todo.name %></td> 
     <td><%= todo.description %></td> 
     <td><%= link_to 'Edit', edit_todo_path(todo) %></td> 
     <td><%= link_to 'Show', todo_path(todo) %></td> 
     <td><%= link_to 'Delete', todo_path(todo), method: :delete, data: {confirm: "Are you sure?"} %></td> 
    </tr> 
    <% end %> 
</table> 

todos_controller.rb : 잘못된 자리에 end 있었다처럼

class TodosController < ApplicationController 
    before_action :set_todo, only: [:edit, :update, :show, :destroy] 

    def new 
    @todo = Todo.new 
    end 

    def create 
    @todo = Todo.new(todo_params) 
    if @todo.save 
     flash[:notice] = "Todo was created successfully" 
     redirect_to todo_path(@todo) 
    else 
     render 'new' 
    end 
    end 

    def show 
    end 

    def edit 
    end 

    def update 
    if @todo.update(todo_params) 
     flash[:notice] = "Todo was successfully updated" 
     redirect_to todo_path(@todo) 
    else 
     render 'edit' 
    end 
    end 
end 

    def index 
     @todos = Todo.all 
    end 

    def destroy 
     @todo.destroy 
     flash[:notice] = "Todo was deleted successfully" 
     redirect_to todos_path 
    end 

    private 

    def set_todo 
     @todo = Todo.find(params[:id]) 
    end 

    def todo_params 
     params.require(:todo).permit(:name, :description) 
    end 
+2

'todos'컨트롤러를 게시 할 수 있습니까? – Mark

+0

안녕하세요, 저는 방금 todos_controller.rb 코드를 게시했습니다. – pdenlinger

+0

정말 고마워! 나는 아래에 당신의 질문에 대답했다. – Mark

답변

2

것 같습니다 여기 내 index.html.erb 파일의 코드입니다. 방금 컨트롤러를 고쳤습니다. index 조치를 정의하기 전에 귀하의 class TodosController을 조기 종료해야합니다.

class TodosController < ApplicationController 
    before_action :set_todo, only: [:edit, :update, :show, :destroy] 

    def new 
    @todo = Todo.new 
    end 

    def create 
    @todo = Todo.new(todo_params) 
    if @todo.save 
     flash[:notice] = "Todo was created successfully" 
     redirect_to todo_path(@todo) 
    else 
     render 'new' 
    end 
    end 

    def show 
    end 

    def edit 
    end 

    def update 
    if @todo.update(todo_params) 
     flash[:notice] = "Todo was successfully updated" 
     redirect_to todo_path(@todo) 
    else 
     render 'edit' 
    end 
    end 

    def index 
    @todos = Todo.all 
    end 

    def destroy 
    @todo.destroy 
    flash[:notice] = "Todo was deleted successfully" 
    redirect_to todos_path 
    end 

    private 

    def set_todo 
    @todo = Todo.find(params[:id]) 
    end 

    def todo_params 
    params.require(:todo).permit(:name, :description) 
    end 
end  
+0

그게 효과가있었습니다; 대단히 감사합니다! – pdenlinger