Reusing the model to write a CLI

Hi,
I am beginner to Rails. So far has been a happy PHP developer. I wrote
a beginner application in rails. this has been working very good for
the web. I want to re-use the model to write views in CLI. How can i
do this? I searched on google and could not find a good solution to
this one.

say its a basic blog application that has web UI (myblog.com/posts/
new) is an action to add a new post and it shows up a web page with
‘title’ and ‘content’ field in the GUI that the end user fills in and
saves

Now I want to do the same thing using a CLI. I login into the server
using ssh, then say this on the terminal

newpost title content where newpost is my ruby file. It needs to have
access to the model of the application so it can save the data into
the table.

Am I clear here or is it still vague

kiran

On 12 September 2011 18:31, maskiran [email protected] wrote:

Hi,
I am beginner to Rails. So far has been a happy PHP developer. I wrote
a beginner application in rails. this has been working very good for
the web. I want to re-use the model to write views in CLI.

You want to re-use the model to write views in CLI (Command Line
Interpreter?)? Sorry, that means nothing to me. Can you explain
again what you want to do?

Colin

On 09/12/2011 05:53 PM, maskiran wrote:

the table.

a beginner application in rails. this has been working very good for
the web. I want to re-use the model to write views in CLI.
You want to re-use the model to write views in CLI (Command Line
Interpreter?)? Sorry, that means nothing to me. Can you explain
again what you want to do?

Colin
If you are intending to do this occasionally as a part of maintenance
etc you can use the console (script/console in 2.x and rails console or
something like that in ROR 3.x). This will give you access to your
objects so you could do something like Post.create(:title =>‘something’,

maskiran wrote in post #1021560:

say its a basic blog application that has web UI (myblog.com/posts/
new) is an action to add a new post and it shows up a web page with
‘title’ and ‘content’ field in the GUI that the end user fills in and
saves

Now I want to do the same thing using a CLI. I login into the server
using ssh, then say this on the terminal

newpost title content where newpost is my ruby file. It needs to have
access to the model of the application so it can save the data into
the table.

You can do that a number of different ways:

1: Using the Rails console interface.

$ rails console (or “rails c” for short)

post = Post.create(:title => “My Post”, :publish_date => Date.today)

2: Use curl to post directly to the web application sending data as JSON

$ curl -X POST --data “{"title":"My
Post","publish_date":"2011-09-12T21:08:57-04:00"}”
http://myblog.com/posts

The point is that Rails provides excellent support for making your web
application double as a REST web service. You should use Rails
controllers as the gateway to all updates to the backing database.

Using “rails console” is helpful for testing, or just quickly adding
some data to the database. But, any updates to a production application
should go through the same controllers that the web application uses for
its own updates.

Hi
Thanks a lot. I guess as all of you have suggested my CLI would just
be calling the web links directly using curl or wget. I am using such
a method now. I thought there might be another way that I could use to
write cli, some thing of the form

require rails
require mydatamodel
use the methods in the model, with data from the cli args or a file

Thanks
Kiran