search not working in partial - ruby-on-rails

i have 2 search fields in 2 different views. they both search for establishments and are identical except for styling.
one is in welcome/index.html.erb and works great.
the other is in a partial called _nav.html.erb and is called from application.html.erb and does not work at all.
do i need to pass something into the _nav partial for it to process the search form properly?
welcome/index.html.erb
<div>
<%= form_tag(establishments_path, method: "get") do %>
<div class="form-group">
<%= text_field_tag :search, params[:search],
placeholder: "Enter the name of an eatery", class: "form-control" %>
<div class="actions"><%= submit_tag "Search", :name => nil, class: "btn btn-primary" %></div>
</div>
<% end %>
</div>
_nav.html.erb
<form class="navbar-form navbar-left" role="search">
<%= form_tag(establishments_path, method: "get") do %>
<div class="form-group">
<%= text_field_tag :search, params[:search],
placeholder: "Search", class: "form-control" %></div>
<%= submit_tag "Search", :name => nil, class: "btn btn-primary" %>
<% end %>
</form>
application.html.erb
<%= render 'layouts/nav' %>
EstablishmentsController/index
def index
if params[:search]
#establishments = Establishment.search(params[:search].downcase).order("created_at ASC")
else
#establishments = Establishment.all.limit(25)
end
end

Your form_tag is already wrapped in a form:
<form class="navbar-form navbar-left" role="search">
#^^^^ Here
<%= form_tag(establishments_path, method: "get") do %>
#^^^^ and here again
<div class="form-group">
<%= text_field_tag :search, params[:search],
placeholder: "Search", class: "form-control" %></div>
<%= submit_tag "Search", :name => nil, class: "btn btn-primary" %>
<% end %>
</form>
When you click on the submit tag, it submits the wrong form (the first one).
You should use it like this:
<%= form_tag(establishments_path, method: :get, class: 'navbar-form navbar-left', role: 'search') do %>
<div class="form-group">
<%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %></div>
<%= submit_tag "Search", :name => nil, class: "btn btn-primary" %>
<% end %>

Related

I new in ruby on rails and i don't know how select_tag works

This is my select_tag:
<div class="card-body">
<%= form_tag services_path, :method=> "get", :id => "filter_form" do %>
<div class="form-group">
<%= select_tag :filter, options_for_select([[t('italy'), "it"], [t('world_wide'), "en"]], selected: params[:filter] ? params[:filter] : I18n.locale), class: "form-control" %>
</div>
<div class="form-group">
<%= submit_tag t('research'), name: nil, class: "btn btn-default bg-primary text-light" %>
</div>
<% end %>
</div>
and these are the buttons:
<%= link_to(t('impact_hub'),
external_service_goto_path("impacthub-coworking"),
class: "btn btn-light-primary font-weight-bold m-1",
target: "_blank") %>
<%= link_to(t('my_spot'),
external_service_goto_path("myspot-coworking"),
class: "btn btn-light-primary font-weight-bold m-1",
target: "_blank") %>
I don't know how to show buttons when I select "italy" in select and not show them when I select world_wide

Dropdown select menu reset to default option selection after form submit

When I click the submit button it does filter the selections according to the selected brand but the dropdown option reset to the first option
`
**<%= form_tag(products_path, :method => "get",
class: 'navbar-form navbar-left') do %>
<div class="input-group">
<%= search_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
<div class="input-group-btn">
<%= button_tag "search", :class => 'btn btn-info glyphicon glyphicon-search',:name => nil%>
</div>
<div class="field">
<%= search_field_tag :min, params[:min], placeholder: "min", class: "form-control" %>
</div>
<div class="field">
<%= search_field_tag :max, params[:max], placeholder: "max", class: "form-control" %>
</div>
<% brands_array = Brand.all.map { |brand| [brand.name, brand.id] } %>
<%= select_tag :brand_id, options_for_select(brands_array) %>
</div>
<% end %>**
`

Skyscanner Flight search API with Ruby on rails6

