I'm trying to implement a simple search function for my Rails app by following this guide. I am trying to allow users to search for animal by name.
Here's my current code:
controllers/animals_controller.rb
class AnimalsController < ApplicationController
def search
#animal = Animal.search(params[:search])
end
end
Here's my views/animals/index.html.erb
<%= form_tag :controller => 'animals', :action => 'search', :method => 'get' do %>
<%= text_field_tag :search, params[:search], :id => 'search_field' %>
<%= submit_tag "Search", :name => nil %>
<% end %>
And here is the error I keep getting:
No route matches {:action=>"search", :controller=>"animals",
:method=>"get"}
I don't understand why this isn't working. I have the search function defined in the animal_controller.rb. Can anyone point out why the search function may not be working?
Based on the guide you are following, creating a GET /search route was never mentioned. You can define the route in config/route.rb with this
resources :animals do
get :search
end
or
get search: "animals#search"
Related
I get an error when I run this code. I want to make a button that redirects to an action from pages_controller.
Submultimi.html.erb
<%= form_tag({:controller => '/pages_controller', :action => 'calculeaza'}, :method => "post") do %>
<%= text_field_tag :field1 %>
<%= submit_tag "Button" %>
<% end %>
pages_controller.rb
def Submultimi
end
def Combinari
end
def Permutari
end
def calculeaza
puts "YAY"
redirect_to '/combinari'
end
Error message: No route matches {:action=>"calculeaza", :controller=>"pages_controller"}
routes.rb
Rails.application.routes.draw do
get '/submultimi' => 'pages#Submultimi'
get '/combinari' => 'pages#Combinari'
get '/permutari' => 'pages#Permutari'
end
If you get an error you must include the error message in the question. Anyway the controller name is obviously wrong so this must be the problem. The controller should not include the "/" nor the "_controller".
<%= form_tag( { :controller => 'pages', :action => 'calculeaza' }, :method => "post") do %>
<%= text_field_tag :field1 %>
<%= submit_tag "Button" %>
<% end %>
Your routes are also wrong:
there's no calculeaza method in routes
methods should be lowercase in routes and controller
Thanks to Phlip for the correction about the controller name :)
As Pablo said, remove the / from your controller name.
Your error message says there is no route defined. That means you haven't correctly told rails what to do with your form's post request; it's trying a route that doesn't exist.
You've got a few things going wrong. Your action names are capitalized in routes.rb, but your method names (at least the one you've linked) is not. They're case sensitive, convention is all lowercase. Also, you don't have a route defined for calculeaza. You need one, in routes.rb add (something like, I haven't tested any of this):
post '/calculeaza/' to 'pages#calculeaza'
If you want to see your currently defined routes, run rails routes in a terminal, and to use it in code append _path to the prefix verb. You end up with something like:
form_tag calculeaza_path do
You may want to read the rails routing guide, especially the parts about resourceful routes.
I'm trying to basically update my integration object attribute "filters". I have a integrations controller and what seems the proper actions to do so. But when I try to save after filling out the text-field I get this error No route matches [POST], I understand what it's saying but isn't update a post? Here is my code for clarity.
Controller
def update
#integrations = current_account.integrations.find(params[:id])
attrs = params.require(:integration).permit(:filters)
if #integrations.update_attributes(attrs)
redirect_to account_integration_path
else
render :filters
end
end
def filters
#integrations = current_account.integrations.find(params[:id])
end
View
<%= form_for #integrations, url: filters_account_integration_path do |f| %>
<%= f.text_field :filters, class: "tag-autocomplete" %>
<%= link_to "Save", account_integration_path, method: :post, class: [ "button", "button--modal" ] %>
<% end %>
Routes
resources :integrations, only: [ :index, :destroy, :update ] do
get "filters", on: :member
end
Hopefully that is enough info let me know if you need more? My basic question is why is this not updating the integration object? Isn't update a post?
resources generates seven routes by default. You're using it to generate only three of those. Those three routes will look like this:
GET /integrations
DELETE /integrations/:id
PATCH /integrations/:id/edit
Your form, on the other hand, is trying to use this route:
POST /integrations/:id
which doesn't match any of the generated routes.
Instead, try using the default form helpers:
<%= form_for #integrations, url: url_for(:controller => :integrations, :action => :update, :id => #integrations.id) do |f| %>
<%= f.text_field :filters, class: "tag-autocomplete" %>
<%= f.submit "Save" %>
<% end %>
That's assuming that #integrations is a single Integration resource. If it isn't, you have bigger problems than just this.
hy
i try to create a simple search on my application
calendar.rb
def self.search(search_condition)
search_condition = "%" + search + "%"
find( :all, :conditions => ['city like ?', search_condition])
end
calendar_controller.rb
def index
#search = #user.calendar.search(params[:search_condition])
#content_calendars = #user.calendar.all
#content_calendars_by_dates = #content_calendars.group_by(&:published_on)
#date = params[:date] ? Date.parse(params[:date]) : Date.today
end
the partial _search.html.erb
<%= form_tag root_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search_condition], :id => 'search_field' %>
<%= submit_tag "Search", :name => nil %>
<% end %
when i try i get this error:
The request method POST is inappropriate for the URL /.
i dont understand why ?
rake routes
user_calendars GET /users/:user_id/calendars(.:format) calendars#index
POST /users/:user_id/calendars(.:format) calendars#create
new_user_calendar GET /users/:user_id/calendars/new(.:format) calendars#new
edit_user_calendar GET /users/:user_id/calendars/:id/edit(.:format) calendars#edit
user_calendar GET /users/:user_id/calendars/:id(.:format) calendars#show
Your form tag needs to route to the calendar index action. Like this:
<%= form_tag(user_calendars_path(#user), :method => 'get') do %>
<%= text_field_tag :search, params[:search_condition], :id => 'search_field' %>
<%= submit_tag "Search", :name => nil %>
<% end %>
EDIT: Figured it out, here's the real problem:
Your route for calendars#create is the same as your route for calendars#index.
/users/:user_id/calendars(.:format)
You need to change your routes.rb file to differentiate between them. Traditionally the plural path goes to the index action.
I created an form_tag form:
<%= form_tag(set_image_dokumente_path) do %>
<%= text_field_tag :shit,'', data: {autocomplete_source: search2_patients_path}, :class => "shit" %>
<% end %>
I try to route to set_image action of dokumente controller, but i get the error:
undefined local variable or method `set_image_dokumente_path' for #<#<Class:0x711ff60>:0x762d578>
By default my form_tag goes to dokumente controller index action!
My routes:
resources :images
get "dokumente/index"
post "dokumente/index"
match 'patients/list' => 'patients#list'
resources :patients do
collection do
get :search2
end
end
How do i have to change it?
You can add the as: parameter to you route in order to create a named path.
For example:
post "dokumente/index", as: 'set_image_dokumente'
or similar, I'm not sure what you are trying to achieve, but I hope you get the idea :)
More info:
http://guides.rubyonrails.org/routing.html#generating-paths-and-urls-from-code
I'm making a form_tag panel that contains information (checkboxes) specific to a controller action. This action is set up in "routes.rb" as follows:
resources :students do
collection do
get :send_student_report_pdf
end
end
This setup works perfectly when I call the action from a link_to:
<%= link_to "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>
However when I used it in a form_tag, it keeps giving me this error:
Routing Error
No route matches "/students/send_student_report_pdf"
The form_tag code I have is here:
<%= form_tag :controller => 'students', :action => 'send_student_report_pdf', :method => 'get' do %>
<%= label_tag "Include columns" %> <br>
<%= check_box_tag "first_name", params[:first_name], checked = true %> <%= label_tag "First Name" %><br>
<%= submit_tag "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>
<% end %>
I have tried giving it the url, path like:
<%= form_tag send_student_report_pdf_students_path, :method => 'get' do %>
But it has been consistently giving me the same Route error (as if the action doesn't exist at all in routes.rb, even though it works perfectly using link_to instead of form_tag submit
Here is the code for the action in the controller, it basically sends back a file.
def send_student_report_pdf
#students = search_sort_and_paginate
puts "params[:first_name] = ", params[:first_namea]
send_data(generate_pdf_report(#students), :filename => "report.pdf", :type => 'application/pdf')
end
If you see that I'm missing something here, please help me.
Thank you very much,
Regards,
The :method => 'get' part in your form_for is in the url_for_options hash, not the options hash, so Rails will be putting it onto the url as cgi params instead. Try changing it to this:
form_tag url_for(:controller => 'students', :action => 'send_student_report_pdf'), :method => 'get' do ...
The reason you can't use the named route is because you didn't name it in your routes. If you name it in your routes and use the named route in your form_tag, you won't need to use url_for...
resources :students do
collection do
get :send_student_report_pdf, :as => :send_student_report_pdf
end
end
You can check whether your routes are as you expect by running rake routes