f.select if empty sends 'placeholder' as its value - ruby-on-rails-4

I am rails 4.2.5 & ruby 2.3.
We are using f.select in a form as:
<%= f.select(:user_id, User.all.collect {|u| [u.name, u.id]}, {include_blank: true}) %>
If I don't select anything & submit the form, its sends 'placeholder' as the select's value.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "users"=>{"100"=>{"user_id"=>"placeholder"}}, "commit"=>"Finish"}
How to remove this 'placeholder' while submitting the form ?
Any suggestions ?

Ok, there was a code on the JS side which was setting 'placeholder' as the value for select. So when removed it worked properly.

Related

Unable to select a field generated by link_to_add_fields with capybara

I have the following element in my view template: <%= link_to_add_fields "Add event", f, :events %>
This opens up a set of fields, which themselves are generated by simple_fields_for. These fields correctly display in the browser running off my local server, so that is not the problem.
However, I can't figure out how to get capybara to fill_in those fields. capybara does not raise an error in clicking the Add event button from link_to_add_fields, but raises the error when I try to fill in the form, saying that it is Unable to find field. Any guesses?
I think you should use id inside your link_to_add_fields and access that id in your capybara fill_in.
<%= link_to_add_fields "Add event", f, :events, :id => 'something' %>
and use id
fill_in "something", with: 'example'

rails4-autocomplete with belongs to association

I want to implement autocomplete via rails4-autocomplete
Rails 4.2.4
Here is the controller
app/controllers/samples_controller.rb
class SamplesController < ApplicationController
autocomplete :patient, :code
Here is the route file,
config/routes.rb
resources :samples do
get :autocomplete_patient_code, on: :collection
end
And that's the view
app/views/samples/_form.html.erb
<div class="field">
<%= f.label :patient_code %><br>
<%= f.autocomplete_field :patient_id, autocomplete_patient_code_samples_path %>
</div>
With this code I mange to get the autocomplete
However I get invalid foregin key error when try to save the sample that's because the patient's code is passed to the foregin key instead of ID. How do I fix this?
Here is the request Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"blabla",
"sample"=>{"patient_id"=>"A123",
"commit"=>"Create Sample"}
Get "/samples/autocomplete_patient_code?term=A12" returns
{"id":"15","label":"A123","value":"A123"}]
After reading the GitHub documentation of rails4-autocomplete, I devised the following solution:
Add attr_accessor :patient_name to your Sample model and modify the form as follows:
...
<%= f.autocomplete_field :patient_name, autocomplete_patient_code_samples_path, id_element: '#patient_id' %>
<%= f.hidden_field :patient_id, id: 'patient_id' %>
...
With this change whenever you select any patient name, that patient's ID will be updated on the hidden field and it will be submitted as patient_id.
Hope this solves your problem.
Source: Github

Must provide source or customer

getting an error with stripe in local testing transfer recipients.
I check SQL lite browser and the user has a stripe recipient ID
I tried clearing my entire database, creating a new listing and then placing an order and getting the same error. not sure of the cause of this error, and help would be great!
Stripe::InvalidRequestError in OrdersController#create
Must provide source or customer.
begin
charge = Stripe::Charge.create(
:amount => (#listing.price * 100).floor,
:currency => "usd",
:card => token
This was a turbo-links issue. I fixed the issue by inputting the following code
<% if user_signed_in? %>
<%= link_to 'New Listing', new_listing_path, class: "btn btn-link", data: { no_turbolink: true } %>
<% end %>

Rails: param is missing or the value is empty because param appearing as "#<Edge:0xb2535b8>"

I'm using form for and the name of the form in the controller is appearing as "#", but I expect it to appear as :edge.
Here's my controller:
def new
#some_stuff
#edge = Edge.new
#some_stuff
end
def create
#edge = Edge.new(edge_params)
#edge.save
end
def edge_params
params.require(:edge).permit(:location_1_id, :location_2_id)
end
View:
<%= form_for( :edge, :url => {:action => 'create'}) do |f| %>
<ul>
<li>
<%= f.label :location_1_id %>
<%= collection_select(#edge, :location_1_id, #location, :id, :record_as_string) %>
</li>
<%= submit_tag(t(:create_edge)) %>
</ul>
<% end %>
Param req:
{"utf8"=>"✓", "authenticity_token"=>"blah",
"#"=>{"location_1_id"=>"91", "location_2_id"=>"92"},
"commit"=>"Create Edge", "action"=>"create",
"controller"=>"admin/edges", "floor_map_floor_id"=>"1"}
So the name of the parameter should be :edge but it's an object that I can't access.
Can anybody tell me what I'm missing?
Any help is appreciated.
You should be using the form block on your collection set like so
<%= f.collection_select(:location_1_id, #location, :id, :record_as_string) %>
(notice that you are calling the collection_select on the block variable f, and NOT passing in the #edge as the first argument).
In addition, because you are creating the object in new (#edge = Edge.new), you should just be using in your form, like so
<%= form_for( #edge, :url => {:action => 'create'}) do |f| %>
(although using :edge wasn't the cause of your problems, I suspect it was because you were using :edge and #edge in the same form. You need to be consistent, use one or the other)

Rails: How to store values dynamically or as cookie

May be the question will sound weird. But as I am doing it very first time, don't know what is it exactly called.
I have a search method with typeahead used in it on homepage of www.mealnut.com (do check a live demo). There are two entries, city and category. City shows data in typeahead array and on submit, gives the search result. Here is a code:
<%= form_tag search_path, :method => 'get' do %>
<%= text_field_tag 'search', nil, :placeholder =>"Enter your city ", :autocomplete => :off, :'data-provide' => "typeahead", :id=>"citysearch" %>
<%= select_tag :category, options_from_collection_for_select(#categories, "id", "category", params[:category]), html_options = { :prompt => 'Select a category'} %>
<%= submit_tag "Search"%>
<% end %>
And here is typeahead script:
$(document).ready(function(){
var cities = ["Bengaluru", "Pune", "New Delhi", "Delhi", "Mumbai"
$("input[data-provide='typeahead']").typeahead({source: cities});
});
And here is issue: when user clicks on back button to return on home page, city gets wiped out and category remains as selected. Here user should see city, he entered last time instead of blank field.
I think this happens cause typeahead just gives array and doesn't store value anywhere. I dont want to create separate table for array.
i found cookie might be the option. But dont know how to go with it as never tried cookie. Also, what is the best solution on this cookie or anything else? Has anybody done this?
Please help!
Update:
I used cookie for one city in controller. My home controller's index action is:
def index
#categories = Category.all
cookies[:search] = "Mumbai"
end
And modified index page form for search to:
<%= text_field_tag 'search', nil, :placeholder =>"#{cookies[:search]} ", :autocomplete => :off, :'data-provide' => "typeahead", :id=>"citysearch" %>
This shows city Mumbai on back button click. But i dont want city to be fixed. It should be user entered value. Can anyone tell how to do it?