Rails generate creates files in wrong folders - ruby-on-rails

when I run a command like rails generate model Address Rails creates the model file and the migration file inside the config folder. A lot of other things are broken too. E.g. I can't launch the server because the configuration file config.ru cannot be found.
What's wrong here and how can I fix this?
I guess some paths are set incorrectly. But I wonder how that could happen because I haven't touched anything since yesterday evening.
Thanks for any help...

Try this
# in your development.rb file write this
config.paths['db/migrate'] += 'db/migrate/'
then run
rails generate model Address

Related

Rails: conflict error overwrite /home/username/blog/.gitignore?

I'm desperately trying to run a server and keep getting this mistake: conflict .gitignore
The output is:
rails new blog
exist
identical README.md
identical Rakefile
identical .ruby-version
identical config.ru
conflict .gitignore
Overwrite /home/jules/blog/.gitignore? (enter "h" for help) [Ynaqdhm]
What should I do? I'm using Ubuntu 20.04. I'm an absolute beginner.
When you run command $rails new it tries to create new rails application for you. But inside that folder that you are running this command file .gitignore already exists.
So, all that you need is to decide do you want to use your .gitignore file that already exists in current folder or to overwrite it with default one.
I think you made some mistake and just to make things simplier, try to run your command rails new blog in some separate empty folder.
It seems like you run rails new blog instead of rails s/rails server.
Since it already has all the files of a rails application called "blog", it's asking if it should overwrite the existing files with the new files it is creating because you ran rails new blog.

Changing Directory Names in a Rails App

I'm relatively new to Rails as well as using PassengerPhusion. I am running an Ubuntu server on Azure, and have the demo app that Passenger provides working fine. I've even changed the text on the homepage.
My question is this:
In my directory, the file directory's name for the app is passenger-ruby-rails-demo and while I am experimenting, i am changing the name of the directory to something like passenger-ruby-rails-demo-test and it returns an error message when viewing fleetpro.cloudapp.net.
I've tried looking through files trying to figure out how this is routed but haven't had any luck. Is there a file within the Rails installation that is telling Passenger to be inside the specific passenger-ruby-rails-demo directory? Pretty newbish question, but it is really bothering me!
I'm not sure about how the naming convention works in regards to the root directory name of your app "passenger-ruby-rails-demo", but I believe the name of that directory is important to running your Rails app, and might have to do something with the name of the module in your config/application.rb file which is named after your Rails app.
There is a solution though: use gem rename.
Add gem rename to your Gemfile and run bundle install.
Then in your app's root directory, run this:
rails g rename:app_to New-Name
This will basically "clone" the app with your new name. You may have to check to make sure all your config files are present afterwards, but from my experience using it, it was a quick breeze. You will most definitely have to push the new renamed app back to git or Azure.
EDIT
As an example I renamed a Rails app to show what you could expect from the output after running the command:
The Rails app's name isn't the problem, it's the PassengerAppRoot switch you'll be using:
PassengerAppRoot /path/to/your/app
Rails doesn't actually care which folder it's put into, so renaming Rails won't fix your problem.
Renaming Rails only changes the internal class references within your application (things like Rails.application.name which have very subtle implications for your app).
In your Azure server, you'll need to locate either your .htaccess / httpd.conf / nginx.conf file, and change the PassengerAppRoot to your new path. This should resolve the issue.

Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first

I'm getting this error:
Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first
But I'm not in a rails directory, I'm on the desktop. There's no .bundle files anywhere to be found. Any ideas?
cd bin/
rm -rf rails
This fixed my problem. I was also getting the same error message
"Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.
Type 'rails' for help."
I was struggling with this over the weekend, but after lots of prodding and poking got it to work.
The problem for me was that Aptana (I believe) created a second rails script in my local bin directory (~/bin) which was called instead of the system script. This local version was older than /usr/local/bin/rails. After moving aside ~/bin/rails, "rails new fred" worked as expected.
Try creating a new folder or rails app in root folder, under ~/
I was having the same problem this morning and that's because rails was already been there while i was creating my application.
To remove the error just go to bin folder of your application and remove the rails folder, and try to create new rails application in your planned folder using rails new app_name.

"Can't initialize a new Rails application" regardless of directory

I'm trying to create a new Rails app using
rails new TLC
And every time regardless of the folder directory (I've tried everything from Documents to Desktop) I'm in I get this error:
Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.
When I type:
ls -a
This is returned:
. .. .DS_Store R9 TLC amplifyu mymanual
Can someone guide me in the right direction?
There may be a Rails application on your PATH (remove it from PATH to fix the problem). Try running rails server in an empty directory to confirm that this is the case.

Ruby on Rails config.secret_token error

I just started learning ruby on rails.
I followed a lot of install examples, but when i ran the example am getting this error
A secret is required to generate an integrity hash for cookie session data. Use config.secret_token = "some secret phrase of at least 30 characters"in config/initializers/secret_token.rb
I search for it but i dont see too much help.
plz help.
Platform: Mac OS X.
The easiest way to generate a new secret token is to run
rake secret
at the command line.
Your token should have been generated automatically by Rails, but you could still use something like:
irb(main):007:0> "#{ActiveSupport::SecureRandom.hex(64)}"
=> "921b00fcfabe0368d70627020f3b4c969cfd9bdc2474f4040c1ae976f687014694beb5d36dfc0c41bac8ebde96a14fceaee228d6e34d8183c5d7cc99d310d4f9"
meaning, you can generate some random string and put it into your config/initializers/secret_token.rb file:
# Be sure to restart your server when you modify this file.
Yourapp::Application.config.secret_token = '921b00fcfabe0368d70627020f3b4c969cfd9bdc2474f4040c1ae976f687014694beb5d36dfc0c41bac8ebde96a14fceaee228d6e34d8183c5d7cc99d310d4f9'
This is an issue with rails version probably. I had this issue when I uninstalled Rails 4 and installed Rails 3. After checking rails -v and seeing that it was indeed Rails 3, I executed rails new myapp. For some reason the configuration file config/initializers/secret_token.rb had the "config.secret_key_base" variable defined, which appears to be how Rails 4 does it. I was able to fix it by changing it to "config.secret_token", which I believe is what Rails 3 uses.
This simple command worked for me :
rvmsudo rake generate_secret_token
Make sure you have this in your environment.rb:
YourApp::Application.initialize!
Ran into this same issue and found out my config/initializers/secret_token.rb file was being ignored by git in my .gitignore file. Check out the config/initializers directory in your git source location and make sure the secret_token.rb file exists. If it doesn't edit your .gitignore file so that git will not ignore the secret_token.rb file and commit your changes (usually hidden - I used these simple commands to display hidden files on a mac http://osxdaily.com/2009/02/25/show-hidden-files-in-os-x/).

Resources