This question already has answers here:
Rails: How to run `rails generate scaffold` when the model already exists?
(7 answers)
Closed 7 years ago.
This is my current situation:
ShoeDeal/
app/controller/
shoe_controller.rb
app/model/
customer.rb
item.rb
order.rb
app/views/shoe/
index.html.erb
db/migrate/
create_customers.rb
create_items.rb
create_orders.rb
I’m making a simple ShoeDeals App where your can select a pair of shoes that you want to purchase and add to a shopping cart. i just kinda found out that ruby has a CRUD terminal command ( rails generate scaffold ) but i already started manually creating the files list. Can i still run this code to finish where i left off?
Generating scaffold files for an existing model/controller etc will create the standard files where they don't exists and skip the ones that do. You can temporarily rename your existing files and inspect/merge the generated code into your existing classes.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
The message I'm getting after running migration command
The db file created after runnig above command is as below
[enter image description here][2]
Error I'm getting while trying to connect with table(demo) in database demo:
let me explain about your error message and your understanding above, when you typed rails generate migration demo (this is from your picture) and then the command line showing the file created, it does not meant the db file created, rails generate migration command usually to add/remove fields from existing table, it's just empty file that that you should put some additional command inside, check db/migration folder,
I believe this getting started with rails can help you.
basically for database steps
Create database file first with rake db:create
create your model with rails generate model Demo, again file created but you have to put some fields inside, check db/migration folder after you run this command
after you put fields then run rake db:migrate
if you missed some fields then you can add additional fields with rails generate migration AddDateToDemo
then put some fields inside and then run rake db:migrate, it's script for rails to execute job database creation/add/remove fields
This question already has an answer here:
Hartl's rails tutorial completed works locally perfect. Heroku persists "We're sorry, but something went wrong."
(1 answer)
Closed 6 years ago.
I have a Ruby on Rails app and I want to deploy it on Heroku. Some errors appear and I think it is because the scaffolding order.
By that, I mean that a class named submissions is first created, but this class references another class named Users that is yet to be created.
This is the error that appears when doing rake db:migrate http://pastebin.com/R83a3rsN
And those are the migrate files and their filename
I guess you mean that:
http://pastebin.com/ig5nHjsj for 20160503205437_create_submissions
and
http://pastebin.com/q2jABiep for 20160428101834_create_users
You have a member in your db/migrate folder named...
20160424205437_create_submissions.db
Just rename it to
20160503205437_create_submissions.db
that will move it to the bottom of the list of migrations and it'll be executed last.
This doesn't seem like a scaffold error per se. Rather, it's an issue with migrations (which scaffolds happened to generate).
To repairs the issue, I'd probably just make a fresh migration which includes all columns your application needs at this point in time. Then edit the order of the create_table blocks to resolve your error.
Then you'd be able to delete the other migration files and run dB:create or reset.
This is an issue with cross-dependencies in your migration. In this case, the Submissions table is expecting that the Users table already exist in order to declare a foreign key to it.
Consider declaring the Users table above the Submissions table in your migration.
I am new user to ruby on rails.
I have some question please give the answer as early as possible
1) Is it possible to create web application without using *rails new application_name* command? means creating required folder and file manually?
2) I want to create application without using scaffold and generator, so everything is created manually...I searched but not get a link to do it...
You really should be using rails new (appname) to generate your project directory.
From there, you do not need to generate a scaffold. If you want to go slightly less abstract and create some things manually you can use rails generate resource (resource name).
If you want to go even less abstract then that, you can use rails generate model (model name) and rails generate controller (controller name) and rails generate migration (migration name). Within this level of abstraction, you can specify options such as methods you want the model to have or columns you want the migration to add.
And the least abstract(most manual) would be if you make these files yourself (like actually going in and creating new folders/files for models, controllers, etc.)
So on an order from most abstract to least:
1) generate scaffold
2) generate resource
3) generate model/controller/migration
4) creating the files/folders without rails
Most developers are usually working with the #2, #3, or #4 layers (remember it is always a tradeoff between eliminating a lot of time off by not having to manually create the same code over and over again and flexibility).
rails new app-name creates tons of files and folders which are necessary for the app to run. You would waste tons of time writing them yourself. Read as "reinventing the wheel".
You can simply create a controller and view file. Add the corresponding route. Start the server. Voila. You will have it. Most intro-articles showcase Scaffold to show the power of Rails i.e how much can be achieved with few lines of code.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could not find generator rspec_model issue
is there any command to generate the model for Rspec without generating any migration and fixture for rails 3, and is it possible to generate the rspec_model after building the project
with same app/model?
The only generator in the rspec-rails gem is rspec:install.
Reading: https://github.com/rspec/rspec-rails#generators
When running rspec:install, it's not going to create spec/model/... files for your existing models, but there is no proble with you running rspec:install on an existing application.
Reading: Could not find generator rspec_model issue
It's really very easy to create your own spec/model/... files; all they need is
require 'rspec_helpers'
at the top.
Is there an easy way to rename a controller and model in my app and all the instances in the corresponding code?
I'm using textmate, would this be as simple as using the replace function and replacing the word Post with Report?
You need to change the name of the Controller and the associated Model, Views, Helpers, Tests & Routes directories, file names, class names & the names in the class definitions.
I found two ways to do this but before you try anything I recommend you back-up your app, preferably with a Software Version Control system like Git & Github.com.
Your first option is to do it manually & there is a good explanation on how to do this here: How to rename rails controller and model in a project
Another way is to destroy your controller & model, and then generate a new one, this will remove all the files which were generated the first time round & replace them with new ones. Michael Hartl explains this solution well in his online guide to Ruby on Rails here: http://ruby.railstutorial.org/chapters/static-pages#sidebar-undoing_things
This is the solution I followed when I needed to make this change to my app, I needed to replace a MVC scaffold I generated called board with a new one called product.
1. First
I made a back-up of the work I did in the layout of the board view, app/views/boards/index.html.erb
2. Then
I ran the below rails commands in the terminal window.
$ rake db:rollback
$ rails destroy scaffold board name:string description:text image:string price:decimal
$ rails generate scaffold product product_type:string name:string description:text image:string price:decimal
$ rake db:migrate
3. Finally
I copied my backed-up boards/index.html.erb file into the newly generated app/views/products/index.html.erb & did a find & replace in my text editor on this file to replace board with product.
I think the second option is much more reliable & quicker but it’s important to make this change early on in your project before you make too many manual changes to the code. It would be better to just take a little more time planning your MVC names & database tables properly before you start your project.
You can also use rails_refactor gem to rename controller, model, etc
for more info check https://github.com/jcrisp/rails_refactor
To rename controller and model use this gem https://github.com/jcrisp/rails_refactor
If you are using textmate, use 'command-shift-f" to look for a string throughout your entire project.
Yes and no. You can rename it that way, but you'll also need to rename the files as well or Rails won't know where to look for the files corresponding to the new Report model/controller/etc.