So I'm working on a new project that seems like a great choice for using the new Engine functionality. It's as engines say, its own little app, w/ its own views and controllers and models. Here's where I'm coming up short.
I create my test application in which I will mount the new engine.
rails new engine_app && cd engine_app
I then create the new engine
rails plugin new my_engine --mountable
I then add the 'gem' to the engine_app's gemfile
gem 'my_engine', :path => './my_engine'
I then mount the engine in engine_app's routes as so
mount MyEngine::Engine, :at => '/my_engine'
I then cd into my_engine's dummy app and run
rails generate model MyModel title:string body:text
Here's where I run into my confusion. From what I understand this is supposed to generate a namespace table (I think it would be my_engine_my_model). The table in the migration file is just my_model.
Secondly how do I run this migration and is the migration file correct in only calling the table :my_model? I have tried running the following but nothing seems to happen, and I've checked the database and the table isn't there.
So to recap, I need to know how to create migrations in the engine, and be able to run them on the parent apps database correctly.
Thanks for any help and guidance.
So all of the tutorials I read didn't specify that you needed to run script/rails generate from within the root level of your engine. I kept seeing references telling me to goto the test/dummy app. After running script/rails generate model [fields] from the root of my engine it created the appropriate model, migration rake task and i was able to run
rake my_engine:install:migrations; rake db:migrate
to run the migrations
You can use the generator with the engine target:
bin/engem ENGINE_NAME rails g GENERATOR
Related
Im following a rails tutorial, and when im supposed to run the command: 'bin/rails generate model Article'. An error occurs saying that there isnt such a command.
I'm using 'command prompt with ruby on rails' and in the rails project i can find a Bin folder. Am also using windows 7.
Also What is the difference between running only 'rails generate' instead of running 'bin/rails generate'?
Using rails generate is fine if you have no bin stubs (binaries in the root /bin folder of your project). If you do have bin stubs then it's preferred to use them because they may do additional things specific to your project. But even then, it's (probably) fine to just use rails generate still. The other bin stubs may be a little more necessary to use, though (again, if present) because they tend to be shortcuts to e.g. bundle exec rake.
Rails 4.1 ships with bin stubs. That is, when you generate a Rails 4.1 project it generates bin stubs for you. So this is probably why your tutorial mentioned using them -- they're now there by default. But if you're on an older version of Rails that won't help you much.
The big reason Rails 4.1 includes bin stubs is because Rails uses spring by default now. Spring is an application preloader... that makes it so that when you call e.g. bin/rake ... it will load and keep a running rails environment in the background and then, the 2nd time you call bin/rake it will fork from the running environment giving you almost instantaneous response. So this is an example of "additional things specific to your project" that you get from using bin/rake over just rake and bin/rails over just rails.
I am looking at rails plugins to help me modularize my application. I have some basic questions that I am confused about.
Can a rails plugin have its own DB? My application is very little traffic, for internal use, so I am fine with the idea of separate sqlite DB's for each plugin. When I do a "rails plugin new" even if I use --full, there is no database.yml generated. If I create one and do a rake db:create, no sqlite db is created.
Is there a good tutorial available for creating a rails plugin with rails 3.2? Most I find are older and use the enginex gem which I think is now built into rails.
Can you run your plugin as a standalone app for testing, i.e. using WEBrick? When I run "rails server" in my plugin directory, it just says "Error: Command not recognized".
I guess that's it, I am just confused on how to begin.
Creating Migrations
The Rails Guide "Getting Started with Engines" instructs you to use 'rails g model post' from the root directory for your engine.
Getting Started with Engines
If you do this, it will create the db/migrate folder for you with the migration inside of it.
$ rails g model post
invoke active_record
create db/migrate/20120517184738_create_my_engine_posts.rb
create app/models/my_engine/post.rb
invoke test_unit
create test/unit/my_engine/post_test.rb
create test/fixtures/my_engine/posts.yml
You can also generate migrations directly just the same, just as you do with a Rails app.
$ rails g migration AddMyEngineTable
invoke active_record
create db/migrate/20120517185241_add_my_engine_table.rb
Running Rails Server
The Rails Guide also states to run 'rails s' from test/dummy, not from the root of your engine directory.
I see that from an ASCIICast on the subject which covered Rails 3.1 RC5 that you used to be able to run 'rails s' from the root directory of your engine/gem. This is no longer the case.
From the Rails issue posted on Github three months ago it appears that they needed to keep the scope of the engine separate from the scope of the dummy app.
Issue #4894: Mountable Engines Rails File
In short run from the engine root:
test/dummy/script/rails s
I use refinery-cms to create a webshop in ruby-on-rails and refinery is largely based on engines. I have therefore made a "webshop" engine with all my models, and i want to install the gem "activeadmin" inside this engine, so when i run "rails g active_admin:install" it will create the model and config files inside my engine dir. I can only get it to install in the root app
Hope you can help me
/Johan
The solution to this was just to cd in to the engine folder, and run the generator command from there.
I'm wasting my time here and I can't seem to figure this out..
I have used Cucumber in Rails applications before, and if I'm not mistaken, it generates the features/step_definitions/web_steps.rb file when you run rails g cucumber:install. Right?
I looked this up in a book I was using a while ago to learn Rails and it says so there aswell:
It nevertheless passes because of the features/step_definitions/web_steps.rb
file, which was generated when you ran the rails generate cucumber:install command.
However, when I run it in this application I'm trying to start working on, it does not generate it..
$ rails g cucumber:install
create config/cucumber.yml
create script/cucumber
chmod script/cucumber
create features/step_definitions
create features/support
create features/support/env.rb
exist lib/tasks
create lib/tasks/cucumber.rake
force config/database.yml
No web_steps.rb to be found. Am I losing my mind here?
Thanks.
Which version of cucumber are you using? if it is a recent version, see
https://github.com/cucumber/cucumber-rails/blob/f027440965b96b780e84e50dd47203a2838e8d7d/History.md
I'm new to rails and trying to follow along with railstutorial.org.
When I use Rails generate scaffold user:string password:string, it creates a new folder entitled generate and does the same command as "Rails Demo_App". I'm assuming I don't have something installed correctly.
I'm using Windows 7, Cygwin/Vim/Sqlite3 - I have needed to reinstall Cygwin about 3 times to make sure openssh, git, vim, and other plugins/libraries were installed. I'm assuming I will need to do something similar again.
Also, a Gemfile isn't created with the rails command. Could this be related?
Aren't you supposed to call script/generate scaffold user:string password:string ? AFAIK, calling generate not from app folder may cause incorrect behaviour.
Use script/generate scaffold ... inside of your application directory. The rails command is used to create a new project.