i'm using WiceGrid to build a table in users index with some filters, my table shows data from a table called users and a table called states. Those tables are connected by a table called users_states, so the relation between this 3 tables is this:
user: has_many :users_states
user columns: id name
user_states: belongs_to :user belongs_to :state
user_states columns: id user_id state_id
state: has_many :users_states
state columns: id title
my objective is to create a dropdown list filter in my users index table that contains the titles of the states that are populated in the index table. Something like this:
but what i have now is this:
users_controller.rb (index)
def index
@users = current_user.auth_users
@users_grid = initialize_grid(@users, include: [:states, :users_states], per_page: 20)
end
index.html.erb (users)
<h1>Listing Users</h1>
<%= grid(@users_grid, show_filters: :when_filtered) do |g|
g.column name: 'Photo' do |user|
image_tag show_photo_user_path(user), width: '100', height: '100' if user.photo_filename
end
g.column name: 'Name', attribute: 'name' do |user|
user.name
end
g.column name: 'State', attribute: 'state_id', assoc: :users_states, custom_filter: :auto do |user|
user_state_last_title(user)
end
g.column do |user|
link_to 'Show', user
end
g.column do |user|
link_to('Edit', edit_user_path(user))
end
end
end -%>
user_helper.rb
#method for aquiring last state from user
def user_state_last(user)
user.states.order('users_states.id').last if user && UsersState.where(user_id: user).last
end
def user_state_last_title(user)
user_state_last(user).title if user && UsersState.where(user_id: user).last
end
can anyone help me please? thank you very much!
Aucun commentaire:
Enregistrer un commentaire