undefined local variable or method `subjects_path' - ruby-on-rails

I want to search text from mysql db table .
view page:
<% form_tag subjects_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
controller code:
before_action :confirm_logged_in
def index
#subjects=Subject.sorted
#subjects = Subject.search params[:search]
end'
Routes used:
root 'demo#index'
get 'admin', :to=> "access#index"
match ':controller(/:action(/:id))' ,:via=>[:get,:post]
i am waiting for your answer.

in routes.rb you need to define the subjects
resources :subjects, only: [:index]
You've not specified any named routes so subjects_path doesn't exist.

Related

Getting "First argument in form cannot contain nil or be empty" when trying to implement search feature on Rails

I'm trying to make a dropdown search by category feature on my webapp. I am currently trying to get the dropdown menu to work, but I am constantly getting the error saying:
First argument in form cannot contain nil or be empty
Here is the view:
<%= form_for #search, html: {class: "pure-form"} do |s| %>
<div class="field">
<%= s.label :category %>
<%= s.select(:category, options_for_select([['Other', 'Other'], ["Breakfast", "Breakfast"], ["Lunch/Dinner", "Lunch/Dinner"], ["Dessert", "Dessert"]])) %>
<%end%>
<div class="card-columns">
<% #images.each do |image| %>
<div class="card">
<div class="card-body p-1">
<%= image_tag image.image, class: 'd-block w-100' %>
<%= link_to image.title, image_path(image.id), class: "btn btn-primary stretched-link" %>
</div>
</div>
<% end %>
</div>
And here is the controller:
class SearchesController < ApplicationController
def new
#search = Search.new
#categories = Image.uniq.pluck(:category)
end
def create
#search = Search.create(search_params)
redirect_to #search
end
def show
#search = Search.find(params[:id])
end
private
def search_params
params.require(:search).permit(:category)
end
end
And here are the routes:
Rails.application.routes.draw do
devise_for :users
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root 'pages#home'
get '/images', controller: 'images', action: 'recent'
get '/upload', controller: 'images', action: 'new'
get '/#:username', to: 'users#show', as: :profile
delete 'images/:id(.:format)', :to => 'images#destroy'
resources :images, only: [:index, :show, :create]
get 'searches/search'
resources :searches
end
I tried to configure different things to see if I could get it working, but I could not. I am very unclear as to what is the problem. Any help would be appreciated! Thanks in advance!

Couldn't find Timetable with 'id'=clas

Generated a timetable scaffold. Then later created a new action "clas" in my timetables_controller.rb.
timetables_controller.rb
class TimetablesController < ApplicationController
before_action :set_timetable, only: [:show, :edit, :update, :destroy, :clas]
def clas
#classtimetable = Timetable.where(params[:clas])
end
//other actions
def set_timetable
#timetable = Timetable.find(params[:id])
end
def timetable_params
params.require(:timetable).permit(:day, :clas, :one_teacher, :one_sub, :two_teacher, :two_sub, :three_teacher, :three_sub, :four_teacher, :four_sub, :five_teacher, :five_sub, :six_teacher, :six_sub, :seven_teacher, :seven_sub, :eight_teacher, :eight_sub)
end
end
Created form_for to the action "clas". Need to pass the select option values as params to the controller
clas.html.erb
<% form_for #classtimetable, :url => clas_timetables_path, :html => { :method => :post} do |f| %>
<div class="field">
<%= f.label "Select class" %>
<% values = ["1c", "2c", "3c", "4d", "5i"] %>
<%= f.select :clas, options_for_select(values) %>
</div>
<div class="actions">
<%= f.submit "Submit"%>
</div>
<% end %>
<% #classtimetable.each do |class_timetable| %>
<%= class_timetable.day %>
<% end %>
routes.rb
resources :timetables do
member do
get 'clas'
end
end
I need to get all the day for the class in my clas.html.erb page. Which is done by selecting the class from dropdown and submit. The value should be passed in params when i click submit.
don't know how to fix it. Any ideas?
Add controller in form_for tag,
<% form_for #classtimetable, :url => { :controller => "your-controller-name", :action => :clas } do |f| %>
Or you can directly write like this,
<% form_for #classtimetable, :url => clas_timetables_path, :html => { :method => :post} do |f| %>
Change before_action like,
before_action :set_timetable, only: [:show, :edit, :update, :destroy, :clas]
You would change collection to member and pass id
resources :timetables do
member do
get 'clas'
end
end
Try to change your form_tag like this,
<% form_for #classtimetable, :url => clas_timetables_path(#classtimetable), :method => :post do |f| %>
Can you try this
def clas
#classtimetable = Timetable.where(timetable_params[:clas]).first
end
First I recognized: You used get in routes, and post in form. So the actions won't match.
Second: You've written:
still getting the same error undefined method `model_name' for Timetable::ActiveRecord_Relation:Class
So look, how you defined #classtimetable
#classtimetable = Timetable.where(params[:clas])
This is a relation, not the ActiveRecord, you want to find by clas. The error will be thrown on this line:
<% form_for #classtimetable, :url => clas_timetables_path, :html => { :method => :post} do |f| %>
form_for expects as first argument an ActiveRecord or something similar (at least something extending ActiveModel::Naming). Since I do not see your definition of Timetable, I just guess, it has an attribute called clas and you want to find the Timetable with clas equals the selection in that form. So it would be:
#classtimetable = Timetable.find_by(clas: params[:clas])
what is the same as:
#classtimetable = Timetable.where(clas: params[:clas]).first
To get the first element from that Relation, you tried to use.
Third: Since you did define your route to be a member route, it should expect an id in clas_timetable_path. Since you load #timetable in clas action in before_filter, I think you want to have there:
<% form_for #classtimetable, :url => clas_timetables_path(#timetable), :html => { :method => :get} do |f| %>

