【问题标题】:The action 'like' could not be found for TodoController找不到 TodoController 的动作“喜欢”
【发布时间】:2016-01-25 17:02:31
【问题描述】:

我正在尝试向我的第一个应用程序添加点赞链接,但出现以下错误:“找不到 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

tod​​o_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>

提前致谢!

【问题讨论】:

  • 你把链接@todosinstesd t&lt;%= link_to "Like", like_todo_path(t), method: :put %&gt;
  • 我改了,还是一样的错误。
  • 如果您想查看为您的应用生成的路径,您可以运行rake routes 来查找点赞操作的路径
  • 感谢您的回复。我已经检查了几次,这是正确的路径。
  • 您的 TodoController 类是否正确定义?您只显示问题中的操作。

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


【解决方案1】:

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

在 todo_controller 中,我只是将我的“like”方法移到了“update”上,并且它起作用了。

有人能告诉我为什么它有效吗?我不知道定位如此重要。

【讨论】:

  • 目前您似乎没有为 update 方法关闭 end,因此在类上下文中未定义 like。虽然你应该有语法错误。你能仔细检查一下括号def .. end、if .. end 是否都正确?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
相关资源
最近更新 更多