I want to implement dependent fields in my application. For that I'm using the dependent-fields-rails gem. I've set up everything with this tutorial. The good thing: It works. Kind of. In my brand model I have brands listet such as Adidas, Nike, ...
Now my two challenges:
I don't know what I have to fill in to the data-option-value=" " field because I want to show the select form for the category if a Brand is selected at all and not a specific one. If I put a value in the field (hardcoded) like data-option-value="Adidas" the category_select form only appears if I've chosen "Adidas" from the Brand dropdown.
I want to show only the matching categories for the Brand that was selected in step 1. My group model contains also the brand record so I can match a category to the brand.
To make it a bit more clear:
Brand model contains companies like Adidas, Nike, ...
Group model contains categories like Footwear, Shirts, ...
Here's what I got:
<div class="form-group">
<%= f.label :brand %>
<%= f.select(:brand, Brand.pluck(:company).uniq, {prompt:true}, {class: 'form-control'}) %>
</div>
<div class="form-group js-dependent-fields" data-select-id='warehouse_brand' data-option-value="Adidas">
<%= f.label :category %>
<%= f.collection_select(:category, Group.all, :brand, :name1, {prompt:true}, {class: 'form-control'}) %>
</div>
Any Ideas? Thanks a lot in advance!
I assume that a brand has many groups. If you don't want to use hard coded values then put the js-dependent-fields in a loop as shown in the code below.
<div class="form-group">
<%= f.label :brand %>
<%= f.select(:brand, Brand.pluck(:company).uniq, {prompt:true}, {class: 'form-control'}) %>
</div>
<% Brand.all.each do |b| %>
<div class="form-group js-dependent-fields" data-option-value= "#{b.company}" data-select-id="warehouse_brand">
<%= f.label :category %>
<%= f.collection_select(:category, Group.where(brand_id: "#{b.id}"), :brand, :name1, {prompt:true}, {class: 'form-control'}) %>
</div>
<% end %>
To show only the matching categories you can write a query that selects the related to the brand as shown in the code
Group.where(brand_id: "#{b.id}")
this actually works:
<div class="form-group">
<%= f.label :brand %>
<%= f.select(:brand, Brand.pluck(:company).uniq, {prompt:true}, {class: 'form-control'}) %>
</div>
<% Brand.all.each do |b| %>
<div class="form-group js-dependent-fields" data-option-value="<%= b.company %>" data-select-id="warehouse_brand">
<%= f.label :category %>
<%= f.collection_select(:category, Group.where(brand: b.company), :brand, :name1, {prompt:true}, {class: 'form-control'}) %>
</div>
<% end %>
Related
First off, I'm pretty new to Ruby and Ruby on rails, so bear with me.
I'm attempting to have my categories section of my form utilize my category table in my db so that when the author wants to select on a category, they can just select the drop down menu and choose one. For now, I've hard coded them and this works
<%= form_for(#article) do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% #article.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :author_id %><br>
<%= f.text_field :author_id %>
</div>
<div class="field">
<%= f.label :category_id %><br>
<%= f.select (:title), [['Finance'], ['Marketing'], ['Programming'], ['Leisure'], ['Management'], ['Food']] %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
but, by switching the categories section to
<div class="field">
<%= f.label :category_id %><br>
<%= f.select :category_id, Category.all %>
</div>
it will give me a drop down box containing each hash, and I'm not to sure why, but I can't do Category.all.name to get the names (I'm assuming it is because it's in a hash). Any help would be great appreciated.
It looks like that you need to change you select use options_for_select tag:
<%= f.select :category_id, options_for_select(Category.all.collect {|c| [ c.name, c.id ] }), :include_blank => true %>
if you want to pre-populate category_id
<%= f.select :category_id, options_for_select(Category.all.collect {|c| [ c.name, c.id ]}, #article.category_id), :include_blank => true %>
For more information options_for_select
Have you defined the Category model in your Controller? The form can/will only know what the controller knows. It cannot know any more. If this form is attached to the articles_controller then it will have access to #article, but it won't have access to #category.
So this leaves you with a couple options. Define category somewhere in your controller, OR alternatively (and this would be my suggestion) setup some type of association for your Articles model.
Does Article have many Categories? This is a great opportunity to setup that has_many association, and then you can access the category model via Article's associations methods.
This is my suspicion, but we really don't have enough information here. Can you post your controller, and your article model so we can see everything that is going on? Also, what error do you get specifically?
I want associate a Place to an Activity, a Place has the following fields: a country a city, a adress and a name. I want to do something like, as soon as you choose a country the will only and a city, there will only appear (in a dropdown) the Places that have that belong to that city, is there anyway how i can do that? My main problem is the variable :city_name, i know i cant create variables in views like this, but i really dont know how to do it and also how to temporarily save it... Any help would be appreciated :)
This code doesn't work, its just a prototype from what i want to do:
<%= form_for #activity, :html => { :class => "form-horizontal activity" } do |f| %>
<div class="transbox">
<h1><%= "Add Place To activity" %></h1>
</div>
<div class = "box_round">
<div class="row">
<div class="control-group col-lg-10">
<%= f.label "City Name", :class => 'control-label' %>
<div class="controls">
//variable :city_name does not exist
<%= f.text_field :city_name, :class => 'form-control' %>
</div>
<%= error_span(#activity[:resume]) %>
</div>
<% if !:city_name.nil? %>
<div class="control-group col-lg-10">
<%= f.label "City Name", :class => 'control-label' %>S
<div class="controls">
<% place_options = options_from_collection_for_select(#places.where(:city => :city_name).sort { |a,b| a.name <=> b.name }, :id, :name, selected: params[:place_id]) %>
<%= f.select(:place_id, place_options, {prompt: 'Select Sport'},
{prompt: 'Select Place', include_blank: false, class: "Place" }) %>
<%= link_to "Place doesn't exist yet?" , new_place_path %>
</div>
<%= error_span(#activity[:resume]) %>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
What you are looking for is an called an AJAX request. Its a bit much to code out for you, but the gist of it is:
Write JS watcher to look at country field.
On country field change, make a JS call to the server and say: "Give me all the cities in that country"
Return JS that takes the list of cities and overwrites the available options in the city's dropdown field.
You may choose to do the same for Places as well, but the concept/idea is the same. For reference fun, check out this Railscast: http://railscasts.com/episodes/136-jquery-ajax-revised
I'm doing a simple exercise with two models. Sport and Teams, defined as
rails g scaffold sport name:integer
rails g scaffold team name:integer fans:integer sport:references
(Note: The reason I'm using scaffold is rapidly prototyping so I can learn/experiment with the parts I'm not familiar with yet)
Problem is that my "sport" (i.e. the foreign key reference) is showing like the following
So it's got that weird #<blahl blah> notation to it...
<%= form_for(#team) do |f| %>
<% if #team.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#team.errors.count, "error") %> prohibited this team from being saved:</h2>
<ul>
<% #team.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :fans %><br />
<%= f.number_field :fans %>
</div>
<div class="field">
<%= f.label :sport %><br />
<%= f.text_field :sport %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I've tried changing the one line to #team.sport.name but it results in an error undefined method 'Ice Hockey' for #<Team:0x3e7e040>... Any ideas how to properly display name from here??
You are using a text_field for referencing an existing object, a select with Sports as options would be more appropriate here.
This is where it has to be changed:
<div class="field">
<%= f.label :sport %><br />
<%= f.text_field :sport %>
</div>
To:
<div class="field">
<%= f.label :sport %><br />
<%= f.select :sport_id, options_for_select(Sport.all.map{|s|[s.name, s.id]}) %>
</div>
The f.select will generate a select box in HTML, the options will me all the sports in your DB.
Some documentation about it:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
http://guides.rubyonrails.org/form_helpers.html#select-boxes-for-dealing-with-models
A cleaner way would be to set a variable #sports in your controller and call it then in your views:
# in controller
def edit
#sports = Sport.scoped
#...
# in edit view
<div class="field">
<%= f.label :sport %><br />
<%= f.select :sport_id, options_for_select(#sports.map{ |s| [s.name, s.id] }) %>
</div>
Additionnal information: If you want to "pre-select" an option for the select, you have to pass it as the second argument of the options_for_select helper:
options_for_select(#sports.map{ |s| [s.name, s.id] }, params[:sport_id])
# this will select by default the option that matches the value of params[:sport_id]
I have a Model "Startup" and I have a other Model "Categorie". The two tables are associated.
I'd like call the data of Categoria through to form of Startup, this categories displayed with a checkbox. Inside the form Startup Form I have the categorie_id. This is the code
<%= form_for(#startup) do |f| %>
<% if #startup.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#startup.errors.count, "error") %> prohibited this startup from being saved:</h2>
<ul>
<% #startup.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.collection_select :round_id, Round.order(:name), :id, :name, include_blank: true %>
</div>
<div class="field">
<%= f.label :category %><br />
<%= f.text_field :category_id %>
</div>
<div class="field">
<%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: true %>
</div>
<div class="actions">
<%= f.submit %> </div> <% end %>
How to display the data of Categories within form with a checkboxs ?
Any idea.
pdt: My english is really bad.
If a Startup can have only one Category, you can do like this in your view:
<div class="field">
<%= f.label :category %><br />
<%= f.collection_select :category_id, Category.all, :id , :name %>
</div>
This will output a dropdown menu with all the categories. Make sure that the Category model has the attribute name.
As you said, a Startup belongs to one Category, so using radiobuttons (checkboxes are here for multiple relation, means you could choose multiple categories):
<div class="field">
<% Category.all.each do |category| %>
<%= f.radio_button :category_id, category.id %>
<%= f.label :category_id, category.name %>
<% end %>
</div>
You may have to add <br /> tags, and html options to make it looks better.
As MrYoshiji says, you can use:
It will display the category name on the screen, and the value sent will be the id.
You can find more details here : http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
I advise you to use the simple_form gem to generate your form. It's a very simple gem to use and customize ! look at it ;)
I have 5 models,
Person
person_car
car (has many tires)
person_car_tire
tire (belongs to car)
so I have this view
_form.html.erb
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% Car.all.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars) do |ff|%>
Car Name: <%= ff.check_box :car_id %>|<%= c.car_name%>
<% c.tires.each do |b|%>
<div class="field">
<%= ff.fields_for(:person_car_tires) do |fff|%>
Tire: <%#= fff.check_box :tire_id%>|<%= b.tire_name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And when I save it works perfectly, the problem comes when I want to edit using this form because it duplicates data of each car 4 times in the view. I've been searching and fields for allows extra options so I made:
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% #person.cars.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars, c) do |ff|%>
Actividad: <%= ff.check_box :car_id %> | <%= c.car.name%>
<% c.person_car_tires.each do |t|%>
<div class="field">
<%= ff.fields_for(:person_car_tires, t) do |fff|%>
Tarea: <%#= fff.check_box :tire_id%>|<%#= t.tire.name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and it now works, but only show the cars and tires that I've selected on the new action. not all as I wanted (because if I use the first form it duplicates checkboxes in the view).
How can I use the same form for both actions?
Hope someone can help me.
You can use the same _form partial for both new and edit. You just need to pass local variables set to different values to this form. Basically, whatever differs, abstract it away as a parameter (local variable) to this function-like partial.