I have these tables on my database with these fields each one:
->classifieds:(id,title,location,description,email,create_at,updated_at,category_id,content_type,picture)
->categories(id,name)
This is what I have in the followings files:
->classified_controller.rb
class ClassifiedController < ApplicationController
layout ‘standard’
def list
@classifieds = Classified.find(:all)
end
def show
@classified = Classified.find(params[:id])
end
def new
@classified = Classified.new
@categories = Category.find(:all)
end
def create
@classified = Classified.new(params[:classified])
@categories = Category.find(:all)
if @classified.save
redirect_to home_url
else
render :action => ‘new’
end
end
def edit
@classified = Classified.find(params[:id])
@categories = Category.find(:all)
end
def update
@classified = Classified.find(params[:id])
@categories = Category.find(:all)
if @classified.update_attributes(params[:classified])
flash[:notice] = ‘Classified was successfully updated.’
redirect_to :action => ‘show’, :id => @classified
else
render :action => ‘edit’
end
end
def delete
Classified.find(params[:id]).destroy
redirect_to :action => ‘list’
end
end
->category_controller.rb
class CategoryController < ApplicationController
layout ‘standard’
def list
@categories = Category.find(:all)
end
def show
@category = Category.find(params[:id])
end
def new
@category = Category.new(params[:category])
if @category.save
return if request.xhr?
render :partial => 'category', :object => @category
end
end
def delete
@category = Category.find(params[:id])
@category.destroy
return if request.xhr?
render :nothing, :status => 200
end
end
-> app/views/classified/show.rhtml
Mostrar items
<%= @classified.title %>
Price: <%=
number_to_currency(@classified.price)%>
<% if not @classified.category.blank? %>
<strong>Category: </strong> <%= link_to
@classified.category.name,:controller => “category”, :action =>
“show”,:id => @classified.category.id %>
<% end %>
<strong>Location: </strong> <%= @classified.location %> <br />
<strong>Date Posted:</strong> <%=
distance_of_time_in_words_to_now(@classified.created_at) %> ago
<strong>Last updated:</strong> <%=
distance_of_time_in_words(@classified.updated_at, Time.now) %> ago
<%= @classified.description %>
<hr/>
<% unless @classified.picture.blank? %>
<%= image_tag(url_for({:action => 'image', :id => @classified.id}))
-%>
<% end %>
<p>Interested? <%= link_to_function('Contact the
seller’,“Element.show(‘contact_seller’)”) %>
<div id="contact_seller" style="display:none;">
<%= form_remote_tag(:url => {:action => 'contact',:id =>
@classified.id },:html => {:id => “contact_form”}) -%>
Your e-mail: <%= text_field “contact”, “email” -%>
Message:
<%= text_area “contact”, “message”, {:rows => 10} -%>
<%= submit_tag ‘Contact seller’ -%>
<%= end_form_tag -%>
</div>
<%= link_to 'Back', home_url %>
-> app/models/classified.rb
class Classified < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :message => “cannot be blank. Make your
title descriptive”
validates_presence_of :price, :message => “must be a numeric, value
(do not include a dollar sign)”
validates_presence_of :location
validates_presence_of :description
validates_presence_of :email
validates_numericality_of :price
protected
def validate
errors.add(:price, “should be a positive value”) if price.nil?||
price < 0.01
end
validates_format_of :email,:with =>
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i
def pictureimg=(picture_field)
return if picture_field.blank?
self.content_type = picture_field.content_type.chomp
self.picture = picture_field.read
end
end
->db/migrate/001_create_classifieds.rb
class CreateClassifieds < ActiveRecord::Migration
def self.up
create_table :classifieds do |t|
# t.column :name, :string
t.column :title, :string
t.column :price, :float
t.column :location, :string
t.column :description, :text
t.column :email, :string
t.column :created_at, :timestamp
t.column :updated_at, :timestamp
end
end
def self.down
drop_table :classifieds
end
end
->db/migrate/002_create_categories.rb
class CreateCategories < ActiveRecord::Migration
def self.up
create_table :categories do |t|
# t.column :name, :string
t.column :name, :string
end
Category.create :name => “Electronics”
Category.create :name => “Real Estate”
Category.create :name => “Furniture”
Category.create :name => “Miscellaneus”
add_column :classifieds, :category_id, :integer
Classified.find(:all).each do |c|
c.update_attribute(:category_id, 4)
end
end
def self.down
drop_table :categories
end
end
I hope this is enough
Thanks