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.
--
Posted via http://www.ruby-forum.com/.
Here is the link containing the code and an image of the form:
https://gist.github.com/a3676da061799ac6e29d
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.
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.
E.G:
class Address < ActiveRecord::Base
has_many :businesses
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
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
Did you mean that? Table categories has a column category? So then
you have, for example, @category.category
Colin
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: