开发者

Ruby On Rails on Ubuntu + apache2 + Passenger

I'm trying to start programming in ruby on rails for web. I use Ubuntu 10.04. My IDE is NetBeans. I installed all packages needed to run ruby, rails. I like to use apache for PHP, so I decide to keep with apache installing Phusion Passenger. I also configured a virtual host for port 3000 (I changed the ports.conf too).

I think I configure everything ok, but, I create a new project in NetBeans (I saved in var/www, same folder for PHP files), and open the project with firefox. I create a new file, c开发者_开发技巧alled test.rb in public folder. I run to this test.rb, but firefox ask me to download the file, instead run it.

I think ruby, rails are totally configured. And I think that there's no problem with my apache2 settings. So, how can i run my ruby projects? I can run .erb, .rhtml, but instead run the code, he shows me the code ;s

Thanks. (sorry for the English)


If you're in a Rails app's public folder you can't run a ruby script ( not without explicitly referencing it from elsewhere in your application first, not that you should do it anyway, its probably a security hole ), its not like PHP where hitting the actual file renders the result.

When a request hits the Rails app it will be redirected through the router ( see config/routes.rb ), if you want to execute ruby code in a Rails app you will need to route it to a controller first.

you could do this by running the command

rails generate scaffold/resource test 

( only one of the options scaffold or resource is needed )

This will create all of the boilerplate code ( and add the routes to your routes file ) that you need to start serving requests.

Only static assets ( html, images etc ) will be served through the public folder. this is why all the CSS and JS are stored on the public side.

If you wish to write a pury ruby application that responds to server requests you'll need to write it in its own app structure, not in a Rails application.


scaney is right, and I'll add an additonal point.

/var/www is the directory apache uses to serve content, not rails. You can use that directory to serve non-rails content from your web server, while using the rails framework to serve rails content. To set up your rails project run the rails generate command (for rails3) as scaney shows (or follow one of the many tutorials out there).


I use Ubuntu for Rails development too. It's really best to use the development server for development. I use Passenger for production environment on the host.

Using Apache to kick-start a Rails program when it can be avoided really defeats the purpose of "separation of concerns".

Also, you're going to need the verbose output from the development server to debug you rails apps.


Ruby is not run the same way you run PHP scripts. With Ruby, and Rails, you need to run an application server which runs your code. In the case of Rails this is as simple as running rails server (in the case of Rails 3, ./script/server for Rails 2) when you're in the projects root directory. This will start a server that runs on port 3000, so go to http://localhost:3000/ to view your app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