Hi my question is relating to Controllers or I think Models. I have a
site that allows a user to create one “Company” profile like a
superuser account. From which that user can then create several
Businesses under that one company account/name. I’ve created the
Controller for the Company and for the Business, but I don’t know how
to create different types of Businesses on or in the Business
Controller. How would I go about doing this, so that when the user
selects for example, “Retail Store” and then presses create new
Business, that a new Retail Store is created under Business. Also
under the newly created Retail Store the user can add product items,
so i think this might need to be a controller too, not sure.
Sorry for an confusion, but this is why I think it might be done
through Controllers or Models.
COMPANY # superuser account
BUSINESS # here is where the user enters info about the
business and selects the business TYPE e.g. Retail Store
RETAIL STORE # this is the newly created business,
determined by the user when creating the Business, when the user
selected the Business type, e.g. Retail Store
At least as a first step, I think I would have independent models for
Company, Business and Product, each in a has_many/belongs_to
relationship w/the ones next to it. Business should have say, a
business_type field, which is where you distinguish between retail
stores & factories & whatever other types you’ve got.
On your companies/show view you could have a “Add Business” button & a
business_type select list, which would call /businesses/new, passing it
the id of the Company it was invoked from & the type of business the
user wants to create (which just gets stored in the business_type field.
Products could work similarly.
In case it isn’t obvious, that’s models, controllers and views for
each of your three things there. If you’re just starting out, try
generating scaffolds for each of these & then modify the generated
files.
This helps, but I’m wondering after the user selects the business_type
and presses create new business, can I have a different view for each
type? So that they can fill-out the relevant information for that
particular business_type. Would it also be possible for you to show me
and example, if not perhaps a link to some examples.
Sure, I think that could work. (You might also want to start googling
for “rails single table inheritance” if you want to be able to treat the
distinct types of businesses as different ruby classes, tho I don’t know
that this is necessary.)
What I’m picturing is a companies/show view w/code something like:
companies/show
<%= form_tag("/businesses/new") do |f| %>
<%= hidden_field_tag(“company_id”, @company) %>
<%= select_tag(“business_type”, Business.valid_types) %>
<%= submit_tag(“Create new business for #{@company.name}”) %>
<%= end %>
And then your businesses controller has a new action that looks
something like: