I'm trying out a few things in Rails and I got a particular issue with the scaffolding process. It might be something I'm not familiar with, let me show you what I did:
First:
rails g controller painel painel
to create a simple front page
Second:
rails g scaffold Noticia title:string category:string tags:string text:text
Then I repeated the same line but changing the name like so:
rails g scaffold Artigo title:string category:string tags:string text:text
and I did the same with Ideia and Analise.
Done that I looked at the files structure and the controllers for Noticia and Ideia were noticia_controller.rb and ideia_controller.rb but the other two were artigos_controller.rb and analises_controller.rb , two were plural and two singular. Naturaly the first two didn't work out on the page as they should and I tried to repeat the same process again and again just to get the same result. I'd appreciate any ideas on the matter, thanks!
This is due to the pluralization rules in Rails being better suited for English than for other languages.
I would strongly recommend avoiding the scaffold generator and just generating your controllers and models using these two commands:
rails g controller artigos
rails g model artigo category:string
And then building out from there.
Related
I have just created the project on ruby on rails. I also generated a controller with an instruction on bash, "rails g controller home".
However, I didn't make database. How can I generate database? Please inform me how to make database.
Thank you.
You can generate a database table by typing:
rails g model myTableName
Because your starting out new i think it would be best to try something like:
rails g scaffold Post name:string content:text
This will link the controller, the model(which is the database) and the views together, giving you a better understanding on how the MVC structure works in rails.
My setup: Rails 3.0.9, Ruby 1.9.2
I wish to generate a scaffold only for the create action, what's the syntax for that? I'm guessing it's something I append to
rails g scaffold Project name:string ...
I don't believe you can. I'm looking at the generator and I see nothing related to using an option to limit the template.
Generator
Template
Rails scaffolding is a very basic tool, it's not meant to be relied on, once you get a handle on the way rails works. However, you can use Ryan Bates' nifty-generator gem to generate scaffolds with greater control. Example:
rails g nifty:scaffold post name:string index new edit
Learn more here. https://github.com/ryanb/nifty-generators
On my blog I'm have posts that belong to a series. I've tried to scaffold series but there are some problems with routes.
The pluralization engine doesn't get it right so I had to manually change Sery, #series, and #sery which is not a big deal.
The routing seems to be ok with resources :series. But then when I try to create a series the form_for helper complains about the route.
And then when I create it with console it works but rails is still complaining about routes.
Please create a simple app and see what the problem is.
rails new test_series_app
And then run the scaffold generator:
rails g scaffold series name:string
And see how the routes are getting mixed up and help me out please!
For the record, I put the singularize code into the scaffold generator (yes, my one contribution to Rails). All it does is check if record_name == record_name.pluralize. If it does and you haven't passed in --force-plural, it calls record_name = record_name.singularize.
In this case "series".pluralize is the same as "series".singularize so I assume it won't do anything.
So if you're having problems w/ it, you need to write an inflector for the word.
(I wrote it after Jeremy Kemper's 2008 RailsConf keynote in which he accidentally passed in a plural model name causing himself all sorts of grief in the middle of his talk.)
I'm new to rails and was trying out the scaffold command - the following scaffold runs and works when I view it via web brick
script/generate scaffold book title:string
the following fails - gives me a weird route error
script/generate scaffold application name:string
the following works
script/generate scaffold app name:string
can anyone shed some light on this? Is 'application' a reserved word?
All your controllers are subclasses of ApplicationController, created by Rails. you can't create another controller with this name.
Yes Application is a reserved word
You can see a complete list on the Wiki
http://wiki.rubyonrails.org/rails/pages/reservedwords
Just so you know, make sure not to do this:
script/generate book title:string
You need to have the word scaffold after generate, like this:
script/generate scaffold book title:string
I want to generate the scaffold in a Rails app, generating the model as usual but having the controller inside the admin namespace. Is it possible?
The first time I've done it, I run
script/generate scaffold blog
and then refactored the controller, views, etc. My prefered solution at the moment is:
script/generate scaffold admin::blog
and then refactor the model, unit test and migration; it's less work.
If there's a better answer, I'll accept it.
You can do this for rails < 3:
script/generate scaffold Blog title:string
or
script/generate scaffold admin::blog title:string
For rails > 3:
rails g scaffold Blog title:string
or
rails g scaffold admin/blog title:string
This question is pretty widely asked on stackoverflow. And I also faced this problem and found no standard solution for that.
So, I created rails-admin-scaffold gem (for now it's Rails 4 only) which automates this process and wrote an article with more detailed explanation. Hope it would be helpful for someone.
For Rails 6:
assuming you have a model like:
rails g model Foo name:string
then you can do this with
rails g scaffold_controller Admin/Foo name:string --model-name="Foo"
(specifying the model name stops the controller from referring to the model Admin::Foo which would be the default)
NB: this isn't perfect; You'll have to fix up a bunch of path errors in your views and tests - but it gets you 80% of the way there.