从方法调用系统(ruby on rails) [英] System call from method (ruby on rails)

查看:66
本文介绍了从方法调用系统(ruby on rails)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Ruby of Rails的方法进行系统调用,但我希望它保持在同一页面上. 现在由于某种原因它没有执行,但是显示:

I need make system call from method of Ruby of Rails , but I want it to stay on the same page. Right now for some reason it does not execute , but shows :

Routing Error

No route matches [POST] "/devices/22918"
Try running rake routes for more information on available routes.

这是按钮:

<%= link_to image_tag("/images/glossy_green_button.png"), device , :method => :turnon, :confirm => "Are you sure?" %>

这是方法:

def turnon
@device = Device.find(params[:id])
result = `/perl/toggle.pl @device.infodot on`
end

请让我知道我在做错什么,

please let me know what I am doing wrong,

谢谢

D

推荐答案

您只是没有正确使用method.您正在使用它来定位要执行的控制器的action(请注意,为清楚起见,我明确地表示了动作而不是方法). 控制器中的可用操作由您的routes.rb文件定义.您最终应该阅读或重新阅读那个

You're simply not using the method correctly. You're using it to target the action of the controller you want to execute (note that I explicitly said action and not method for clarity). Available actions in a controller are defined by your routes.rb file.You should eventually read or re read that

以您的情况为例,假设您有资源device(我想这就是您的资源),您首先要在routes.rb文件中创建一个新动作

In your case, let's say you have a resource device (I guess this is what you have), you'll first create a new action in your routes.rb file

resources :devices do
  put :turnon, on: :member
end

您可以在此处阅读有关此语法的文档,但是基本上我是m通过每个设备上的HTTP PUT method使操作turnon可用,这意味着可以通过URL /devices/1/turnonurl_helper:turnon_device_path(或turnon_device_url)

You can read doc about this syntax here but basically I'm making the action turnon available via the HTTP PUT method on each devices, meaning that it will be accessible through the URL /devices/1/turnon or via the url_helper : turnon_device_path (or turnon_device_url)

我假设您的打开动作将修改现有内容,而不创建新内容,这就是为什么我使用PUT动词

I assume that your turnon action will modify existing things, not creating new things, that's why I'm using the PUT verb

然后该链接将类似于:

<%= link_to image_tag("/images/glossy_green_button.png"), turnon_device_path(device) , :method => :put, :confirm => "Are you sure?" %>

看到该方法是与我创建的新路由相对应的HTTP方法.

see that the method is the HTTP method corresponding to the new route I created.

我还假定您将turnon方法放入DevicesController.

I also assume that you put the turnon method in the DevicesController.

最后,如果要在ajax中执行此操作,可以查看remote: true

Finally as you want to do that in ajax, you can have a look a the remote: true option

这篇关于从方法调用系统(ruby on rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