0

Hi maybe this is a fool question, but i really try to look couldnt find any relevant info, (maybe cause i dont know where exactly to start looking at) ..

I need to build a select box with options so the user can "filter" by the candidate.poisition value i have. at this moment the view has this code..

I was thingin on using :params, to post them in URL (i have always do php, im doing rails cause my task now is to insert a design, i achieve everything, just missing a selectbox filter)

Anyway.. here is the code.. wich is pretty simple

    <% @candidates.each do |candidate| %>

      <% if (candidate.active == true) && (candidate.position.present?) %>

All the html code with info.. like 

<%= candidate.name %> : <%= candidate.position? ? t('generales.'+candidate.position) : t('activerecord.models.elite') %>

    <% end %>

    <% end %>

how can i make this , as im really a noob in Rails, im a regular user on PHP not even really good jaja, pls, i hope someone can give me a hand on this.

Can I use something like this : How to do a LIKE query in Arel and Rails?

Like candidates.where("position like '%?%'", params[:query]).to_sql

Also how can i build the Post as you do in PHP , no idea, to change the position that you want to filter.

Model Candidate.rb just have this inside :S

class Candidate < ActiveRecord::Base
end
Community
  • 1
  • 1
Moncho Chavez
  • 694
  • 2
  • 10
  • 31
  • Doing a select that filters a list involves a certain number of steps. For instance: the select itself, the controller action, the JavaScript part if you're doing this with AJAX. It's not clear which of these your question is about. Also, it's not clear how the code snippet you posted relates to the question. – Marcelo De Polli Dec 06 '13 at 23:22
  • I always work in PHP , i also know AJAX but i have no idea of how to implement it on rails.. in PHP yo post a $var that will sent the information to modify query, or condition query. but here, i have no idea – Moncho Chavez Dec 06 '13 at 23:23

1 Answers1

1

Basically you're right. Here is improved code:

<% @candidates.each do |candidate| %>
  <% if candidate.active && candidate.position.present? %>

    <%= candidate.name %> : <%= t("generales.#{candidate.position}") %>

  <% end %>
<% end %>

Update per request in comments:

Use code like this in controller:

if params[:position] == 'red'
  @candidates = @candidates.where(position: 'red') 
elsif params[:position] == 'blue'
  @candidates = @candidates.where(position: 'blue') 
else
  @candidates = Candidate.all
end

and build html form that will pass parameter position.

or you can use https://github.com/activerecord-hackery/ransack

NARKOZ
  • 27,203
  • 7
  • 68
  • 90
  • but that code its already working fine.. i just need to make a select box to filter by candidate position.. im a rails noob, i have no idea of how to do this.. – Moncho Chavez Dec 07 '13 at 00:02
  • this is the closets i get , but i cant make it work with my code.. http://stackoverflow.com/questions/15474883/rails-how-to-filter-results – Moncho Chavez Dec 07 '13 at 00:04
  • Yes! im just looking at ransack already, anyway thanks for the answer just one thing... If i set a URL /candidates?position=position1 this will work right? .... i mean for the selectbox on the view – Moncho Chavez Dec 07 '13 at 00:21
  • but the syntax is fine right domain.com/candidates?position=red – Moncho Chavez Dec 07 '13 at 00:38
  • 1
    yes if form uses get request. you can use `index` action in controller. – NARKOZ Dec 07 '13 at 00:39