I want to be able to performa range search with ransack. I was hoping I could do this with a slider that has two thumbs. I'm thinking that the _in predicate would be perfect for a situation like this. However, I cannot find an example on how to appropriately configure this. Here would be some example code:
CONTROLLER `users_controller.rb:
def index
#page_title = 'Users'
#page_icon = 'store'
#q = User.search(params[:search]).ransack(params[:q])
#retailers = #q.result.order("#{sort_column} #{sort_direction}").paginate(page: params[:page], per_page: 20)
end
VIEW users/index.html.erb:
<%= search_form_for #q, url: users_path, class: 'form' do |f| %>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<%= label_tag :search %>
<%= text_field_tag :search, params[:search], class: "form-control" %>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<%= f.label :first_name_eq, "First Name" %>
<%= f.select :first_name_eq, {include_blank: true}, {class: "chosen-select"} %>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<%= f.label :team_eq, "Team" %>
<%= f.select :team_eq, Team.options_for_select, {include_blank: true}, {class: "chosen-select"} %>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<%= f.label :score_in, "Score Range" %>
<%= f.search_field :score_in, {include_blank: true} %>
</div>
</div>
</div>
<div>
<%= f.submit 'Apply Filters', class: 'btn btn-outline-dark btn-md' %>
<%= link_to 'Reset Filters', users_path(q: {reset: true}), class: 'btn btn-outline-danger btn-md' %>
</div>
<% end %>
I guess where I'm lost is how to start or implement a slider w/ two thumbs that would appropriately pass the parameters when submitted. Or even if I just create a two thumbed slider from a js library, how to connect it then to ransack, or at least pass the values of the two thumbs to ransacks params. Another thing that might be helpful is if someone has an example, both in controller/view, of what the _in predicate should look like.
Related
I am just trying to set up a basic application with Ruby on Rails. I have the basic CRUD functions for different entities and with one of them, the form that is supposed to load when the user wants to edit a row does not appear. It knows which ID it needs to edit, it just doesn't load the form.
Link to take you to edit page;
<%= link_to 'Edit Review'. edit_review_path(review) %>
Controller function;
def edit
#review=Review.find(params[:id])
end
HTML Form;
<div class="container">
<h2>Edit Review Details</h2>
<% form_for #review do |f| %>
<div class="form-group">
<%= f.label :"Profile" %><br />
<%= f.select :ProfileId, Profile.all.collect {|x| [x.Name,x.id]}, {:include_blank => 'Select Profile'}, class:'form-control'%> <br /><br />
</div>
<div class="form-group">
<%= f.label :"Product" %><br />
<%= f.select :ProductId, Product.all.collect {|x| [x.pName,x.id]}, {:include_blank => 'Select Product'}, class:'form-control'%>
</div>
<div class="form-group">
<%= f.label :"Review Author" %>
<%= f.text_field :Author %>
</div>
<div class="form-group">
<%= f.label :"Product Rating" %>
<%= f.number_field :ProductRating %>
</div>
<div class="form-group">
<%= f.label :"Review Text"%>
<%= f.text_field :ReviewText %>
</div>
<div class="form-group">
<%= f.label :"Date of Review" %>
<%= f.date_field :DateofReview %>
</div>
<div class="form-group">
<%= f.submit %>
</div>
<% end %>
</div>
I have 2 models that have a working update feature and they are the exact same code as this however for this, when I click the button. The page loads but nothing appears other than the text.
Can someone explain why this is happening? I thought it may be the dropdown boxes but I removed them and still no form loaded up.
One of my tests is creating a form post via javascript that is dropping the first input field from the form. The tests are obviously failing because of this. When deugging via binding.pry, it looks like a js post is dropping the first parameter [address_by_user] when posting it to my route. This parameter is posted just fine when testing via the ui in localhost but no longer works with rspec. Any idea what could cause this? Tests were passing as of two days ago and the forms haven't been changed. I tried restarting my computer in case it was a zombie process or something being cached, but the behavior continues. Any thoughts?
The form that is submitted via javascript using the following code:
var form_id = "#form-step" + currentIndex;
var my_form = $(form_id);
var url = $(form_id).attr('action');
var form_data = my_form.serialize();
console.log(form_data);
var submission = $.post(url, form_data);
The form looks like this and is submitted after clicking the "Next" button in a wizard, that works fine.
<%= form_with(url:"add_property", scope: :property, class: "form", id:"form-step0") do |f| %>
<div class="form-body" id="step0form">
<h4 class="form-section"><i class="ft-location"></i> <%= t(:property_details) %></h4>
<%= render 'shared/error_messages', object: f.object %>
<div class="row">
<div class="form-group pac-card col-md-8 offset-md-1 required" id='pac-card'>
<div class="field" id='pac-container'>
<%= f.label :address_by_user, t(:address), class:"label-control" %>
<%= f.text_field :address_by_user, :required => true, class: "form-control pac-input google-autocomplete", id:"pac-input" %>
</div>
</div>
<div class="form-group form-row col-md-2">
<div class="field">
<%= f.label :unit_number %>
<%= f.text_field :unit_number, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group form-row">
<div class="field col-md-2 offset-md-1">
<%= f.label :bedrooms %>
<%= f.number_field :bedrooms, :required => true, class: "form-control", in: 1...20 %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :bathrooms %>
<%= f.number_field :bathrooms, class: "form-control", in:1...10 %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :accommodates %>
<%= f.number_field :accommodates, class: "form-control", min: "0" %>
</div>
</div>
<div class="form-group form-row">
<div class="field col-md-2 offset-md-1"><%=label_tag(t(:property_type))%></div>
</div>
<div class="form-group form-row" data-toggle="buttons">
<div class="btn-group col-md-12">
<div class="field col-md-2 offset-md-1">
<%= f.label :room_type, class: 'text-center btn btn-default active' do %>
<%= image_tag('house-icon.png') %>
<br>
<%= f.radio_button :room_type, "entire_place", checked: true %>
<span>Entire Place</span>
<% end %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :room_type, class: 'text-center btn btn-default' do %>
<%= image_tag('room.png') %>
</br>
<%= f.radio_button :room_type, "private_room" %>
<span>Private Room</span>
<% end %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :room_type, class: 'text-center btn btn-default' do %>
<%= image_tag('people.png') %>
</br>
<%= f.radio_button :room_type, "shared_room" %>
<span>Shared Room</span>
<% end %>
</div>
</div>
</div>
<div id="hidden-fields">
<%= f.hidden_field(:latitude, id: 'latitude') %>
<%= f.hidden_field(:longitude, id: 'longitude') %>
<%= f.hidden_field(:street_number, id: 'street_number') %>
<%= f.hidden_field(:street_name, id: 'route') %>
<%= f.hidden_field(:city, id: 'locality')%>
<%= f.hidden_field(:state, id: 'administrative_area_level_1') %>
<%= f.hidden_field(:country, id: 'country')%>
</div>
</div>
<% end %>
When tested directly from the site via localhost, all parameters post fine and I get this: (as expected)
<ActionController::Parameters {"address_by_user"=>"Pura Uvita Vacation Home, Puntarenas Province, Uvita, Costa Rica", "unit_number"=>"", "bedrooms"=>"2", "bathrooms"=>"2.0", "accommodates"=>"6", "room_type"=>"entire_place", "latitude"=>"9.1839769", "longitude"=>"-83.72444630000001", "street_number"=>"", "street_name"=>"", "city"=>"Uvita", "state"=>"Provincia de Puntarenas", "country"=>"Costa Rica"} permitted: false>
When the parameters are obtained via binding.pry from the rspec flow, the following is the result:
<ActionController::Parameters {"unit_number"=>"", "bedrooms"=>"2", "bathrooms"=>"2", "accommodates"=>"6", "room_type"=>"entire_place", "latitude"=>"", "longitude"=>"", "street_number"=>"", "street_name"=>"", "city"=>"", "state"=>"", "country"=>""} permitted: false>
The two most likely causes of this are
The fields are no longer actually being filled in - look at the browser while the tests are running and see if the form is actually being correctly filled in.
You have an error in another JS file causing the JS you think is processing the form not to run. This can happen because JS assets are concatenated in the test environment, allowing an error in on JS file to prevent any files concatenated after it not to be processed.
I am trying to initiate a variable showName when the page loads. It worked fine when I did it using just angular but when I implemented it with rails(with embedded ruby tags) I am not able to hide the text field and save button as my variable gets undefined. I have tried couple of ways and ended with no result.
Here is my code. Any help is really appreciated. Thanks.
Edit.html.erb:
<div class="row" ng-controller="EditUserCtrl">
<div class="col-md-6 col-md-offset-3" id="editForm">
<%= form_for(#user) do |f| %>
<h1 id="editHeader">Account Information </h1>
<%= render 'shared/error_messages' %>
<div class="form-group">
<%= f.label :name, "Full Name:" %><br>
<div class="row">
<div class="col-sm-7">
<div ng-hide="showName">
<%= f.label :name, #user.name , id:"editFields" %>
</div>
<%= f.text_field :name, { :'ng-show' => 'showName'}%>
</div>
<div class="col-sm-5" ng-show="showName">
<%= f.button "Save", :action => "update_name" , :method => :put, class: "btn btn-primary", :'ng-click' => "clicked()", id:"editAccount" %>
</div>
<div class="col-sm-5" ng-hide="showName">
<i id="nav-cart-count" class="glyphicon glyphicon-edit"></i> Edit
</div>
</div>
</div>
<% end %>
</div>
Use ng-init to initialize the variable
<div class="row" ng-controller="EditUserCtrl" ng-init="showName = true">
I'm creating a simple form in rails and the submit button doesn't work. I think I'm missing something obvious here but the html seems to be fine (I'm far froma front end expert). Any advice or pointers?
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Apply</h2>
<hr class="star-primary">
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<div class="row control-group">
<% if #user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(#user.errors.count, "error") %>.
</div>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="col-xs-12 floating-label-form-group controls">
<%= form_for #user, url: users_path(#user), :html => {:method => :post} do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: "form-control", placeholder: 'Name' %>
<%= f.label :email%>
<%= f.text_field :email, class: "form-control", placeholder: "Email" %>
<%= f.label :address%>
<%= f.text_field :address, class: "form-control", placeholder: "Address" %>
<%= f.label :phone %>
<%= f.text_field :phone, class: "form-control", placeholder: "Phone Number" %>
<%= f.submit "Apply"%>
<%end%>
</div>
</div>
</div>
</div>
</div>
Also, when the submit button fails, nothing happens. No error messages, no console errors. Nothing.
Take method out from the html hash?
form_for #user, url: users_path(#user), :method => :post do |f|
Try this:
<%= form_for #user, :url => {:controller => "your-controller-name", :action => "your-action-name"} do |f| %>
Can you try this one:
<%= form_for #user, url: {action: "create"} do |f| %>
...
...
<%= f.submit "Apply" %>
<% end %>
But I strongly suggest you to use simple_form gem.
i have this form :
<h1> Global data management </h1> </br> </br>
<h2>Enter the conditions and click "find" option to search for users based on conditions. </br> Or click the "List" option to list all the data. </h2>
</br></br></br>
<%= form_for(:find_field, url: find_field_path , method: :get) do |f| %>
<div class="row">
<div class="span8 offset1">
<div class = "row">
<div class = "span4">
<div class="field">
<%= f.label :data_type_choice, "Data type" %>
<%= f.select :data_type_choice, [["all","all"],["2D","2D"],["3D","3D"],["2D3C","2D3C"],["3D3C","3D3C"]] , :name => nil%>
</div>
</br></br>
<h3> COMPLETED WITHIN </h3>:</br>
<div class="field">
<%= f.label :from_date_choice , "From date " %>
<%= f.date_select :from_date_choice , :name => nil%>
</div>
</div>
<div class = "span4">
<div class="field">
<%= f.label :basin_choice, "Basin" %>
<%= f.select :basin_choice, [["all","all"],["Cauvery","Cauvery"],["KG-PG","KG-PG"],["others","others"]] , :name => nil %>
</div>
</br></br>
<h3> </h3>
<div class="field">
<%= f.label :to_date_choice , "To date " %>
<%= f.date_select :to_date_choice , :name => nil %>
</div>
</div>
</div>
</br></br>
<%= f.submit " Search ", class: "btn btn-large btn-primary" %>
</div>
</div>
<% end %>
everything works perfectly, all the parameters are passed correctly etc, but in the view where im trying to display the result, i get a HUGE url like so:
http://localhost:3000/global/data/find?utf8=%E2%9C%93&find_field%5Bdata_type_choice%5D=2D&find_field%5Bfrom_date_choice%281i%29%5D=2012&find_field%5Bfrom_date_choice%282i%29%5D=7&find_field%5Bfrom_date_choice%283i%29%5D=9&find_field%5Bbasin_choice%5D=KG-PG&find_field%5Bto_date_choice%281i%29%5D=2012&find_field%5Bto_date_choice%282i%29%5D=7&find_field%5Bto_date_choice%283i%29%5D=9&commit=++Search++
ive tried using the ":name => nil" as shown in this railscast
why is this happening and how can i reduce the url size to just "http://localhost:3000/global/data/find" ?
EDIT:
this is my find method in fields_controller:
def find
#data_type_choice = params[:find_field][:data_type_choice]
#basin_choice = params[:find_field][:basin_choice]
#from_date_choice = params[:find_field][:from_date_choice]
#to_date_choice = params[:find_field][:to_date_choice]
end
i also want to display the data entered in the form through find.html.erb like so:
<h1><%= #data_type_choice %> dsfgdfsg</h1>
if i make it a post request, the erb is not being rendered! what can i do?
thanks :)
Just change your method from GET to POST and change the routes accordingly.
Try
<%= form_for(:find_field, url: find_field_path , method: :post) do |f| %>
Your routing file should match the request as POST.