Rails Date Search

New to rails and building my first app on my own (did a few tutorials). Wanting to build an app where the root page, search.html.erb view, is only a search bar with two params, and outputs the search result to the index view. Thanks in advance!
What I have now:
Controller:
def search
#tours=Tour.search(params[:date], params[:region])
end
Search View:
<%= form_tag tours_path, :method => 'get' do %>
<p>
<%= text_field_tag :region %>
<%= text_field_tag :date %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Tour Model:
def self.search(date, region)
if date && region
where(date: date, region: region)
else
all
end
end
Routes:
get '' => 'tours#search'
post '' => 'tours#search_results'
get 'tours/:date/:region' => 'tours#index'
resources :tours, except: :index
Rake routes:
root GET / tours#search
GET / tours#search
POST / tours#search_results
GET /tours/:date/:region(.:format) tours#index

Locale Switcher

I am busy going through PBP - Agile Web Development with Rails and implementing the locale switcher.
However when I try switch between english and spanish I get a error:
No route matches [POST] "/en"
My controller is as follows:
class StoreController < ApplicationController
skip_before_filter :authorize
def index
if params[:set_locale]
redirect_to store_path(locale: params[:set_locale])
else
#products = Product.order(:title)
#cart = current_cart
end
end
end
and an extract of the application.hmtl.erb that is being used;
<div id="banner">
<%= form_tag store_path, class: 'locale' do %>
<%= select_tag 'set_locale', options_for_select(LANGUAGES, I18n.locale.to_s), onchange: 'this.form.submit()' %>
<%= submit_tag 'submit' %>
<%= javascript_tag "$('.locale input').hide()" %>
<% end %>
<%= image_tag("logo.png") %>
<%= #page_title || t('.title') %>
</div>
the routing folder is as follows:
scope'(:locale)' do
resources :users
resources :orders
resources :line_items
resources :carts
resources :products do
get :who_bought, on: :member
end
root to: 'store#index', as: 'store'
end
Cant figure out what I did wrong. If I enter /en or /es in the url it works correctly. However choosing it in the drop down that is created I get the error mentioned
Found the issue, the form_tag was expecting a POST so I changed
<%= form_tag store_path, class: 'locale' do %>
to
<%= form_tag store_path, class: 'locale', :method => :get do %>
and it worked
You are missing an slash in the scope as guides states:
# config/routes.rb
scope "/:locale" do
resources :books
end
http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params

Rails - Form_tag for custom action

I have a controller games and a method:
def index
#games = Game.all
end
def set_game
#current_game = Game.find(params[:set_game])
end
In my view I have:
<%= form_tag("/games") do %>
<% #games.each do |g| %>
<%= radio_button_tag(:game_id, g.id) %>
<%= label_tag(:game_name, g.name) %><br>
<% end %>
<%= submit_tag "Confirm" %>
<% end %>
Routes:
resources :games
match 'games', :to => 'game#index'
How can I make this form work for my set_game method?
Thanks.
<%= form_tag(set_game_games_path) do %>
...
<% end %>
#routes.rb
resources :games do
collection do
get '/set_game', :as => :set_game
end
end
That's an example of a custom route:
match "customroute" => "controller#action", :as => "customroutename"
This can be then accessed with "customroutename_url" in your views. If you want to create a custom route for your set_game action, for example, it would be
match "setgame" => "games#set_game", :as => "setgame"
Then you can do
<%= form_tag setgame_url %>
...
<% end %>
You can read more about custom routes here: http://guides.rubyonrails.org/routing.html#non-resourceful-routes

Resources