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

On 12 July 2011 05:13, Rodrigo R. [email protected] wrote:

It uses a method called ‘build’, which says (to me at least) that it doesn’t
exist.

Show us the exact error and the code around the failing line.

Colin

On 12 July 2011 15:32, Rodrigo R. [email protected] wrote:

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 responsvel por Marketing’ %>
<%= company_contact_form.text_field :marketing_name %>

<% end %>

On 12 July 2011 15:54, Rodrigo R. [email protected] wrote:

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

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]

And no, it doesn’t work =/

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

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?”).

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

On 12 July 2011 19:13, Leigh D. [email protected] wrote:

in the controller". The field will be passed as a string from the
@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.

Colin

On 12 July 2011 16:31, Rodrigo R. [email protected] wrote:

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.

On Tue, Jul 12, 2011 at 12:15 PM, Colin L. [email protected] wrote:

On 12 July 2011 15:54, Rodrigo R. [email protected] wrote:

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

On 12 July 2011 16:32, Rodrigo R. [email protected] wrote:

And no, it doesn’t work =/

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

On 12 July 2011 20:58, Colin L. [email protected] wrote:

understand what you mean by "it happens before the column is available
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| %>

<%= company_contact_form.label 'Name of the responsible for marketing' %> <%= company_contact_form.text_field :marketing_name %>
<% 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

On Tue, Jul 12, 2011, Colin L. [email protected] wrote:

them. If the site is for an international audience I suggest you use
the form.
case of a parse error.

Thanks for the exception reminder!

On 12 July 2011 21:47, Rodrigo R. [email protected] wrote:

@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

Hi Colin,

On Tue, Jul 12, 2011, Colin L. [email protected] wrote:

them. If the site is for an international audience I suggest you use
the form.
case of a parse error.

I refactored it into a lib/utilities.rb module so the testing would be
easier. That seemed like the cleanest approach. And once I added the
exception handling, all the other app tests are now passing and the
client is happy!

Thanks.

*Leigh

On Jul 12, 11:43pm, Rodrigo R. [email protected] wrote:

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:

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