Is there a good rails example of running a script from a controller method and saving the result into the database?
I need to run a script and capture the result, then insert it into db.
Command:
@command_result = %x[#{开发者_如何学C"cd ../../cpanel ; ruby login.rb param 2>&1"}]
Is this the right way to do it? Create a controller method, run this in the method, and then somehow save it in the DB? If there is a tutorial or example of doing this kind of thing I would love to know about it. I want to run scripts in a rails application but I'm not sure how to do stuff beyond simple CRUD built in functionality.
Think in terms of the CRUD model, and the answer become clearer. In your case, you could create a model called CommandResult. Let's say it'd have two attributes; the text of the command, and the text of the output. Inside a controller, you could use it to save your data like this:
result = CommandResult.new
result.command = "cd ../../cpanel ; ruby login.rb param 2>&1"
result.output = `#{result.command}`
result.save!
精彩评论