System call from method (ruby on rails)

眉间皱痕 提交于 2020-01-06 08:16:30

问题


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.

This is the button:

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

This is method:

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

please let me know what I am doing wrong,

thank you

D


回答1:


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

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

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)

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

Then the link will look something like :

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

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

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

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



来源:https://stackoverflow.com/questions/13149640/system-call-from-method-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!