Sophisticated scaffolding in Ruby on Rails

I am new to Ruby on Rails. So far I have created CRUD operations using scaffolding. Now I need to combine the two objects into one form, using scaffolding, and not hard coding.

How can we combine two objects together using a script?

I have two objects: Student_address and Student_phnumber. I want to fake these two objects into a single form, where I can perform CRUD operations, and I want to achieve this with forests.

Student_address is an object consisting of Hse_name, Street_name, etc. Student_phnumber is another object consisting of ph_number, type, etc.

I want to combine these two objects together.

+7
source share
3 answers

Forests are nothing more than a generator to model a complete base resource. There are many other generators besides forests that come with Rails by default. None of them are configured to create resources for a set of related models. I suspect that most of this, due to the wide range of methods for expressing this relationship, makes creating a universal user interface nearly impossible. In addition, scaffolding is better suited for quick and quick start-up, with the aim of modifying to suit your needs. These changes are usually fairly associated with any non-trivial application. There are many third-party generators, including the popular great generators , but none of them create the type of code that you want to create, as far as I know. If this is a relationship that you need to set up often, you might consider creating your own generator to handle the task - in fact, it's pretty easy. A good way to do this is to realize your ideal case, and then create a generator from it.

Edit:

In addition, you can customize your variable / attribute names. They must be lowercase and underlined, so Street_name will become Street_name . Arbitrary abbreviations are also very difficult to code / maintain, so Student_phnumber will be better expressed as student_phone_number . The reason for this, besides consistency ( ph_number vs Student_phnumber , for example), is because Rails actually uses the casing and spacing in internal methods like these .

+6
source

Let me see if you understand you.

The model / entity relationship you are describing is as follows:

 student address - house_name - street_name - etc phone_number - number - area_code - etc 

Do you want to:

a) automatically generates models

b) automatically creates a controller / view with a form for creating a student, including fields for setting the address and phone number

Good. b) cannot be done using the Rails overpass. However, to achieve this you can use the ActiveSupport gem ( docs here ). Here is what you do:

 gem install active_scaffold rails g active_scaffold Student name:string rails g active_scaffold PhoneNumber area_code:integer number:integer student_id:integer rails g active_scaffold Address first_line:string second_line:string student_id:integer 

The only manual work that you will need to do is pop up into the model and add the relationship:

 Address belongs_to :student PhoneNumber belongs_to :student Student has_one :address has_one :phone_number 

What ActiveScaffold will do is automatically create for your view:

enter image description here

Fill out this form and your models will be saved and linked together!

+2
source

Is this what you are looking for?

rails generate scaffold Student student_address:string student_phnumber:string

0
source

All Articles