I have a text field as:
<%= text_field :search, :class=>'input-xxlarge search-query',:id=>'keyword' %>
Then on click of a link I want to pass the value in this text field to a
controller method.
I do not want to use a form and form submit.
I have the link as:
<a href ="/home/search" >GO</a>
and to this 'search' method I want to pass the text field value....
also to go directly to the page "/home/search" designed to this action "search"
How do I do this???
Thank you...
Read here link_to send parameters along with the url and grab them on target page
<%= link_to "Go", '/home/search?param1=value' %>
So if you won't use form, you should use jQuery for put value of field into attribute link (href) with parameter.
Example on jsffidle
<%= text_field :search, :class=>'input-xxlarge search-query',:id=>'keyword' %>
<%= link_to "Go", '', :id => "searchlink" %>
$(':input').bind('keypress keydown keyup change',function(){
var word = $(':input[id="keyword"]').val();
$('a[id="searchlink"]').attr("href","/home/search?param1=" + word.toString());
});
and in controller:
if params[:param1] == ""
render :search # or redirect whatever do you want
else
param = params[:param1]
....
end
In your routes file
resources :home do
member do
get 'search'
end
end
In your html
<div id='parentDiv'>
<%= text_field :search, nil, :class=>'input-xxlarge search-query',:id=>'keyword' %>
<%= link_to("GO", '#', :class => 'search-link')
</div>
In the javascript file
$(document).ready(function(){
$('div#parentDiv').on('click', '.search-link', function(){
var search_val = $('#keyword').val().trim();
if(search_val != ''){
window.location.href='/home/'+search_val+'/search';
} else{
alert('You need to enter some data');
}
});
});
And in your search action
def search
search_value = params[:id]
# your code goes here.
end
Try something like this...(not tested). This will give you the logic.
<%= link_to "Go", "#", onclick: "window.location = \"www.sitename.com/home/search?q=\"+$(\"#keyword\").val()" %>
Related
I am trying to pass an user input into link_to params. I know generally you use forms but here I am not linking these params to a particular model so I just want to access the user input and then pass it to params when user clicks on the link.
Something like this:
Ask for user input - <div><%= date_field :date%></div>
Then pass it in params -
<td><%= link_to "Send", controller_func_path(date: 'date_field value') %></td>
You do not need to link the form to a model to send input data. You can use the form to send the input data as follows:
foo.html.erb
<%= form_with url: boo_action_path do |form| %>
<%= form.text_field :foo %>
<%= form.submit 'Send' %>
<% end %>
foo_controller.rb
class FooController < ApplicationController
def boo
foo_input_value = params[:foo]
end
end
Demir is right, sending data with normal forms is more practical. However, if changing links on the fly is a strict requirement, you can do so with javascript:
<script type='text/javascript'>
function changeLink() {
date = document.getElementById('date_changeable').value;
targetHref = document.getElementById('tochange')
baseLink = targetHref.dataset.baseLink
document.getElementById('tochange').href = baseLink + date;
}
</script>
<%= date_field 'date', 'changeable', onchange: 'changeLink()' %>
<td><%= link_to "Send", 'http://example.com', data: { base_link: 'http://example.com' + '/' }, id: 'tochange' %></td>
I'm trying to implement a two search form_tag on a the same page, each search form is placed inside dynamic bootstrap tabs. The first one which is working is basic a search form with one field. The second one which is not working has two fields, one is the same search method as the first and the other I'm trying to get the address from the other_location field and via params[:other_location].
With the current setup the other_location field form the second form does not appear!
Both of the forms are inside partials and I am rendering them inside two dynamic bootstrap tabs like this:
<%= render 'pages/search' %>
<%= render 'pages/search_other' %>
<%= form_tag search_items_path, :method => "get" do %>
<%= text_field_tag :search, params[:search], autofocus: true,
class: "search-query search_size",
placeholder: "Enter product to search" %>
<%= submit_tag "Search", name: nil, :style => "display: none;" %>
<%end%>
<%= form_for :search_other_path, :method => "get" do |form| %>
<%= form.text_field :search, autofocus: true,
class: "search-query search_size",
placeholder: "Enter keyword to search" %>
<% form.fields_for :other_location_path, :method => "get" do |f| %>
<%= f.text_field :other_location, class: "search-query search_size",
placeholder: "Enter address to search" %>
<%= form.submit "Search", name: nil, :style => "display: none;" %>
<%end%>
<%end%>
model
def self.search(search)
return where("0=1") if search !~ /\w{4}/
where("lower(title) LIKE lower(:term)", term: "%#{search}%")
end
routes.rb
get 'search' => 'pages#search', as: 'search_posts'
get 'search' => 'pages#search_other', as: 'search_other'
get 'search' => 'pages#other_location', as: 'other_location'
controller:
def search_other
if params[:search]
#posts = Post.near(other_location,10).search(params[:search]).page(params[:page])
else
#posts = []
end
end
def other_location
other_location = params[:other_location]
if params[:other_location]
Geocoder.search(params[:other_location])
end
end
def search
if params[:search]
#posts = Post.near(action,10).search(params[:search]).page(params[:page])
else
#posts = []
end
end
On your route file:
get 'search/other' => 'pages#search_other', as: 'search_other'
get 'search' => 'pages#search_other', as: 'search_other_items'
both GET requests are going to your pages_controller.rb #search_other method. So even if you have the two form_tags sending the data to different paths (search_other_path, and search_other_items_path) it would be going to the same controler method - which is redundant.
On your actual HTML you have two form tags:
<%= form_tag search_items_path, :method => "get" do %>
and
<%= form_tag search_other_items_path, :method => "get" do %>
You have not mentioned search_items_path in your routes, so I have no idea where that's pointing to. Likely its a proper controller that works since you mentioned the first form was the only one working.
Now, your mentioned controller only has a search method. So to start you are looking at the wrong controller. You should be looking at the controller methods being referenced by the form's action.
In this case, the second form is sending it's request to search_other_items_path which according to your routes, its pointing to pages_controller.rb -> #search_other method.
You should edit your question to include code that is actually relevant. Maybe then I can actually help.
How do I submit a form (it may be just one field, i will give example in a second) with just 'enter' button, without showing a submit button to the user, or even placing it in a code at all?
right now i have:
= form_tag admin_users_path, :method => 'get'
= text_field_tag :filter, params[:filter]
and after clicking 'enter' it sends me to the Admin::UsersController, but params[:filter] is blank, however my information is displayed by "better_errors" in QUESRY_STRING and REQUEST_URL. Any ideas how can i make this work?
EDIT
Solution WITHOUT form_tag would be very much appreciated, it keep screwing with my css...
Try this:
= form_tag admin_users_path, :method => 'get' do
= text_field_tag :filter, params[:filter]
If you want just a link see this
Solution WITHOUT form_tag would be very much appreciated, it keep
screwing with my css...
HTML sends data through forms, and if you wanted to submit a text_field with enter, you'll have to use a simple form to define both the submission path & which data to send
Therefore, the two ways you can use are either to use JS, or a form to submit the field:
form_tag
= form_tag admin_users_path, :method => 'get'
= text_field_tag :filter, params[:filter]
This will submit with enter, and to fix your css, just amend the styles to work with the form. If you're scrimping on this fundamental html functionality
JS
The other option will be to mimic the submission of an HTML form with javascript:
$('element').on('keyup', function(e) {
if (e.which == 13) {
$.post(url, { value1: $(this).val() } );
}
});
<%= search_form_for #q do |f| %>
<%= f.search_field :title_cont, placeholder:"Search" %>
<%= f.submit %> <-------(1)
<% end %>
you just need to remove the "=" sign form the arrowed line or line (1) like
<% f.submit %>
I have a form_for, which has an input text field, the inputed text is sent to a controller which is doing a search on a table from db.
But I would like to do something different, instead of inputing the text in a text field, I want to create default search queries. Which will be buttons with text, and based on that text a param will be sent to the controller and do the search.
How can I do this? I am using Rails 4
My form_tag
<%= form_tag pos_project(#project), method: :get do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
What I want to achieve:
Button:(text in the button) -> MyButton
When clicked on this button, the text MyButton will be sent to the controller, the controller will take the param, and query the db table and display the results which contain the text MyButton using the below query.
where("name ## :q", q: query)
Why not use HTML and Javascript:
<!-- in your view -->
<%= form_tag pos_project(#project), method: :get, id: "your_search_form" do %>
<p>
<%= hidden_field_tag :query, params[:query] %>
<%= submit_tag "Search", name: nil %>
</p>
<% for value in your_preset_values do %>
<%= link_to(value, '#', data: { search_query_value: value }, class: "search-query-link")
<% end %>
And in your coffee.js file:
# A class for the Search form JS
class SearchForm
constructor: ->
# when a search query link is clicked...
$('a.search-query-link').click (e) ->
# find the value from the data attribute
searchQuery = $(this).data('search-query-value')
# assign the value to the hidden field
$("input[type='hidden']#query").val(searchQuery)
# optional - submit the form
$("#your_search_form")[0].submit()
# Called when the document is loaded
jQuery ->
new SearchForm()
You can use "commit" from parameter to find which button is pressed.
So in controller you will get
params[:commit]="button1" # or
params[:commit]="button2" # or
params[:commit]="button3" # based on which button you pressed
I have a search field and button. When the user searches something like "cat" my view renders a bunch of related keywords such as "catnip", "cat toys", "cats" etc... each of these results are links, and are to pass the value of the string displayed back into the search to generate results for the selected link. For example:
User renders search page and searches for "cat"
view page renders results related to "cat" such as "catnip" "kittens"
User now clicks on "catnip"
View page renders results related to "catnip" such as "grass"
Is this possible with link_to? I'm lost and not quite sure what to do...
--
My Code:
SEARCH VIEW PAGE
<% form for(:search, url => search_path) do |f| %>
Search: <%= f.text_field :search %><br>
<%= f.submit "Search Keyword" %><p>
<% unless #keywords.nil? %>
<h3>Keyword Results</h3>
<% #keywords.each do |k| %>
<%= link_to k.name, :controller => "search", :action => "keywords", value => k.name %>
<% end %>
SEARCH CONTROLLER
def keywords
if request.post? then
#keywords = googlesearchapi(params[:search])
end
I want to pass the link_to value that the user clicks on as the :search parameter... thanks in advance for any advice~~
First of all, you want the link to be: "../search/keywords?search=catnip", to do this, modify the link_to as:
<%= link_to k.name, :controller => "search", :action => "keywords", :search => k.name %>
Then, you need to delete the line if request.post? then, otherwise it won't handle the requests coming from link_to, as it is a GET request (not POST).