Jump to content

The ultimate community for Ruby on Rails developers.


Photo

rails habtm select box show categories, then display/filter associated values

ruby on rails habtm ruby categories

  • Please log in to reply
4 replies to this topic

#1 arjunm009

arjunm009

    Signalman

  • Members
  • 12 posts
  • Locationindia

Posted 28 October 2013 - 01:08 PM

in this stackoverflow question Rails views based on HABTM Category , it shows how to display column values of table of one model(users), which is associated to other model(skill), in the controller's(skills) views.

 

i have an application where the end user chooses values from a two select box to display the content.

 

more specifically, i have a places model and an category model, where travel places are associated to certain categories, like sunny, nightlife, etc.

 

now referencing from the above stackoverflow question, i am able to show places related to those categories. but i am unable to modify the categories, which are presented in ul list, to show in a select box.

 

first, this is what i had did

<%= form_for category_path(:id), :html => {:method => :get} do |f| %>
<label>Categories</label>
<%= f.collection_select(:category_ids , Category.find(:all), :id , :name) %>
<%= f.submit "Show" %>
<% end %>

but, when i submit, only the url changes - localhost:3000/categories?utf8=%E2%9C%93&%2Fcategories%2Fid%5Bcategory_ids%5D=1&commit=Show

then i did this.

<%= form_tag ( { :controller => categories, :action => :show, :method => "get" }) do |f| %>
<label>Categories</label>
<%= f.collection_select(:category_ids , Category.find(:all), :id , :name) %>
<%= f.submit "Show" %>

it says, No route matches {:action=>"show", :method=>"get", :controller=>"categories"}

i dont know why, but rails is lying. i do have a controller categories with an action show.

 

so how do i resolve this.

 

and also, how to display two select box, so it contains all the categories from the categories table, but where a value selected from one select box is not shown in the other select box, coz if i select "sunny" or any other from one select box, second one should not show "sunny" or any other based on first.

 

in other words, selection should be unique.

 

FYI: this was the answer to that stackover flow question

In config/routes.rb:
resources :skills

Generate a SkillsController with rails g controller skills and put there:

def index
@skills = Skill.all
end

def show
@skill = Skill.find(params[:id])
end

Then your views:

#app/views/skills/index.html.erb
<ul>
<% @skills.each do |skill| %>
<li><%= link_to skill.name, skill_path(skill) %></li>
<% end %>
</ul>

and

#app/views/skills/show.html.erb
<h1>Users who have the <%= @skill.name %> skill</h1>
<ul>
<% @skill.users.each do |user| %>
<li><%= user.full_name %></li>
<% end %>
</ul>

thanks.



#2 johns276

johns276

    Passenger

  • Members
  • 8 posts
  • LocationNashville, TN

Posted 30 October 2013 - 02:25 PM

Would you be kind enough to post the output from

rake routes

Thanks!



#3 arjunm009

arjunm009

    Signalman

  • Members
  • 12 posts
  • Locationindia

Posted 01 November 2013 - 06:19 PM

hey

 

i was able to solve this. but only issue still remains is how to ensure unique selection from the select box. i believe this may require some ruby(initially) and javascript, where if one selects one value from one of the  select box, the other box does not show that same value and vice versa.

 

select box is being populated from the same category model.

 

can u help me out with this.



#4 Jamie

Jamie

    Controller

  • Moderators
  • 114 posts
  • LocationThe UK

Posted 12 November 2013 - 08:48 AM

hey

 

i was able to solve this. but only issue still remains is how to ensure unique selection from the select box. i believe this may require some ruby(initially) and javascript, where if one selects one value from one of the  select box, the other box does not show that same value and vice versa.

 

select box is being populated from the same category model.

 

can u help me out with this.

 

Would you be able to share how you managed to solve this query so others can learn from it? Then I'm sure others will be more than happy to help you solve your second query.

 

 

:)


Rails developer based in Newcastle, UK.
Web app owner - Twitter lover

#5 arjunm009

arjunm009

    Signalman

  • Members
  • 12 posts
  • Locationindia

Posted 12 November 2013 - 05:17 PM

this is how i managed to get it work. adding unique id's id1 and id2. the is just an id that will show up in the generated form.

<%= form_for :categories, :url => {:action => :show}, :method => "get", :html => { :class => "form" } do |f| %>
<%= collection_select(:c, :id1, @categories, :id, :name) %>
<%= collection_select(:c, :id2, @categories, :id, :name) %>
<% end %>

on submit params would be like so

"c"=>{"id1"=>"2", "id2"=>"1"}

so in your controller u should process it like 

@var = params[:c][:id1]

now i found it rather strange coz in majority of guides about collection_select, the id after @categories was supposed to be modified to get something like this work. i dont know why i have a diff case.

 

anycase this got me up and running.

 

as for the second query, i did get a solution only for bare bones html select boxes:

$("#c_id1").change(function(){
$("#c_id2 option:hidden").show();
if($(this).val().length ){
$("#c_id2 option[value=" + $(this).val() + "]").hide();
}
});
$("#c_id2").change(function(){
$("#c_id1 option:hidden").show();
if($(this).val().length ){
$("#c_id1 option[value=" + $(this).val() + "]").hide();
}
});

but this has flaws, 

 

first - when the page loads, options that are by default selected, the uniqueness only happens when user makes a selection. that is, this select box loads the categories from the same model. so the html form shows the first option by default in both the boxes. but when u actually make the selection, only then this script does it work.

 

second - if you got a pretty css3 drop down select box. the script wont work at all.

 

http://jsfiddle.net/74LFD/9/

 

this is where am stuck at right now.

 

since this is a pure js way, i think the controller and js also can both work together to make it possible. though, i m not sure .







Also tagged with one or more of these keywords: ruby on rails, habtm, ruby, categories

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users