I’m a RoR beginner, and I have a problem with my “app”.
I have a table called ” annonces ” and a table called ” marques “.
(It’s like “post” and “category”, for a blog app)
I’m following the screencast #228 of railscasts, but when I want to sort
the “Marque” column, it sort by ID and not by name…
So the question is, how do I do a sortable table column, with multiple
table?
It’s more “click on a header to sort by that attribute”.
For example, if you click on the name of the column (Marque) this should
sort alphabetical ASC and DESC: AUDI – BMW – VOLKSWAGEN / Re-click :
VOLKSWAGEN – BMW – AUDI.
But for the moment, Rails sort me like this (ID) : 1 – 2 – 3 // 3 – 2 – 1. That render this : VOLKSWAGEN – AUDI – BMW // BMW – AUDI / VOLKSWAGEN.
Like I said, I’m following this screencast :
http://railscasts.com/episodes/228-sortable-table-columns
You can see on this page a thumbail of the result.
But in my case, it’s with multiple table ( Marque, Modele, Energie…)
The screencast work perfectly when everything is in the same table.
But I want to do it with content from other table.
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 […]
Thanks!
++++
Thanks to Ryan, from RailsCasts who “point me into the right direction” 😉
In my annonces_controller:
And I replaced this in the private section :
Thanks!