How to make rails code reloaded each request - ruby-on-rails

I'd like to play with rails code to understand it deeper and how everything works internally. And it could be very nice to change rails code and see the changes after reloading page (by default I need to restart server), what is the right way?

you shouldn't need to restart your server just refresh your page, except for adding migrations
also you can play with your rails app interactively using the rails console similar to irb
just type rails console or rails c into your app directory

Related

Reload! rails app without closing rails console or objects instantiated within it?

I am using Rails Console to help me muck around with an object instance running a Watir browser instance.
I've had to navigate via watir through various forms to get to where I want to thus I don't want to have to recreate the object and browser instance everytime I want to see the effects of changed code I'm wriring.
reload! doesn't seem to do the trick for me, confirmed for e.g. through changing proposed browser field text input in the code, calling reload! in the active rails console session and calling a method, the old text still used from before the reload!.
Is there a way to reload so that I can use uodated code on the already created objects in the already running rails console session?
Thanks

How to auto restart rails server with grunt?

I am working with rails-api so each time i change controller or Route file i want to auto reload rails server, so new changes will be applied.
I got basic setup to work with this plugin: link, where rails start together with grunt.
The problem is i want to reload rails server each time any rails files are changed.
Is it possible to achieve?
Yes, of course. You will need to use Guard for this purpose. Please read carefully Guards official page. They're doing good job in explaining how to setup your development environment.
There are tutorials on RailsCasts and also on NetTuts.
Edit: I forgot to mention that there is second(but definitely not the last one) option to achieve this. If you start using Spring which is Rails application preloader. Read more about this on Github page.
Here is great tutorial on how to setup everything together - http://girders.org/blog/2014/02/06/setup-rails-41-spring-rspec-and-guard/

Ruby on Rails. How do I change view file in production without restarting server

I made feature that allows to change one of view files through browser.
It changes file and doesn't use DB. In development mode all works well.
But as far as I know rails caches all files in production mode.
Does somebody know solution?
I have found this post
http://railshints.tumblr.com/post/1559660060/reload-rails-templates-dynamically-in-production-mode but it seems not work or I do something wrong.
I use Rails 2.3.5

Is the rails console dynamic?

is the console in rails (~ rails c) dynamic? For example; if I open the console and then make changes to a model will it pick these changes up or do I have to exit out of the console and run rails c again for it to pick up the changes in the model?
You will need to call the reload! method in the console to reload the changes. This method's magic is automatically called by rails server in development mode.
As a comment's pointed out beneath and another answer here, if you change things to do with the environment of the application, such as adding new gems to the Gemfile, making changes to anything in config or adding a new plugin then you'll need to restart the console. Any changes to app will be reloadable with reload!
If you were using this particular way to test that a method was working, I wouldn't. Tests (as in, the Test::Unit or RSpec) variants are much nicer because you have a reproducible way of running them again and again. rails console is great for one-off testing, but if you want to write a maintainable application then write tests.

Solution found: cPanel-created Ruby On Rails setup returns blank pages for any views

Solution found!
This appears to be a bug specific to Mongrel + Rails 2.3.8, the webserver used by Rails+cPanel. This is why it only shows up when you run the app from cPanel, but not when serving it locally via script/server.
For more details, see:
https://rails.lighthouseapp.com/projects/8994/tickets/4690-mongrel-doesnt-work-with-rails-238
The eventual workaround found is to put the following file in the /config/initializers directory of each new Rails 2.3.8 app:
https://gist.github.com/471663
Once I add this initializer, my Rails app functions as expected.
Original question:
I've gone through the very helpful tutorial at railsforzombies.org and am now ready to get a test RoR install going. My site is on a shared host, using cPanel. I've created the test application using cPanel, and it's set up at ~/rails_apps/blog. I created a rewrite that redirects mydomain.com/testblog/ to mydomain.com:12002, which is the port that cPanel has my RoR app running on. I also started the application via cPanel, in development mode.
If I go to mydomain.com/testblog/, I see the helpful page that lets me know that my RoR app has been created and is functioning. Great, right? Well, not so fast.
I'm following along with the Getting Started guide at guides.rubyonrails.org/v2.3.8/getting_started.html, and I get to step 4. I've run script/generate controller home index and edited the resulting view at app/views/home/index.html.erb. It's just a simple
<h1>Hello rails!</h1>
However, when I go to mydomain.com/testblog/home/index (with or without the trailing '/'), I get a blank page in my browser, nothing at all (View Source shows nothing).
To make sure that I'm not going crazy, I put a text file in my rails app's /public directory, and when I go to mydomain.com/testblog/test.txt, it gets served properly. So I know that the Apache rewrite from cPanel is working properly.
Any ideas? I figure I'm overlooking something that's obvious, but I'm drawing a blank for now.
For reference, I'm running on cPanel 11, Ruby 1.8.7 and Rails 2.3.8. I would love to be running Rails 3.0, but the shared host says it's a no-go for now.
edited to add:
Generating the home controller and index action above added the following lines to my routes.rb file (I've checked, they're there):
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
so mydomain.com/testblog/home/index should map to app/views/home/index.html.erb.
Edited again with more investigation details:
I found something that may shed some light on this. If I stop the rails app in cPanel, then log in via PuTTY and start the dev server with script/server and execute
curl http://0.0.0.0:3000/home/index
the dev server does respond with
<h1>Hello Rails!</h1>
from app/views/home/index.html.erb. So it's working there. It's when I start the Rails app in cPanel (which tells me its running on port 12002) that I get an empty response from mydomain.com/testblog/home/index. I also get an empty response from
curl http://0.0.0.0:12002
if I run it via SSH on the server. So it looks like something screwy with cPanel, not with Rails.
Did you make sure and delete your index.html file in the app/public directory of your app?
Also, after generating the home controller, did you setup your routes.rb to recognize the new controller and action?

Resources