[英]Adding an action to a rails scaffold class from index
I've created a new todo object with 我创建了一个新的todo对象
rails g scaffold todo title:string commplete:boolean
And want to add an action to the index to complete a todo with one click. 并想向索引添加一个操作以一键完成一个待办事项。
So I created an action complete
on the todos
controller and then add the following to the index page
. 因此,我在
todos
控制器上创建了一个action complete
的action complete
,然后将以下内容添加到index page
。
<td><%= link_to 'Complete', complete_todo_path(todo) %></td>
While this is the same as the edit command it will not work. 尽管此命令与edit命令相同,但将不起作用。
I just get undefined method complete_todo_path
我只是得到
undefined method complete_todo_path
Why does edit work, but the new method does not? 为什么编辑有效,但新方法无效?
Try this. 尝试这个。
resources :todos do
member do
get :complete
end
end
You have edit to your routes.rb
file as above. 如上所述,您已经对
routes.rb
文件进行了编辑。
Note: add complete
action to before_action :set_todo
to access the todo object
in complete action view file 注意:将
complete
动作添加到before_action :set_todo
以访问完整动作视图文件中的todo object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.