I want to generate scaffolding based on an existing model using scaffold_controller.
rails generate scaffold_controller NAME
I only get empty views, I'm not getting the fields defined in the migration. Not sure what is really going on because I use to do this and it use to work all the time in previous projects.
I'm using Rails 3.2.12, is this feature no longer available?
Thanks for the help.
If you want to have attributes in your views, you have pass them into the generate scaffold_controller command, like this:
generate scaffold_controller <YOUR_MODEL_NAME> attribute1 attribute2 attribute3
Then it will create views with just those attributes in them.
After further investigation, I think the problem is specific to my Rails installation, not sure what but if you ever get this problem, this is the workaround that I found but requires twitter bootstrap:
rails generate scaffold_controller <YOUR_MODEL_NAME>
rails g bootstrap:themed <YOUR_MODEL_NAME> -f
Hope it helps.
Your example uses scaffold_controller where as if you would like a model to be created and displayed in your views you should use just scaffold. In the case of your code example:
rails generate scaffold_controller NAME
This should become:
rails generate scaffold NAME
You will need to specify your attributes and then run
rake db:migrate
Related
How can you create multiple scaffolds at once at the rails command line?
so that these commands could be send at once
rails g scaffold person name:string
rails g scaffold adress street:string person:references
rails g scaffold website url:string person:references
Thank you
Generators are initialized from scripts/rails. You could specify your own generator which just chains generators, or use something like rake to invoke multiple commands, or use a shell script if you want them literally chained.
I'm sharing one database for two web applications. The User model is already being used for one of these apps, so, in order to sign in to the other one, I had to create another model to avoid mixing users info.
I could make Devise work for this new model, called SystemUser. The problem is now I'll have to use every variable with another name. For example: current_system_user, system_user_signed_in?, etc. I'm using these variables, with their original name, across the whole application, and I would like to know if there's a way to avoid overwriting it. For example: by creating a method called current_user that returns current_system_user, and that way with the other variables mentioned before.
I think this should do the trick:
devise_for :users, class_name: 'SystemUser'
have you considered using CanCan for roles?
https://github.com/ryanb/cancan
I could finally solve the issue by generating the Devise views again. I don't know why, but the devise/sessions folder was missing, and it was doing it with another view, and when I started using it, it worked.
Thanks anyway.
I'm trying to automate my database population with sample objects, so I use rake tasks. I would like to use some data from other website pages.
I created txt file with url list from where that information supposed to be read and ran into problem: I don't know how to make requests outside from rake task. I need to get response, extract some selectord as RSPEC allows to do. Thank you, guys!
For custom rake tasks, you can always create your own plugin. Refer rails guide - plugins section.
In the plugin, you can use get method to get the data from an external URL. Refer ActionDispatch::Integration::RequestHelpers methods for the details.
You can add your own extraction logic and seed the database.
so, finding tutorials around the internet provides some of the different types of scaffold available, like:
$ rails generate scaffold Post
$ rails generate scaffold Club
(Answered)1)where can i find a list of the scaffolds available to a standard rails 3 install? possibly with descriptions/screenshots?
(Answered)2)is there a recommended place for community provided scaffolds?
3)what data types can i use for the variables created with scaffolding? for instance, is there a date datatype? float? any ruby datatype?
4)where do i go to find this documentation? i've been searching for info on scaffolding w/o much success, what should i be looking for?
The stuff that goes after scaffold is the name of the model you want to create and optionally its properties. The rails scaffold generator will then create a Model class, Migration, Controller, basic Views and Route entry. So in your two examples it will create scaffolding for a model called Post and another one for a model called Club. You can name these whatever you want e.g.
rails generate scaffold MyModel.
Best resource to start is the getting started guide:
http://guides.rubyonrails.org/getting_started.html
Just starting to learn Ruby on Rails. I'm using RoR 3. I have read this: http://guides.rubyonrails.org/association_basics.html
But I want to make sure I understand completely.
When creating a new model (I'm doing via scaffold for now), should I specify foreign_key fields at that point, or does the association handle that completely? I believe that association is only at app level, not at the db level, correct?
So I think I must do:
rails generate scaffold post body:text title:string user_id:integer
So in summary, when creating a blog application, must I specify the user_id field in the post model, or does the user model's has_many :posts take care of actually adding that to the db (mine is mysql) when I migrate?
And if the answer is that I should do them when I create the model in the first place (via scaffold or by hand), what happens when I decide later on that I want to add a foreign key, must I add that as an execute statement in a new migration?
You're correct. You need to specify the foreign key when you create your scaffold/model/migration as you stated to get the DB to be correct, and the has_many takes cares of the model for you.
So for initial generation of a scaffold (or model), just do:
rails generate scaffold post body:text title:string user_id:integer
as you stated, and add the has_many for the model itself.
For additions later on, you would make up a new migration, something like (assuming you want to use generation, but you could write your own migration):
rails generate migration add_user_id_to_posts user_id:integer
With that, you can run a rake db:migrate, and then update your model with a has_many or whatever association you need.