In my view I have
<%= form_tag searches_path, method: 'get', do %>
<p>
<%= text_field_tag :search %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
and in my controller I have
class SearchesController < ApplicationController
def index
raw_result = params[:search]
result = raw_result.gsub(/\s+/, "+")
movie_details = HTTParty.get("http://imdbapi.org/?title="+result+"&type=json")
#searches = ActiveSupport::JSON.decode(movie_details)
end
end
When I visit the view I get a undefined method `gsub' for nil:NilClass. I'm guessing because the form hasn't been submitted. Am I implementing the json parser correctly?
raw_result = params[:search] || "default value"
Related
I'm trying to filter the results of an index page given the content of a search box and whatever is selected on 2 other dropdowns.
I'm using simple_form and I already got the search BOX to work and filter the results with one of the params:
My view:
<%= form_tag(entitys_path, method: :get) do %>
<%= text_field_tag :name_filter, params[:name_filter] %>
<%= submit_tag 'Search' , name: nil %>
<% end %>
My index method:
def index
#entitys = if params[:name_filter]
Entity.joins(:user).where('LOWER(users.name) LIKE?', "%#
{params[:name_filter]}%".downcase).paginate(page:
params[:page], per_page: 8)
else
#entitys = Entity.all.paginate(page: params[:page],
per_page: 8)
end
end
I can't figure out how to make 2 more dropdowns and apply those to the index method filtering.
Thanks in advance.
From a comment, it looks like the question is about how to populate select tags. Here's the methods needed
select_tag
options_from_collection_for_select
So they'll probably look like this:
<%= select_tag(
'location',
options_from_collection_for_select(
get_all_locations,
'name', # The method called to generate the value passed up to the controller
'name' # The value displayed to users
)
) %>
This will populate params[:location] when the form is submitted.
View:
<%= form_tag(entitys_path, method: :get) do %>
<%= text_field_tag :name_filter, params[:name_filter] %>
<%= select_tag :location_filter, options_for_select(get_city_list) %>
<%= select_tag :activity_filter, options_for_select(get_professional_activity_list) %>
<%= submit_tag 'Procurar' , name: nil %>
<% end %>
Controller index method:
def index
if params[:name_filter].present? || params[:activity_filter].present? || params[:location_filter].present?
#entitys = Entity.joins(:user).where('LOWER(users.name) LIKE?', "%#{params[:name_filter]}%".downcase).where('LOWER(entities.location) LIKE?', "%#{params[:location_filter]}%".downcase).where('LOWER(entities.professional_activity) LIKE?', "%#{params[:activity_filter]}%".downcase).paginate(page: params[:page], per_page: 8)
else
#entitys = Entity.all.paginate(page: params[:page], per_page: 8)
end
end
enter code here
This worked for what I needed (or so it seems)
I'm learning Ruby on Rails and to begin I'm making this little blog application via http://guides.rubyonrails.org/getting_started.html#showing-articles .
Now I'm at 5.10 where I need to add validation to the form so if the user adds a tittle with a length shorter than 5.
So this is my articles_controller.rb:
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
end
def create
#render plain: params[:article].inspect
##article = Article.new(params[:article])
#article = Article.new(article_params)
##article.save
if #article.save
redirect_to #article
else
render 'new'
end
redirect_to #article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
And in this view I have an error (new.html.erb):
<%= form_for :article, url: articles_path do |f| %>
<% if #article.errors.any? %>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
This is the error I get:
I'm new to Ruby and rails so I hope I can get some help.
You didn't initialize #article instance variable, but you try tu use it in new view. You should have:
def new
#article = Article.new
end
I'm trying to make a basic search function for a simple product inventory app on Sinatra, but don't know how to make the controller and view to properly output all the products which have similar names to a results page.
SearchPage.erb:
<form action="/search", method="post">
<input type="text" name="product[name]">
Controller:
post '/search' do
#Products = Product.find_by(name: params[:product][:name])
#Products = Product.all(:name.like => "%#{params[:name]}%") #found this on another question
erb :"result"
end
Result.erb
<% #Products.each do |product| %>
<%=product.name %>
<%=product.details %>
EDIT: I was able to make search work based on the suggestion with the following code. Thanks!:
Search.erb View
<form action="/search", method="get">
<input type="text" name="search">
Controller
get '/search' do
#products = Product.all
if params[:search]
#products = Product.search(params[:search])
else
#products = Product.all
end
erb :'results'
end
Model
class Product < ActiveRecord::Base
def self.search(search)
where("name like ?", "%#{search}%")
end
Results.erb View
<% if #products.present? %>
<table>
<td>Product Name</td><td>Company</td>
<% #products.each do |product| %>
<tr><td><%=h product.name %> </td>
<td><%=h product.company.name %></td>
<% end %>
<% else %>
<p>There are no Products containing the term(s) <%= params[:search] %>.</p>
<% end %>
</table>
I notice off the bat you're using a POST method. There is an easier way to do create search functionality for your products. Try this:
Posts Controller:
#products = Product.all
if params[:search]
#products = Product.search(params[:search]).order("created_at DESC")
else
#products = Product.all.order('created_at DESC')
end
end
Posts Model (note: If you are using SQLite keep it as LIKE. If you are using Postgres, change LIKE to ILIKE)
def self.search(search)
where('name like :pat or content like :pat', :pat => "%#{search}%")
end
Search Form (Put into your Result.erb and edit as needed but keep as get method. I personally like using form helpers but you can create a normal form if you'd like)
<%= form_tag(products_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Posts" %>
<%= submit_tag "Search" %>
<% end %>
Render Results
<% if #products.present? %>
<%= render #products %>
<% else %>
<p>There are no posts containing the term(s) <%= params[:search] %>.</p>
<% end %>
Let me know if this works for you. If not, i'll try help some more.
I have a form to fill in booking details, but I'm getting the following error:
NoMethodError in Bookings#new
undefined method `bookings_path'
This happened after change the routes.rb file, to nest the booking resources in the user resource.
My form file code is the following:
<% provide(:title, 'Book Now!') %>
<section id="book-now">
<%= form_for(#booking) do |f| %>
<header>
<h1>Edit booking</h1>
</header>
<%= f.text_field :name, placeholder: "Name" %>
<%= f.text_field :check_in, placeholder: "Check-in" %>
<%= f.text_field :check_out, placeholder: "Check-out" %>
<%= f.submit "Save" %>
<% end %>
</section>
My booking controller code is:
class BookingsController < ApplicationController
def new
#booking = Booking.new
end
def create
#booking = Booking.new(params_bookings)
#booking.user_id ||= current_user.id
if #booking.save
redirect_to user_path(#booking.user_id)
else
# render 'new'
end
end
end
private
def params_bookings
params.require(:booking).permit(:check_in, :check_out, :name, :user_id)
end
end
and my routes.rb file looks like this:
Hightide::Application.routes.draw do
resources :users do
resources :bookings
end
match '/users/:user_id/bookings/new', to: 'bookings#new', via: [:post, :get]
you have this error because your bookings routes are nested to the users, so when you write this
form_for #booking
you essentially call
form_for bookings_path, ...
coz rails gets the type of the object you send to form_for and tries to get vanilla path for it.
to solve the problem you need either create the vanilla bookings resources in your routes, or specify both a user and a booking reference for the form_for call, like so
form_for [current_user, #booking]
In view:
<%= text_field_with_auto_complete :vendor, :number, {}, {:with => "'vendor[number]=' + $('vendor_number').value"} %>
In Controller:
auto_complete_for :vendor, :number do |vendors, params|
vendors.vendor_company_filter(params[:company_id])
end
It works fine, gives list of vendor number as a result
but I need "vendor name + vendor number" in a result list, how can I achieve this..??
please help..
found an easy way,
in Controller
def auto_complete_for_vendor_number
#vendors = Vendor.find_by_number(number)
render :partial => 'auto_complete_for_vendor_number'
end
in _auto_complete_for_vendor_number.html.eb partial:
<ul>
<% #vendors. each do |vendor| %>
<li><%= vendor.name+'('+vendor.number+')' %></li>
<% end %>
</ul>
It worked fine :)