TodoController无法找到“喜欢”的动作

时间:2016-01-25 17:02:32

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.2

我正在尝试添加类似链接到我的第一个应用程序,我收到此错误:“无法找到TodoController的动作' 我尝试了不同的东西,但没有一个能奏效。

的routes.rb

Rails.application.routes.draw do
  devise_for :users

  root :to => 'home#index'
  resources :todo do
    member do
      put "like", to: "todo#like"
    end
  end
end

todo_controller.rb

class TodoController < ApplicationController


def index
    @todos = Todo.where(done: false)
    @todone = Todo.where(done: true)
end

def new
    @todo = Todo.new
end

def todo_params
    params.require(:todo).permit(:name, :done)
end

def create
    @todo = Todo.new(todo_params)
    if @todo.save
        redirect_to todo_index_path, :notice => "Your todo item was created!"
    else
        render 'new'
    end
end

def update

    @todo = Todo.find(params[:id])
    if @todo.update_attribute(:done, true)
        redirect_to todo_index_path, :notice => "Your todo item was marked as done!"
    else
        redirect_to todo_index_path, :notice => "Your todo item wasn't marked as done!"
    end

def like
        @todo = Todo.find(params[:id])
    if @todo.liked_by current_user
        redirect_to todo_index_path, :notice => "Your todo item was liked!"
    else
        redirect_to todo_index_path, :notice => "Your todo item wasn't liked!"
    end
end




def destroy
    @todo = Todo.find(params[:id])
    @todo.destroy
    redirect_to todo_index_path, :notice => "Your todo task has been deleted!"
end

端 端

index.html.erb

  <h2 class="big-title">Todo:</h2>
<% @todos.each do |t| %>
<p><strong><%= t.name %></strong>
<small><%= link_to "Mark as Done", todo_path(t), :method => :put %></small>
<small><%= link_to "Like", like_todo_path(t), method: :put %></small>

提前致谢!

1 个答案:

答案 0 :(得分:0)

好的,我解决了! 问题不是我想的那样,而且非常简单。

在todo_controller中,我只是将“喜欢”方法移到“更新”上,并且它有效。

有人可以告诉我为什么会这样吗?我不知道定位是如此重要。