I'm working with RoR with a legacy database in spanish language.
I have the table and registropersona and model RegistroPersona. Now I want to run rails generate scaffold_controller registro_persona But the generated name of controller should be
RegistrosPersonasController (Note the final s in Registros and Personas).
How can I do this?
Unfortunately, I don't think there is a way to specify the controller name using scaffold_controller without also affecting the model name. I've looked around and haven't been able to find anything that does what you're looking for.
I believe you have two options:
1 - use rails generate controller RegistrosPersonas. The downside is you end up with a blank controller, and you'd have to fill in all of the REST methods yourself.
2 - use rails generate scaffold_controller RegistrosPersonas. This will create the REST methods for you, however all of the model references will use the pluralized name (ie you'll see RegistrosPersonas.all). So the downside is that you'll need to go through the controller and change each reference from RegistrosPersonas to RegistroPersona. Hopefully you should simply be able to use a simple find + replace in whatever text editor or IDE you're using.
Hope that helps somewhat.
You can either edit your config/initializers/infletions.rb file to support that, or just rename the controller manually.
Related
So I think I jacked up my naming conventions (or rather realized I want to do it a different way). I initially created a "Persons" model (which created person.rb). I also created a persons_controller, but apparently rails looks for "People" so I changed the controller name to people_controller.rb (in the files...not in the command line).
I'm new to rails so I really just need to scrap all this and change my names because this setup (having to using Person, Persons, People) throughout the model/controller/views is just a bit confusing for a beginner. All I want to do is change the "Persons" or "People" to the word "Players". So if I were starting from scratch I'd do "rails generate model Player" in the command line, and "rails generate controller Players". But I have no idea how to go about changing my existing controller and model names to this...and I couldn't fully understand some of the older questions related to this topic.
Any help would be greatly appreciated here. Step by Step instruction like your talking to a 12 year old is also highly encouraged given my novice status.
thanks guys,
The basic conventions that will explain everything
About class name and file name
Class name must correspond to file path. In English, if you have a Thing model, it must be in models/thing.rb file ; if you have a ThingsController it must be in controllers/things_controller.rb
Class name with camel case (i.e SomeThing) must be declared in a file with underscore (i.e some_thing.rb). The file name is written in small letters and underscore is used to "separate" the words. Other example: ThisIsEasyToUnderstand will give this_is_easy_to_understand
About model name, table name and controller name
A model name is singular, a table name is plural and a controller is plural. For example a Thing model will have a things table, and will work with a ThingsController controller
Rails try at most to use correct English syntax, so a Person model will work with a people table and a PeopleController controller
You may have trouble when the model name is, in English, the same in its singular and plural form. Ex: aircraft, eyeglasses, scissors etc.. I won't details the solution to keep my answer clear, but know it can append and you can find solutions on those a bit everywhere on internet.
As an overview:
When you create a model, create a name in its singular form
When you create a controller create a name in its plural form
if you need to rename models or controller you already created you must rename in your code but also rename the file name
If you need to rename a model you will also need to rename its table, and you need a migration for that (search google for "rails migration rename table" )
Hope it help for your first steps in Rails
You can quickly scrap a model (and its associated files) by calling
rails destroy model [modelname]
Same with controllers, scaffolds, etc.
rails destroy scaffold [modelname]
rails destroy controller [controllername]
Of course, there's no undoing this after you've deleted them, so I'd create the new controllers/models/scaffolds first, migrate any relevant code, and then destroy the useless files.
I come from a Java background, and am very used to wrapping code into objects.
I was working on my first rails application; while writing up the controller, I wanted to wrap a snippet of code that adapted input from the view and modified it to a format that is accepted by the backend.
I was unsure on where to position these objects. Should this "Adaptor" object I created go into the models? (I tried putting it there, but I cannot manage to find a reference to the file using "require".)
Thanks!
EDIT
For example, say my input was a date in a string format: "31st July" -> my controller would be:
def create
adaptor = Adaptor.new
date = adaptor.to_date(params[:date])
...
end
Where would the adaptor live? And if this object were to be "common" code, could it be used by my models?
This is presumably a helper object, and should then go into the helpers folder. It is also possible that this should be part of the model. You should read a bit about the "Fat Model, skinny controller" strategy.
A quick google brought these up:
devinterface.com
goodcoresoft.com
I need to have two (or maybe even more) different create (and update) methods in one controller. I already have views displaying the forms, so I basicaly only need to tell the submit which method to call in the controller. Is that possible? If so, how? Or can I only have one create method and have that method do different things depending on which view called the method?
Thanks
Ignoring the fact that this sounds like a really terrible idea, it's possible. You will need to add some more routes that will match the new actions in your controller. You won't be able to call them 'create' and 'update' because method names must be unique within the same class.
Having said that, I really beg you to rethink your approach. REST, as described in the Rails Getting Started guide, by far the standard for building Rails applications. If you're not familiar with it, I would recommend stopping where you are and reading up on it. Your application will be much easier to build and maintain, and you won't waste time asking structural questions. If you are familiar with it and are choosing to ignore it, then I wish you the best of luck.
you can use this command:
rails g scaffold_controller 'controller_name'
or if spastic method you can use this:
rails generate controller 'controller_name' add new
Let's say that you have an object Book. You can change values of Book in any method inside of your books_controller.rb as long as that method has access to #book.id.
def crazy_create_method
book.create (book_params)
book.save
end
That being said, try to stick to the default new/create methods and if you need to get weird later on it's always easy to call the code belong in whatever method you need. Rails bakes a lot of out of the box functionality into the default REST actions.
book.title = my_title
book.save
I'm trying to access a model for global system settings. It should be loaded in almost every controller. The problem is I can't find any help on getting rails to load the dam thing.
If this helps, the model is called Config and has two columns; 'key' and 'value'
I come from a background in PHP. I remember codeigniter could load models via.
$this->load->model('a_model_name');
The controllers I'm trying to load the model in do not have the same name as the model.
The problem here is actually the name of the model. Config is used by other Rails classes - see the list of reserved names on the Rails wiki
Rails autoloads classes when it needs them. So long as the class is in a file with the same name, and it's in one of the directories Rails looks for classes in, you just use the class as if it was loaded and Rails will load it automatically
You can add before_filter in ApplicationController:
before_filter :load_config
...
private
def load_config
something
end
By default all controllers in Rails inherits from ApplicationController, so all controllers will execute load_config method at the begining.
By the way I really like rails-settings gem/plugin for configuration. It is probably something that you need. And it loads data when it is needed, so you don't have to load config in each controller, but just when you need some value, then you just do:
Settings.key
and you get what you want.
Presumably, if you want to load a piece of information with a particular key you could use Config.find_by_key('the_key_you_want') to get the record in question.
I'm not sure that this would be the best way to handle your configuration, but it really depends on how your system needs to work.
EDIT:
Didn't see your update to the question before I posted my answer, seems like I misunderstood what you meant. Leaving this answer here in case it's useful, Gareth's answer seems like a fair answer.
I have just started Rails and have a basic question.
I need to add customer properties(like email id etc) so that the Rails app can read them at runtime. How can I do this ?
Can I add them to development.rb and if so how can I read it ?
In java I would have created a properties file and read it from my app.
thank you,
firemonkey
Are you trying to do store and load configuration settings?
It's easy to store configuration settings in a yaml file and load them with initializers - loads better than littering your environment files.
This Railscast: http://railscasts.com/episodes/85-yaml-configuration-file shows you how.
I'm not sure exactly what you are asking. I'm guessing you want an initial set of data in the database that you can access when you actually run the app? If that is so check out this other SO question How (and whether) to populate rails application with initial data
It's a little unclear exactly what you're trying to do, but it sounds like maybe you have a model called Customer and you would like to add some attributes to it, such as email address, id, and so on?
Basically, with Active Record you don't need to do anything special to add a simple attribute (like a string or an integer). Just add a field called "email_address" to your customers table in the database, and all of your Customer objects will automagically get "email_address" and "email_address=" methods (not to mention the Customer class itself getting "find_by_email_address" and other useful methods as well). If you are adding a field containing another model, it's a bit more complicated - add a "something_id" field to the table, and an association to the class definition (eg, "has_one :something"). For more information, see the ActiveRecord api documentation.
You don't have to use any particular means to add the field to your database, but you might want to consider Migrations. Migrations are a convenient way to keep your schema versioned and synchronized across multiple machines.
If you are building your model right now, there's a short cut built in to the generator to add fields. Instead of just saying...
script/generate scaffold customer
...you can say...
script/generate scaffold customer email:string name:string badge_number:integer
...and it will generate all the appropriate fields in your migration, as well as adding them to your generated views.