Amending partials

2 views
Skip to first unread message

Sam

unread,
Feb 9, 2010, 8:19:49 AM2/9/10
to Ruby on Rails: Talk
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.

Sharagoz --

unread,
Feb 10, 2010, 5:34:52 AM2/10/10
to rubyonra...@googlegroups.com
It would help if you show us the form(s)

--
Posted via http://www.ruby-forum.com/.

Sam

unread,
Feb 11, 2010, 5:36:58 AM2/11/10
to Ruby on Rails: Talk
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.

Sharagoz --

unread,
Feb 11, 2010, 6:34:08 AM2/11/10
to rubyonra...@googlegroups.com
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/

Sam

unread,
Feb 12, 2010, 6:32:01 AM2/12/10
to Ruby on Rails: Talk
Hi thanks for the info.

Here is the link containing the code and an image of the form:
https://gist.github.com/a3676da061799ac6e29d

Sharagoz --

unread,
Feb 12, 2010, 7:29:47 AM2/12/10
to rubyonra...@googlegroups.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| %>
<p>
<b>Business name</b><br />
<%= fields.text_field :business_name %>
</p>

<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.

Sam

unread,
Feb 12, 2010, 9:10:16 AM2/12/10
to Ruby on Rails: Talk
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: </p>
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:in `new'

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.

Sharagoz --

unread,
Feb 12, 2010, 10:54:59 AM2/12/10
to rubyonra...@googlegroups.com
Can you show us how the model associations for Business, Address and
Category?

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

Sam

unread,
Feb 16, 2010, 5:32:58 AM2/16/10
to Ruby on Rails: Talk
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

Sharagoz --

unread,
Feb 16, 2010, 5:49:41 AM2/16/10
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Feb 16, 2010, 6:55:12 AM2/16/10
to rubyonra...@googlegroups.com
On 16 February 2010 10:32, Sam <samir_...@hotmail.co.uk> 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

Sam

unread,
Feb 18, 2010, 9:35:23 AM2/18/10
to Ruby on Rails: Talk
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:

<h1>New business</h1>

<%= error_messages_for :business %>

<% form_for(@business) do |f| %>
<p>
<b>Address</b><br />
<%= f.text_field :address_id %>
</p>

<p>
<b>Business name</b><br />

<%= f.text_field :business_name %>
</p>

<p>
<b>Business email</b><br />
<%= f.text_field :business_email %>
</p>

<p>
<b>Business telephone</b><br />
<%= f.text_field :business_telephone %>
</p>

<p>
<%= f.submit "Create" %>
</p>
<% 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?


On Feb 16, 11:55 am, Colin Law <clan...@googlemail.com> wrote:

Reply all
Reply to author
Forward
0 new messages