【发布时间】: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
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>
提前致谢!
【问题讨论】:
-
你把链接
@todosinstesdt<%= link_to "Like", like_todo_path(t), method: :put %> -
我改了,还是一样的错误。
-
如果您想查看为您的应用生成的路径,您可以运行
rake routes来查找点赞操作的路径 -
感谢您的回复。我已经检查了几次,这是正确的路径。
-
您的 TodoController 类是否正确定义?您只显示问题中的操作。
标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.2