Help filling out a registration (I think it's called multi tenant)

Actually what I’m looking for is a mix of the multistep form of 217 episode, with a different (and related) model (like 192) for each step, and want them to be saved at same time.

Actually what I'm looking for is a mix of the multistep form of 217 episode, with a different (and related) model (like 192) for each step, and want them to be saved at same time.

I have not looked at that episode (217) recently, but I believe it is building up the data over several steps (in the session) and then saving it at the end. You can use the same technique but at the end save it to your multiple models instead of the single one.

Colin

If I do the same as the 196 episode it works, but I want a has_one and belongs_to relationship, no has_many.

With has_one it doesn’t work. If I set all to singular form (the names of model I mean) I get this error:

NoMethodError in CompaniesController#new

undefined method `build' for nil:NilClass

I got company and company_contact models

Company model:

has_one :company_contact, :dependent => :destroy

accepts_nested_attributes_for :company_contact

CompaniesController:

def new

@company = Company.new

@company.company_contact.build

end

CompanyContact model:

belongs_to :company

views/companies/new:

<% f.fields_for :company_contact do |company_contact_form| %>

  <%= company_contact_form.label 'Nome do responsável por Marketing' %>

  <%= company_contact_form.text_field :marketing_name %>

<% end %>

Could you not top post please, it makes it difficult to follow the thread. Insert your comments at appropriate points in previous message. Thanks.

If I do the same as the 196 episode it works, but I want a has_one and belongs_to relationship, no has_many. With has_one it doesn't work. If I set all to singular form (the names of model I mean) I get this error:

NoMethodError in CompaniesController#new

undefined method `build' for nil:NilClass

I got company and company_contact models Company model: has_one :company_contact, :dependent => :destroy accepts_nested_attributes_for :company_contact CompaniesController: def new @company = Company.new @company.company_contact.build

I have not done this but looking at the docs [1], for a has_one relationship I think you need to do @company.build_company_contact

Colin

[1] ActiveRecord::Associations::ClassMethods

Sorry, I don’t know exactly what top posting is, but I’m replying the last email always, if that’s what you mean

And no, it doesn’t work =/

save each page form values as session variables and only perform the insert to multiple tables on the final page, right?

Hi Colin, Sayuj and Michael,

This is a proprietary, in-house app in Rails 3.1rc4 for the U.S market and they hate date_select ("Date is a little hard to fill out. It is time-consuming to click on "January" and scroll to "June", etc. Any chance I could type in 6/3/11?").

If you want to allow the user to enter dates like this you will have to parse it yourself to make sure the month and day are as you want them. If the site is for an international audience I suggest you use separate fields for the three components as otherwise you will have no end of trouble as most of the world uses day/month/year. I don't understand what you mean by "it happens before the column is available in the controller". The field will be passed as a string from the form to the controller and should appear in params exactly as entered by the user. Note also that, since you have specified a date field in the database, what is stored there is not "2011-07-11" or any other string, but is the date itself.

Colin, you are right that params[:start_date] is exactly as entered in the form.

What I ended up doing was handling it in the controller with: [JobsController]   def create     @job = Job.new(params[:job])     @job.start_date = parse_free_date(params[:job][:start_date])     @job.due_date = parse_free_date(params[:job][:due_date])

and private def parse_free_date(date_str)     case date_str     when /\A(\d|\d\d)\/(\d|\d\d)\/(\d\d)\z/ # "6/3/11" or "06/03/11"       parts = date_str.split('/', 3)       "20#{parts[2]}-#{parts[0]}-#{parts[1]}"     when /\A\d{4}-\d{2}-\d{2}\z/ # "2011-07-11"       date_str     when /\A(\d|\d\d)\/(\d|\d\d)\/(\d\d\d\d)\z/ # "6/3/2011" or "06/03/2011"       parts = date_str.split('/', 3)       "#{parts[2]}-#{parts[0]}-#{parts[1]}"     when /\A(\d|\d\d)-(\d|\d\d)-(\d\d\d\d)\z/ # "6-3-2011" or "06-03-2011"       parts = date_str.split('/', 3)       "#{parts[2]}-#{parts[0]}-#{parts[1]}"     end   end

I'll tighten up the regexes later; they are good enough for a prototype.

Thanks for all your help!

**Leigh

Sorry, I don't know exactly what top posting is, but I'm replying the last email always, if that's what you mean

Top posting is putting your reply above the previous message, as you have done, as opposed to inserting your reply at appropriate points in the previous message as I am doing here.

Could you not top post please, it makes it difficult to follow the thread. Insert your comments at appropriate points in previous message. Thanks.

I tried to make my suggestion clear, but apparently I failed.

Colin

It is no good just saying it doesn't work, what hope has anyone here got of helping further if that is all the information you give? Show us the modified code and the new error message you are getting.

Colin

Something like Date.strptime( params[:job][:start_date], "%Y/%m/%d" ) [ untested ] should do what you want. Don't forget to catch the exception in the case of a parse error.

Colin

Hi Colin, Sayuj and Michael,

This is a proprietary, in-house app in Rails 3.1rc4 for the U.S market

and they hate date_select ("Date is a little hard to fill out. It is time-consuming to click on "January" and scroll to "June", etc. Any chance I could type in 6/3/11?").

If you want to allow the user to enter dates like this you will have to parse it yourself to make sure the month and day are as you want them. If the site is for an international audience I suggest you use separate fields for the three components as otherwise you will have no end of trouble as most of the world uses day/month/year. I don't understand what you mean by "it happens before the column is available in the controller". The field will be passed as a string from the form to the controller and should appear in params exactly as entered by the user. Note also that, since you have specified a date field in the database, what is stored there is not "2011-07-11" or any other string, but is the date itself.

Colin, you are right that params[:start_date] is exactly as entered in

the form.

What I ended up doing was handling it in the controller with: [JobsController] def create @job = Job.new(params[:job]) @job.start_date = parse_free_date(params[:job][:start_date])

Something like Date.strptime( params[:job][:start_date], "%Y/%m/%d" ) [ untested ] should do what you want. Don't forget to catch the exception in the case of a parse error.

Thanks for the exception reminder!

Hi Colin, Sayuj and Michael,

This is a proprietary, in-house app in Rails 3.1rc4 for the U.S market and they hate date_select ("Date is a little hard to fill out. It is time-consuming to click on "January" and scroll to "June", etc. Any chance I could type in 6/3/11?").

If you want to allow the user to enter dates like this you will have to parse it yourself to make sure the month and day are as you want them. If the site is for an international audience I suggest you use separate fields for the three components as otherwise you will have no end of trouble as most of the world uses day/month/year. I don't understand what you mean by "it happens before the column is available in the controller". The field will be passed as a string from the form to the controller and should appear in params exactly as entered by the user. Note also that, since you have specified a date field in the database, what is stored there is not "2011-07-11" or any other string, but is the date itself.

Colin, you are right that params[:start_date] is exactly as entered in the form.

What I ended up doing was handling it in the controller with: [JobsController] def create @job = Job.new(params[:job]) @job.start_date = parse_free_date(params[:job][:start_date])

Something like Date.strptime( params[:job][:start_date], "%Y/%m/%d" ) [ untested ]

I meant "%m/%d/%Y" of course (I said it was untested, it still is.)

Colin

views/companies:

<% f.fields_for :company_contact do |company_contact_form| %>

  <div class='field'>

    <%= company_contact_form.label 'Name of the responsible for marketing' %>

    <%= company_contact_form.text_field :marketing_name %>

  </div>

<% end %>

CompaniesController:

def new

@company = Company.new

@company.build_company_contact

end

Company model:

has_one :company_contact, :dependent => :destroy

accepts_nested_attributes_for :company_contact

This code doesn’t crash or show any error, but it just doesn’t show the CompanyContact information at the form.

BTW, sorry for top posting again =/. I just remembered when I had already sent that last email

views/companies: <% f.fields_for :company_contact do |company_contact_form| %> <div class='field'> <%= company_contact_form.label 'Name of the responsible for marketing' %> <%= company_contact_form.text_field :marketing_name %> </div> <% end %> CompaniesController: def new @company = Company.new @company.build_company_contact end Company model: has_one :company_contact, :dependent => :destroy accepts_nested_attributes_for :company_contact

This code doesn't crash or show any error, but it just doesn't show the CompanyContact information at the form.

So it probably has worked then. Now you have a different problem you need to investigate. Have a look at the Rails Guide on debugging to get ideas on how to proceed. Using ruby-debug to break into your code and inspect data and follow the flow is particularly useful.

Colin

http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

That explains exactly what I did, the one-to-one part, and still nothing

I did it =), thank you everyone, the problem was that I was writing:

<% f.fields_for :company_contact do |company_contact_form| %>

instead of:

<%= f.fields_for :company_contact do |company_contact_form| %>

Thank you very much,

Now I just need help to make this into a multi step form. If anyone have any ideas I appreciate it.

Thank you again,

Rodrigo

I did it =), thank you everyone, the problem was that I was writing: <% f.fields_for :company_contact do |company_contact_form| %> instead of: <%= f.fields_for :company_contact do |company_contact_form| %>

Thank you very much, Now I just need help to make this into a multi step form. If anyone have any ideas I appreciate it.

Hi Rodrigo,

I have uploaded a demo to github:

https://github.com/phollyer/Very-simple-multi-step--multi-model-form

This should give you a place to start. As the title says, it is VERY SIMPLE. There is no error handling, I leave that as an excercise for you, or for anyone else that finds it useful.

I would seriously suggest though, if I may, that you should do some reading - the latest AWDwR book, and the latest Ruby Pickaxe book, both from PragProg wouldn't be a bad place to start. Otherwise, you are going to find yourself continually asking questions.

When you have cloned the repo, just do the following to get it up and running:

bundle install rake db:migrate

That should be all you need, now you can run the cuke features to test, or start the server and try it in your browser.

HTH

Paul

Hi Colin,