In my annonce_controller :

  helper_method :sort_column, :sort_direction
  def index
    @annonces = Annonce.order(sort_column + " " + sort_direction)
    @modeles = Modele.all
    @marques = Marque.order(sort_column + " " + sort_direction)
    @energies = Energy.all

  end
...
...
 private

  def sort_column
  Annonce.column_names.include?(params[:sort]) ? params[:sort] : 
"marque_id"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : 
"asc"
  end
end

For the " def index " I tried :
@annonces = Annonce.order(sort_column + " " + sort_direction)
@marques = Marque.all

and

@annonces = Annonce.find(params[:id])
@marques = Marque.order(sort_column + " " + sort_direction)

In my marque_controller :

class MarquesController < ApplicationController
  helper_method :sort_column, :sort_direction

  def index
    @marques = Marque.order(sort_column + " " + sort_direction)
  end
...
...
  private

  def sort_column
    Marque.column_names.include?(params[:sort]) ? params[:sort] : 
"marque"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : 
"asc"
  end
end

the annonce.rb model :
class Annonce < ActiveRecord::Base
  attr_accessible :marque_id, :modele_id, :price, :type, :energy_id, 
:power
  belongs_to :marque
  belongs_to :modele
  belongs_to :energy
end
the marque.rb model :
class Marque < ActiveRecord::Base
  attr_accessible :marque
  has_many :modeles
  has_many :annonces
end

The view : annonce : index.html.erb
<table>
  <tr>
  <th><%= sortable "marque_id" %></th>
    <th><%= sortable "modele_id" %></th>
    <th><%= sortable "price" %></th>
    <th><%= sortable "power" %></th>
    <th><%= link_to "Energy", :sort => "energy_id" %></th>


<% for annonce in @annonces %>
  <tr>
    <td><%= annonce.marque.marque %></td>
    <td><%= annonce.modele.try(:modele) %></td>
    <td align="right"><%= annonce.price %> €</td>
    <td align="right"><%= annonce.power %></td>
    <td><%= annonce.energy.try(:energy) %></td>
  </tr>
<% end %>
</table>

The view of marque look like this.

My tables looks like that :

Annonces:
    create_table :annonces do |t|
      t.integer :marque_id
      t.integer :modele_id
      t.string :price
      t.string :power
      t.string :energy

Marque:
    create_table :marques do |t|
      t.string :marque

      t.timestamps

I hope this will help...

I'd create all this by doing : rails g scaffold marque marque:string
Or : rails g scaffold annonce marque_id:integer [...]

Thank you again ;)

++++

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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to