Hi i'm working on flight ticket website(search cheap flight ticket),using Skyscanner Flight API. Basically i copied this API tutorial to implement flight search function. However it only shows blank result in search.html.erb. In tutorial's form page you only fill out country name in form using different API. I'm stuck now and wonder how to implement search action & find_ticket action to take several parameter from index page's form and shows result properly.
Environment Rails:6.02 Ruby:2.65 RapidApi:skyscannerFlightSearch(i'm using RakutenRapidAPI)
Routes.rb
Rails.application.routes.draw do
root to: 'travel#index'
get '/search' => 'travel#search'
resources :messages
end
travel_controller.rb
class TravelController < ApplicationController
def index
end
def search
tickets = find_ticket(params[:country])
unless tickets
flash.now[:alert] = 'ticket not found'
return render action: :index
end
#ticket = tickets.first
end
private
def find_ticket(name)
request_api(
"https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/#{URI.encode(name)}"
)
end
def request_api(url)
response = Excon.get(
url,
headers: {
'X-RapidAPI-Host' => URI.parse(url).host,
'X-RapidAPI-Key' => Rails.application.credentials.rapid[:XXXX_key]
}
)
return nil if response.status != 200
JSON.parse(response.body)
end
end
Views/travel/index.html.erb
<div class="container">
<div class="row">
<div class="col-lg-12 mt-5">
<% if flash[:alert] %>
<div class="alert alert-warning"><%= flash[:alert] %></div>
<% end %>
<div class="mx-auto mt-5" style="width: 400px">
<%= form_with(url: search_path, method: 'get', local: true) do %>
<div class="form-group">
<%= label_tag :country, 'country '%>
<%= text_field_tag :country, nil, placeholder: 'US', class: 'form-control' %>
<%= label_tag :currency, 'currency'%>
<%= text_field_tag :currency, nil, placeholder: 'USD', class: 'form-control' %>
<%= label_tag :locale, 'locale'%>
<%= text_field_tag :locale, nil, placeholder: 'en-US', class: 'form-control' %>
<%= label_tag :originplace, 'originplace'%>
<%= text_field_tag :originplace, nil, placeholder: 'SFO-sky', class: 'form-control' %>
<%= label_tag :destinationplace, 'destinationplace'%>
<%= text_field_tag :destinationplace, nil, placeholder: 'JFK-sky', class: 'form-control' %>
<%= label_tag :outbounddate, 'outbounddate'%>
<%= text_field_tag :outbounddate, nil, placeholder: '2020-02-01', class: 'form-control' %>
</div>
<%= button_tag 'Search', class: 'btn btn-success btn-block' %>
<% end %>
</div>
</div>
</div>
</div>
views/travel/search.html.erb
<div class="container">
<div class="row mt-5">
<div class="col-lg-6">
<h4>Ticket info</h4>
<dl>
<dt>PlaceId</dt>
<dd><%= #ticket['PlaceId'] %></dd>
<dt>lataCode</dt>
<dd><%= #ticket['IataCode'] %></dd>
<dt>Name</dt>
<dd><%= #ticket['Name'] %></dd>
<dt>SkyscannerCode</dt>
<dd><%= #ticket['SkyscannerCode'] %></dd>
<dt>CityName</dt>
<dd><%= #ticket['CityName'] %></dd>
</dl>
</div>
</div>
</div>
</div>

Turbolinks and rails

I can't submit my post without refreshing edit page, I am using turbolinks gem and simple_form here is the source code:
<div class = "row">
<div class = "col-lg-9 col-md-8">
<%= simple_form_for #post do |f| %>
<div class = "form-group">
<%= f.input :title, :label => false %>
<%= f.input :content, :as => :ckeditor, :input_html => { :ckeditor => {:toolbar => 'MyToolbar', :height => 465} } %>
<%= f.input :cat_list, :label => "Type your tags here" %>
</div>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
</div>
<div class = "col-lg-3 col-md-4 ">
<div class = "form-group" data-no-turbolink >
<p>
<%= image_tag(#post.thumbnail.url(:original), :class => "img-thumbnail") %>
</p>
<p>
<%= f.file_field :thumbnail %>
</p>
<p>
<%= f.select :tag_list, Post::Months, { }, { :multiple => true, :size => 10, :class => "form-control" } %>
</p>
<p>
<%= f.button :submit, class: "btn btn-primary" %>
</p>
</div>
</div>
</div>
<% end %>
How can I solve this issue?
Please correct me if I have misunderstood your problem.
By default, Turbolinks works only for GET requests.
So, in your case, you are not using turbolinks to submit the page.
Notice the data-no-turbolink attribute added to <div class = "form-group">
<div class = "form-group" data-no-turbolink >
<p>
<%= image_tag(#post.thumbnail.url(:original), :class => "img-thumbnail") %>
</p>
<p>
<%= f.file_field :thumbnail %>
</p>
<p>
<%= f.select :tag_list, Post::Months, { }, { :multiple => true, :size => 10, :class => "form-control" } %>
</p>
<p>
<%= f.button :submit, class: "btn btn-primary" %>
</p>
</div>
Whenever turbolinks finds a data-no-turbolink attribute, it ensures that all anchor tags within it fallback to the default behavior i.e. ignore turbolinks
The 'edit' page however may have been rendered using turbolinks.

Rails - getting the value out from the view form into a controller

In my form the user has to select a category for his post.
<div class="field form-group">
<%= f.text_field :category, class: "form-control" %>
</div>
(Currently I am using text-input instead of a drop-down list)
Posts belong to categories
#post = #category.posts.build(post_params)
However I can't understand how to get the category value out of that field.
I have tried passing a number, to find_by id, and string to find_by name.
#category = Category.find(params[:category]) #returns no Categoy with nil id
#category = Category.find_by(name: params[:category]) #returns no method error
any help would be appreciated
Edit:
Form code
<%= form_for [#company, #post], :html => { :class => "form-posts"} do |f| %>
<div class="field form-group">
<div class="input-group input-group-lg">
<span class="input-group-addon">$</span>
<%= f.text_field :text, class: "form-control", placeholder: "text", required: true %>
</div>
</div>
<div class="field form-group">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</span>
<%= f.text_field :date,
class: "form-control",
value: #today,
data: {behaviour: "datepicker"},
required: true %>
</div>
</div>
<div class="field form-group">
<%= f.text_field :comment, class: "form-control", placeholder: "Comment (optional)" %>
</div>
<div class="field form-group">
<%= f.text_field :category, class: "form-control" %>
</div>
<div class="actions"><%= f.submit 'Add', class: "btn btn-lg btn-primary" %></div>
<% end %>
Edit 2
controller:
form:
<%= f.text_field :category, class: "form-control" %>
def create
#company = current_user.companies.find(params[:company_id])
#category = #company.categories(params[:category])
#post = #category.posts.build(post_params)
debugger:
{"utf8"=>"✓", "authenticity_token"=>"...", "transaction"=>{"text"=>"lalala", "date"=>"11.11.2013", "comment"=>"", "category"=>"5"}, "commit"=>"Add post", "action"=>"create", "controller"=>"posts", "company_id"=>"2"}
undefined method `transactions'
Take a look at the development log.
Probably it will be something like params[:post][:category]
Get your category like this:
params[:post][:category]

Resources