Amending partials

Hi there

I’m new to ruby on rails and I need some help with amending a view i
created using partials. I need to create a booking form that allows me
to enter a business name, address and category and i created three
different views, tables etc using scaffolding (I did this as I needed
separate tables for normalisation).

The problem is that I now have the form I need to capture the relevent
information but I cannot save the information in one go as there are
three different create buttons. I deleted the code for the buttons but
that does not help as I can only save one part of the form instead of
saving all the information on the form. I have added the relevant
relationships between the tables but I am not sure how I can save all
the information on the form. Apologises if I have not explained this
well, I wish there was a way to show you a screenshot of what i mean.

Any ideas?

Thanks,
Sam.

It would help if you show us the form(s)

Thanks for replying, I’d like to post an image but Iseem to be having
a few issues posting the link on here, I used image shack to upload
the image of the form but I cannot post the direct link or embedded
link, i keep getting an error message saying “We were unable to post
your message”. Not sure what to do.

On Feb 10, 10:34 am, Sharagoz – [email protected] wrote:

It would help if you show us the form(s)


Posted viahttp://www.ruby-forum.com/.

Hi thanks for the info.

Here is the link containing the code and an image of the form:

If the code is too long to copy/paste directly into the post, then you
can create a public gist on github and link to that:
http://gist.github.com/

Here’s what you can do:

First, edit all the form partials and remove the form tags so that only
the fields are left.
I dont know what the relationship between the models are. Is it business
has one category and has one address? If so, the form would look
something like:

<% form_for(@business) do |fields| %>


Business name

<%= fields.text_field :business_name %>

<p>
  <b>Telephone</b><br />
  <%= fields.text_field :telephone %>
</p>

<p>
  <b>Website</b><br />
  <%= fields.text_field :website %>
</p>

<% fields_for(@business.category) do |f| %>
<%= render(:partial => ‘new_category’, :locals => {:f => f}) %>
<% end %>

<% fields_for(@business.address) do |f| %>
<%= render(:partial => ‘new_address’, :locals => {:f => f}) %>
<% end %>

<%= fields.submit(‘Done’) %>
<% end %>

The model design seems weired, but I guess you have your reasons.

I tried the code and I get the following error message:

NoMethodError in Businesses#new
Showing businesses/new.html.erb where line #21 raised:

undefined method `address’ for #Business:0x3220350

Extracted source (around line #21):

18: <%= f.text_field :website %>
19:


20:
21: <% fields_for(@business.address) do |f| %>
22: <%= render(:partial =>‘new_address’, :locals => {:f=>f}) %>
23: <% end %>
24:

RAILS_ROOT: C:/User/InstantRails/rails_apps/gofetch

Application Trace | Framework Trace | Full Trace
C:/User/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/
lib/active_record/attribute_methods.rb:205:in method_missing' app/views/businesses/new.html.erb:21:in_run_erb_47app47views47businesses47new46html46erb’
app/views/businesses/new.html.erb:5:in
_run_erb_47app47views47businesses47new46html46erb' app/controllers/businesses_controller.rb:29:innew’

The relationship between the business, address and catogories is: a
business can only have one address but an address can have one or many
businesses, a business can have one or many categories and a category
can have one or many businesses.

I have already created the relationships and another table to
decompose the relationship between category and business. I’m actually
working on a final year project on how to adapt SCRUM to a one man
team, I have to use ruby on rails as my tutor says it works well with
this methodology, I don’t have much technical aptitude with
programming but I’m pretty good with the SCRUM side of things. The
reason the model design may seem weird is that I was told to use
scaffolding and partials to create this part of the app.

class Business < ActiveRecord::Base
has_many :addresses
has_and_belongs_to_many :categories
validates_presence_of :business_name
end

class Category < ActiveRecord::Base
has_and_belongs_to_many :businesses
validates_presence_of :category
end

class Address < ActiveRecord::Base
belongs_to :businesses
validates_numericality_of :house_number
validates_presence_of :street_name
validates_presence_of :city
validates_presence_of :county
validates_presence_of :postcode
end

Can you show us how the model associations for Business, Address and
Category?

E.G:
class Address < ActiveRecord::Base
has_many :businesses
end

In your previous post you said this:
“a business can only have one address but an address can have one or
many businesses”

Your models are then set up wrong. They should be:
class Business
belongs_to :address

class Address
has_many :businesses

A tip:
validates_presence_of :street_name
validates_presence_of :city
validates_presence_of :county
validates_presence_of :postcode

^ can be reduced to this:
validates_presence_of :street_name, :city, :county, :postcode

On 16 February 2010 10:32, Sam [email protected] wrote:

class Business < ActiveRecord::Base
has_many :addresses
has_and_belongs_to_many :categories
validates_presence_of :business_name
end

class Category < ActiveRecord::Base
has_and_belongs_to_many :businesses
validates_presence_of :category

Did you mean that? Table categories has a column category? So then
you have, for example, @category.category

Colin

I have changed my models after you pointed out they were wrong:

class Business < ActiveRecord::Base
belongs_to :address
has_many :types
has_many :categories, :through => :types
end

class Address < ActiveRecord::Base
has_many :businesses
end

class Category < ActiveRecord::Base
has_many :types
has_many :businesses, :through => :types
end

class Type < ActiveRecord::Base
belongs_to :business
belongs_to :category
end

I haven’t entered the validation in yet, but I will do it later on.
My partial form looks like this:

New business

<%= error_messages_for :business %>

<% form_for(@business) do |f| %>

Address
<%= f.text_field :address_id %>

Business name
<%= f.text_field :business_name %>

Business email
<%= f.text_field :business_email %>

Business telephone
<%= f.text_field :business_telephone %>

<%= f.submit "Create" %>

<% end %>

<%= render :partial=>“new_address”, :locals=>{:address=>Address.new}
%>
<%=
render :partial=>“new_category”, :locals=>{:category=>Category.new} %>
<%= render :partial=>“new_type”, :locals=>{:type=>Type.new} %>
<%= link_to ‘Back’, businesses_path %>

I’ve got rid of the multiple create buttons that were present when I
created the form and I am only left with one create button.
How do make the create button save all the data on the form? Also what
validation method do I use to check that the details I want to save
are not already present in the database?