Run ruby script in rails - ruby-on-rails

I made a script in ruby ​​and now I need to run it in a rails application. I'm a little lost, but I have a controller, and wanted through a GET /service /:id returns the result of this script. How can I do this?

By the comments, it seems like you want to make this into a method you can call from your controller.
Simple.
Define the method in the corresponding Model for the Controller you're calling it from (or whatever Model you wish to define the method in) and then call it from the Controller.
Say you have a ScriptRunner model, and you want to call this from the show action of some controller.
In your ScriptRunner model, simply do
def runscript(id)
...
end
and in your controller, do
def show
ScriptRunner.runscript(params[:id])
#service = Service.find_by_id(params[:id])
end
..or whatever you want it to do.

Sergio Tulentsev approach is correct, Just make a model class out of it and call the model method from your controller.
Alternatively which is wrong and hacky you can just call your ruby script directly from the controller
def get
#results = system "ruby your_ruby_script.rb"
end

Related

Ruby on rails, pass an argument from one action to another

I have two actions in my controller:search, show_db.
say, they look like this:
def search
#uploads=current_user.uploads
end
def show_db
end
I need to pass arguments (#uploads) from search-action to show_db-action. (I know I could write #uploads=current_user.uploads in show_db, but I cannot, it is much more complex.)
smt like this:
def search
#uploads=current_user.uploads
show_db(#uploads)
end
def show_db(list)
blablabla
end
Is it possible to do?=)
Many thanks in advance.
If you want to pass the result of one method to another it's pretty simple in Ruby, your second code block should work, you just need to use the list variable from the parameter in your method
def show_db(list)
x = list
list.do_somethings
etc
end
Create a third method that knows how to create #uploads, use it from both actions.
Also, if in your index action you want to render show_db with #uploads variable, you can do this:
def search
#uploads=current_user.uploads
if something
return show_db(#uploads)
end
end
def show_db(list)
blah
render 'show_db'
end
But in this case, show_db is just a method, not a controller action.
You probably want to use a variable across actions. You should know that each time you make a request from browser, you are creating a new instance of controller. Hence, your variables would not work as they should because you will actually be trying to access an instance variable across instances. An impossible task.
I suggest using sessions to store information across multiple requests.
Google up how to use sessions.

Ruby on Rails Controller methods separately?

Just to be educated I wanted to know if it is a good practice to have one controller method for both GET and POST actions, e.g def signup ... end, which would display a form and if request.post? is true - then perform all the business-logic and so on. Is it any good approach, or should I have these methods being separated from each other ?
Thanks in advice!
I think it'll be much better to define a separate action for the post request. You can obviously get it done within the same action, but if you're going to write a big if..else block in sign_up action you may as well use another action. You could call it create if you're short of names :P . It makes the code more logical and readable.
There is little difference in code organisation either way.
With separate methods, it looks like:
def signup_create
# create here
end
def signup_new
# render here
end
With the same method, it looks like:
def signup
if request.post?
# create here
else
# render here
end
end
It looks like they are both reasonably well organised. Choose what your prefer. If they are the default CRUD methods, separate methods are nice, given that there are separate names thought up already (eg. new vs create, edit vs update).
If they are not CRUD, or extra forms on the page, and you can only think of one name for it (like signup), feel free to overload the name and use the same method.
i think that separate actions for different requests are better if u are using(or going to use) method based authorization (eg cancan).

Custom Controller variable in application.html

User Story:
Action for Facebook that has open graph object.
For this I need to modify the tag defined in application.html
Problem:
The logic would need to be defined in helpers or the application_controller
From my understanding this is not clean.
Question:
I want to pass variables directly into the application.html view.
Preferably pass those variables from a custom controller into the application.html. This way I can still utilize the rails routing system to only pass those variables when I am on the facebook action.
The common mechanism for passing variables in to the view is to create instance variables in your controller as these are ported over automatically.
This is the standard approach if it is almost certain they will be used. For things that may not be used, create a helper method that will take care of providing them.
This is the difference between doing this:
def show
#facebook_graph = ...
end
And this in a helper:
def facebook_graph
...
end

How can I create a new object in cron in rails by referencing controller and passing variables?

Currently, I have a manual form-submission way of creating an object which fires off stuff in the create action.
I access this form this way:
http://localhost:3000/contact_faxes/new?contact=1&fax=7&status=done
This takes me to a form, and executes stuff in the create action. As you can see, I take parameters and change them, and pass them as if it were a form submission.
But I now want to enable this same process through a cron job.
One way I did it was to type all the code back into the cron job, not using the methods new/create at all, which didn't seem like the way to go.
How would I be able to leverage the existing methods using the cron job?
Here are the new and create methods:
https://gist.github.com/746686
I would move all of that code out of the controller and into either the model or another workflow object.
In your ContactFax object, create do something like:
ContactFax < ActiveRecord::Base
after_create :send_fax
def send_fax
pdf = Prawn::Document.new
...
OutboundMailer.deliver_fax_email #etc
end
end
That way, in your cron job, you can access the objects without your controller, which is what's getting in your way now.

In Ruby on Rails, when in the View, can you dump out the files and line numbers before reaching View?

The first layer is routing and the second layer the controller, so when in View, is there a way to dump out the path or filenames and line numbers of route and controller before reaching view?
I wanted to do this because www.example.com/stories/12345 is showing a page, but stories_controller.rb doesn't have a def index or def show
Update: I guess it might be as if the program has an error, in which case it will show an application trace and a rails trace.
Well actually there is no other alternative than using a index or show and it has to be defined inside the class StoriesController. But it does not mean that those methods are defined inside the file "stories_controller.rb".
There are a few options:
they are defined in the ApplicationController from which it derives. Or there is another parent-class.
for instance, when you use ActiveScaffold your controller contains a single line active_scaffold that adds all the standard CRUD actions (index, show, ...)
To see where a method is defined, you could type inside your script/console session
StoriesController.new.method('index')
and it would return a set of classes/modules, as explained here.
For instance, in our ApplicationController we include a module that defines a method login_required, and that looks like this:
>> ApplicationController.new.method(:login_required)
=> #<Method: ApplicationController(PilgrimBase::Base::Authenticated)#login_required>
Hope this helps.
I know this doesnt really answer your question, but ...
you can see your routes by doing
rake routes
do you have show.html.erb in your app/views/stories/ ?

Resources