Solr Sunspot Autocomplete Text Field Not Working - ruby-on-rails

I am using the sunspot_autocomplete gem to implement auto suggest to my search bar. Just need some help creating the text field. I am using rails 3.
Before I installed the gem, my search bar's code looked like this:
<%= form_tag dairies_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search], style:"width:550px; height:30px;", :autofocus => true %><br>
<%= submit_tag "Search!", :name => nil, class: "btn btn-primary btn-lg", style: "margin-top:20px" %>
</p>
<% end %>
The autocomplete gem requires this code for a text field:
<%= autosuggest_text_field "dairy", "title", "http://127.0.0.1:8983/solr/", "dairy_title"%>
My question is do I replace all of the search bar code with that ^ or do I just replace the text_field_tag line? I have either been getting errors, or the entire code shows up on my website. Sorry for the beginner question. Still lots to learn! Thank you for your help.

Related

Ruby on Rails - search results to show up on the current page

I have a search bar available on every page of the webapp by adding
<body>
<%= form_tag(class_data_index_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search your major" %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<%= yield %>
in application.html.erb.
When I search, it shows the results, but it also navigates to /class_data?search=SearchKeyword. I want the results to show up on the current page, whether it'd be /home, /anothermodel.
How can I achieve this?
You want to use a remote form with <%= form_tag remote: true %> - see Rails form_tag remote example as an example and a starting point.

Passing variable from view to controller Rails 4

I want to pass one variable which is introduced by the user in the front-end. I dont use any model (i dont need it, because im working with JSON data all in memory). I have looked many tutoriasl but almost all of them are focused on filling out a form. My application does not have any form nor tables. Any idea?
Thank you.
{<div class="module1">
<p>Mein Lastprofil berechnen</p>
<div class="boxed">
Jahreshausverbrauch (kWh)
<%= text_field_tag "input", nil, placeholder: "3500" %>
<%= button_to "Senden", root_path, :method => :get %>
</div>
</div>}
I want to save/pass the variable introduced when clickling the button.
By having a text field you implicitly already have a form.
Make it explicit by wrapping the text field and button:
<%= form_tag root_path, method: :get do %>
<%= text_field_tag :input, placeholder: "3500" %>
<%= submit_tag "Senden" %>
<% end %>
Then you can access the value as params[:input] in the controller.

Displaying countries using country_select gem in Rails 4

I am trying to implement the country_select gem (gem 'country_select','~> 2.1.0') in a Rails 4 app without success. This is the code in my form. I have tried several permutations of this from similar questions on stack without success. I don't understand why I am getting the error -
"undefined method `input' for ActionView::Helpers::FormBuilder:0x007fafb5878c48>"
Bear in mind that I am a Rails beginner.
<%= form_for(#user, html: { multipart: true }) do |f| %>
<%= f.label :country %><br>
<%= f.input :country, as: :country %>
<%= f.submit "Update my account", class: "btn btn-primary" %>
<% end %>
I have also tried
<%= f.label :country %>
<%= f.country_select :country %>
from the documentation. 'country' is one of my user attributes.
Any guidance would be welcome on the newbie problem.
I have just spent an hour on the same problem and am annoyed at the brevity of the github documentation. Nevertheless, this worked for me and it sounds like what you need:
<%= f.country_select(:country, {selected: #user.country}, {class: "form-control"}) %>
I couldn't get the second parameter (see https://github.com/stefanpenner/country_select) to work - it gave me an error every time I tried to add anything else before the {selected...}.
<%= f.country_select :country, {priority_countries: ["SG", "US"], include_blank: "select country"}, class: 'form-control' %>
This worked for me, the first :country is an attribute of user, all the options of country_select gem will come as the next parameter and the last param will take the class name and other format features.
If the param contains a single field it can be passed with or without brackets.

Include link in form label?

I'm looking to include a link in a form label as such:
<%= form.check_box 'eula' %>
<%= form.label 'eula', "I agree to the <a href='#' id='eula-link'>EULA</a>", class: 'label-checkbox' %>
Rails writes the HTML out, as it probably should, but how would I accomplish this? Clicking EULA opens a JS popup. I was thinking of embedding a link_to in there somehow ?
Using html_safe with parens will render the html, like so:
<%= f.input :eula, :as => :boolean, label: ("I agree to the #{link_to 'Terms of Service', terms_path}.").html_safe %>
Assuming you're using vanilla rails form helpers, you can do this:
f.label :eula do
'I agree to the #{link_to("EULA", "#")}'
end
Source: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-label
19 ways tried, with either the hyperlink being encoded or html_safe replacing hyphens in the url ???
This is what worked for me
<%= f.label :cookies do
"Do you agree to our #{link_to('Cookies Policy', 'http://www.your-­url.co.uk/privacypolicy')}".html_safe
end %>
The specific use of " and ' appears significant.
The answer from jenson-button-event nearly worked for me, but required a change in the placement of a parenthesis close to load without errors.
For me the below solved it. Note the close-bracket after 'Cookies Policy' here, rather than after the link path itself.
<%= f.label :cookies do
"Do you agree to our #{link_to('Cookies Policy'), 'http://www.your-­ url.co.uk/privacypolicy'}".html_safe
end %>
try "I agree to the #{link_to 'EULA', #, :id => 'eula-link'}"
I wanted a simple way to add a font awesome help link button after the form field label, this is what I used in my haml file:
= form_for [#preplan, #structure] do |f|
= f.label :template do
Template
= link_to 'https://intercom.help/blazemark/preplans-and-structures/structure-templates', target: '_blank' do
= fa_icon 'fw info-circle'
As of rails 6.0.2.1 (January 2020), this is what worked for me:
<div class="form-group form-check">
<%= form.check_box :accept_terms, class: "form-check-input", required: true %>
<%= form.label :accept_terms, class: "form-check-label" do %>
<span>
Accept <%= link_to 'Terms and Conditions', 'https://your.url.here.com' %>
</span>
<% end %>
</div>

Hide a submit_tag in Rails 2.3 by default

I wanted to have the submit_tag in rails 2.3 hidden by default i.e. get style="visibility:hidden" in the html output for the submit_tag. I searched the APIs but wasn't of any help. I tried sth. like
<%= submit_tag 'Save', :id=>"submitBtn"+i.to_s, :visibility=>"hidden"%>
just in case hoping that it might work. But it didn't work. :-(
try <%= submit_tag 'Save', :style => "display:none" %>

Resources