Rails 4 gem rails4-autocomplete 1 letter suggestions - ruby-on-rails-4

I am using gem rails-jquery-autocomplete, but It shows suggestions only after 2 letters, is there a possibility to display suggestions for 1-letter long searching?

Yes there is! Just add 'min-length' => 1 in your search form like:
<%= form_tag terms_path, method: 'get' do %>
<%= autocomplete_field_tag :search, params[:search], autocomplete_term_phrase_terms_path, 'min-length' => 1 %>
<%= submit_tag "Search" %>
<% end %>

Related

No route matches [POST] "/basic_pages/basic_pages/home"

So I would simply like to post to my root. My problem is that if I try to do it the second time (first time works fine) I am posting /basic_pages/basic_pages/home instead of /basic_pages/home. Why does that happen and how do I fix this?
home.html.erb:
<h1>BasicPages#home</h1>
<p>Find me in app/views/basic_pages/home.html.erb</p>
<%= form_tag ('basic_pages/home') do %>
<%= text_field_tag :my_input %>
<%= submit_tag "Send input" %>
<% end %>
routes.rb
Rails.application.routes.draw do
root 'basic_pages#home'
post 'basic_pages/home'
get 'about' => 'basic_pages#about'
end
Hope this provides all the information necessary.
Instead of using form_tag ('basic_pages/home') use form_tag ('/basic_pages/home'). When you have posted the url is getting changed to /basic_pages/home, and then the form is again posting to /basic_pages/basic_pages/home, because of the relative path given to the form.
Try this
Rails.application.routes.draw do
post '/' => "basic_pages#home", as: "root"
get 'about' => 'basic_pages#about'
end
home.html.erb:
<%= form_tag ('/') do %>
<%= text_field_tag :my_input %>
<%= submit_tag "Send input" %>
<% end %>

rails 4 ransack gem, undefined path error

I have rails 4 app with an Events model that I want to make searchable on a variety of fields using the Ransack gem.
my controller action:
def index
#search = Event.ransack(params[:q])
#events = #search.result(distinct: true)
end
my search form in index.html.erb:
<%= search_form_for #search do |f| %>
<%= f.text_field :name_cont, class: "radius-left expand", placeholder: "Event Name" %>
<% end %>
However, in loading the page, I get the following error:
ActionView::Template::Error (undefined method events_path' for #<#:0x007fc08b3fa838>): 1: Events 2: 3: <%= search_form_for #search do |f| %> 4: <%= f.text_field :name_cont, class: "radius-left expand", placeholder: "Event Name" %> 5: <% end %> 6: app/views/events/index.html.erb:3:in_app_views_events_index_html_erb___2056451196739971413_70232473324180'
when I rake routes, I get:
events_show GET /events/show(.:format) events#show
events_index GET /events/index(.:format) events#index
so i assume if I could get this form to point to events_index_path, it would work as expected.
what do I need to do to specify the correct path for the form?
Tbh i am not sure what i did to get this working. here is my controller index action now, which includes a distance/location search feature:
def index
if params[:within].present? && (params[:within].to_i > 0)
#search = Event.near(params[:location],params[:within]).search(params[:q])
else
#search = Event.search(params[:q])
end
#events = #search.result
end

Indicate that an uploaded file is present in edit form -- paperclip

In my current solution, I am able to put a checkbox in the edit form so that users can delete attachment. However, there is no indication for the user that a file has been uploaded, the name of that file, etc. so that he can decide whether to delete.
Right now the form look like this. The first material is an existing one, the next 3 are due to
def edit
#post = Post.find(params[:id])
3.times { #post.post_materials.new }
end
As you can see, it's very hard to distinguish between them. Ideally, I want the first material file name to appear somehow.
<%= form_for #post, :html => { :multipart => true } do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
Materials:
<ul>
<%= f.fields_for :materials, :html => { :multipart => true } do |materials_form| %>
<li>
<%= materials_form.label :asset %>
<%= materials_form.file_field :asset %>
<%= materials_form.label :_destroy, class: "checkbox inline" do %>
Remove attachment <%= materials_form.check_box :_destroy %>
<% end %>
</li>
<% end %>
</ul>
<%= f.submit "Submit", class: "btn btn-large" %>
<% end %>
Running paperclip's generator creates a migration to add 4 attributes on your model, as you can see here. These attributes are:
<attachment>_file_name
<attachment>_file_size
<attachment>_content_type
<attachment>_updated_at
So, If you ran the generator this way: rails generate paperclip post_material asset, on your PostMaterial model, you will have these attributes:
asset_file_name
asset_file_size
asset_content_type
asset_updated_at
Then, on your code you can do something like this:
if materials_form.object.asset.exists? #object represents the current post_material instance
#show a label with object.asset_file_name
else
#render materials_form.file_field :asset
end

ransack search # rails 4

When I am searching for gender field say "male", it searches for "male" and "female" as "male" keywork is in "female" too.
So How can I use ransack in rails so that it matches for exact keyword instead of any partial group of chars.
PS:similarly how can I enable or disable the exact or partial key match in RANSACK gem.
I believe you could just do
<%= search_form_for #q do |f| %>
<%= f.label :gender_eq, "Gender:" %>
<%= f.search_field :gender_eq %>
<% end %>
that will tell the search to look for the exact string of your input
Here is some of the info from the developer on how it's used.
https://github.com/activerecord-hackery/ransack#in-your-view
I believe the way you do your search is <%= (field_name)_cont ... %> cont means contains.
What you want is _eq, change your view into <%= (field_name)_eq ... %> and it should work.
For more info:
https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching

Uploading a file without a form - Rails

Ruby 2.1
Rails 4.1
It should be pretty simple to upload a file without using any gems. I would include in a form something like this:
<% form_for #upload, :multipart => true do |f| %>
<%= f.file_field :uploaded_file %>
<%= f.submit "Upload file" %>
<% end %>
What I want to do is upload the file through a single menu action. How do I go about doing this?