Question

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
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top