I have following entry in my routes for subcategory**(a.k.a. type)** pages:
match '/jewellery/:taxon/(:tag)' => 'shop#index', :as => 'shop'
I also have a set of filters (type, material, plating etc) on the subcategory pages. So, consider a taxon "Earrings". I have following subcategories in this taxon - "Danglers", "Hoops", "Studs".
So, the corresponding subcategory pages are "/jewellery/earrings/danglers", "/jewellery/earrings/hoops" and "/jewellery/earrings/studs".
I want a functionality that if a user goes to sub-category page, the corresponding checkbox in the type filter should be checked. So, if I go to "/jewellery/earrings/studs" the checkbox labelled "Studs" should be checked by default. I want to allow multiple type selection, so input from filters needs to be an array. I have the following code:
<% Typelabels.each { |label| %>
<%if filter[:name] == "Type"%>
<% if (!params["type"].nil? && (params["type"].member? label))
or (!params[:tag].nil? && params[:tag].gsub(/[+-]/," ") == label.downcase)%>
<input type="checkbox" id='<%= label.to_s %>' name="type[]" value="<%= label %>" checked />
<label for="<%= label.to_s %>" class="<%= t %>_name_label"><%= label%></label>
<%else%>
<input type="checkbox" id='<%= label.to_s %>' name="type[]" value="<%= label %>"/>
<label for="<%= label.to_s %>" class="<%= t %>_name_label"><%= label%></label>
<%end%>
<% end %>
<%}%>
But challenge is that when I un-check the box "Studs" (which was checked by default, on "/jewellery/earrings/studs"), I get redirected to the same page as the tag in params is fixed.
Please can someone help me
Related
Hi there. I was wondering how I could link 2 input fields together with an object when submitting a form.
At present I have a food menu that contains many food items. For each item in the menu, I wish to have two input fields linked to it. A check-box to select the item and a number field to enter the quantity of the item required.
When I click submit I would like to be able to access the selected items in the controller to which they were sent to and then be able to display each item in a designated view.
I was able to send the quantity of only a single item through. However, when I entered quantities for many food items, the resulting hash in the params was empty.
Is there any way I could send the item(s) selected + quantities entered as a key-value pair in the params when I click submit.
Edit(1):-
I am currently able to send the food items as a key-value pair but I would like to only send those where the checkbox is ticked.
Selecting food items and entering quantities
Resulting parameters:
{"1"=>{"quantity"=>"2"}, "2"=>{"quantity"=>""}, "3"=>{"quantity"=>"1"}, "4"=>{"quantity"=>""}, "5"=>{"quantity"=>""}, "6"=>{"quantity"=>""}, "7"=>{"quantity"=>""}, "8"=>{"quantity"=>""}, "9"=>{"quantity"=>""}, "10"=>{"quantity"=>""}, "11"=>{"quantity"=>""}, "12"=>{"quantity"=>""}}
Code:
<h2><u>Appetizers: -</u></h2>
<form action="/order/create" method="post">
<input type="hidden" value="<%= form_authenticity_token %>" name="authenticity_token">
<% #food.where(course: 'appetizer').each do |appetizer| %>
<ul>
<li>
<%= appetizer.name %> - <%= number_to_currency(appetizer.price, :unit => "£") %>
<input type="checkbox">
</li>
<label for="<%= appetizer.id %>_quantity">Qty:</label>
<input type="text"
name="<%= appetizer.id %>[quantity]"
size="2"
id="<%= appetizer.id %>_quantity">
</ul>
<% end %>
<input type="submit" value="Submit">
</form>
<br>
<%= link_to 'Back', menu_path %>
You can get quick answers, if you include a code snippet. Coming to your question -
Have you used the array notation for the input element names?
<input type="text" name="textbox[]" />
Using this you can get two arrays - one array for checkbooks and other for text boxes.. this should be equivalent of having a map.
See this
Post which talks about similar problem.
Hello I want to show the search form inside the navbar only in the index page of products and the show page of the product. The thing is that it is showing on both the index page and the show page but when I click home to go to the root_path I get the following error:
No route matches {:action=>"show", :controller=>"products"} missing required keys: [:id]
How can I avoid that? This is my code in the application.html.erb:
<% if current_page?(products_path) || (product_path) %>
<div class="col-sm-3 col-md-3 pull-right">
<form class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="search_term" id="search_term">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</div>
<% end %>
It would be better to just check the current controller.
<% if controller.controller_name == 'products' %>
You also have access to action_name if you wanna use that too
<% if controller.controller_name == 'products' && controller.action_name == 'show' %>
You can check for the product controller and action in order to achieve that. Checkout the below code.
if controller.controller_name == 'products' && %w(index show).include?(action_name)
For the action name in different ruby version have a look at Rails - controller action name to string
This will check for the product controller and both actions.
I am trying to use a simple html5 form in Ruby on Rails. My fieldset code:
<fieldset class="form-group">
<legend><%= question.title %></legend>
<% question.answers.each do |answer| %>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input" name="question-<%= question.id %>" value="answer-<%= answer.id %>">
<%= answer.title %>
</label>
</div>
<% end %>
</fieldset>
is displayed correct, and when I select one or several options and submit, I have only one parameter in the request as it were radiobutton form although I selected several variants:
{"question-162"=>"answer-467"}
How to make this form working correct and send multiply parameters in the submit request?
Change the name attribute of the checkbox to question-<%= question.id %>[] (adding trailing []), and you will get request parameters like below:
{"question-1"=>["answer-1", "answer-3"]}
I'm trying to create a form that will save the current_user.id. Something is funky in my controller. Any thoughts on obvious issues?
View (new.html.erb)
<!-- Label and input for user_id -->
<div class="form-group">
<label for="user_id" class="control-label">
User
</label>
<%= current_user.id %>
<input type="hidden" name="user_id" value="<%= current_user.id%>">
</div>
Controller (favorites_controller.rb)
def create
#favorite = Favorite.new
#favorite.dish_comment = params[:dish_comment]
#favorite.user_id = curent_user.id
d = params[:dish_id]
r = params[:restaurant_id]
problem code:
#favorite.user_id = curent_user.id
I thing you should put an # in front of your current_user to make it accessible in the ERB and in the controller.
Plus, once you save the current_user, you should user params[:user_id] to match the erb (input name is user_id).
Please look at the rails server output, it will tell you what parameters are submitted in the request.
Thanks #jackhaskeyboard. I spelled "current" wrong.
I'm now getting the following errors: Add Favorite Error Messages
1. "User has already been taken"
-A user should be able to post unlimited favorites, so I"m not sure why this is occurring.
"Dishing can't be blank"
-The purpose of this form is to select two variables (dish & restaurant), search the dishing join table to see if that combination exists. If not, create one, if so, grab the dishing_id, and use it to create a new favorite entry (user & dish/restaurant)
Here's my view code:
<div class="row">
<div class="col-md-12">
<form action="/create_favorite" method="post">
<!-- Hidden input for authenticity token to protect from forgery -->
<input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>">
<!-- Label and input for user_id -->
<div class="form-group">
<% current_user.id %>
<input type="hidden" name="user_id" value="<%= current_user.id%>">
</div>
<!-- Label and input for dishing_id -->
<div class="form-group">
<label for="dishing_id" class="control-label">
Dish
</label>
<%= select_tag(:dish_id, options_from_collection_for_select(Dish.all, 'id', 'dish_name')) %>
</div>
<!-- Label and input for restaurant_id -->
<div class="form-group">
<label for="restaurant_id" class="control-label">
Restaurant
</label>
<%= select_tag(:restaurant_id, options_from_collection_for_select(Restaurant.all, 'id', 'name') ) %>
</div>
<button class="btn btn-success">
Create Favorite
</button>
or
Cancel
</form>
</div>
</div>
I have this formtastic field in a form where an user input his credit card data:
<%= form.input :credit_card_type, :as => :radio, :collection => User.credit_card_types %>
User.credit_card_types look like this:
def self.credit_card_types
{'Visa' => VISA, 'Master Card' => MASTER_CARD, 'American Express' => AMERICAN_EXPRESS}
end
Which give me the following html:
<li id="user_credit_card_type_input" class="radio optional">
<fieldset>
<legend class="label">
<label>Tipo de tarjeta</label>
</legend>
<ol>
<li>
<label for="user_credit_card_type_1">
<input type="radio" value="1" name="user[credit_card_type]" id="user_credit_card_type_1"> Master Card
</label>
</li>
<li>
<label for="user_credit_card_type_0">
<input type="radio" value="0" name="user[credit_card_type]" id="user_credit_card_type_0"> Visa</label>
</li>
<li>
<label for="user_credit_card_type_2">
<input type="radio" value="2" name="user[credit_card_type]" id="user_credit_card_type_2"> American Express</label>
</li>
</ol>
</fieldset>
</li>
Given that, my question is, how can I add an specific background image to each radio button label? I can't do it with CSS because the labels have not any id and I can't (or I don't know how) put one to then. I tried to add an image_tag but it escapes the html. Any suggestion ?
Formtastic can be customized for your needs. You should be able to find Formtastic configuration file in config/initializers. Add a line like this:
Formtastic::SemanticFormHelper.builder = SemanticCustomFormBuilder
Then create in your lib directory a file named semantic_custom_form_builder.rb
class SemanticCustomFormBuilder < Formtastic::SemanticFormBuilder
def cicloon_special_radio_button_input
#define your radiobuttons here
end
end
You can use your custom definition like this in your forms:
<%= form.input :credit_card_type, :as => :cicloon_special_radio_button %>