Could not find schedule with 'id' = clas
Created a schedule schedule. Then a new action "clas" was later created 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 for action "clas". It is necessary to pass the values of the selection parameters as parameters 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 day long for a class in my clas.html.erb page. This is done by selecting a class from the dropdown and submitting. The value should be passed in parameters when I hit submit. don't know how to fix it. Any ideas?
source to share
Add controller to tag form_for
,
<% form_for @classtimetable, :url => { :controller => "your-controller-name", :action => :clas } do |f| %>
Or you can write directly like this,
<% form_for @classtimetable, :url => clas_timetables_path, :html => { :method => :post} do |f| %>
Change before_action
as,
before_action :set_timetable, only: [:show, :edit, :update, :destroy, :clas]
You would change collection
to member
and missid
resources :timetables do
member do
get 'clas'
end
end
source to share
First I found out: you were using route and submit by form. So the actions won't match. Second, you wrote:
still gets the same undefined error `model_name 'method for schedule :: ActiveRecord_Relation: Class
So see how you defined @classtimetable
@classtimetable = Timetable.where(params[:clas])
This is the relationship, 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 that extends ActiveModel :: Naming). Since I don't see your schedule definition, I just think it has a clas attribute and you want to find a schedule with clas equal to the selection in this form. So it would be:
@classtimetable = Timetable.find_by(clas: params[:clas])
what is the same:
@classtimetable = Timetable.where(clas: params[:clas]).first
To get the first item from this Relation you tried using.
Third: since you have defined your route as a member route, it must expect an id in clas_timetable_path. Since you are loading @timetable into clas action in before_filter, I think you want there:
<% form_for @classtimetable, :url => clas_timetables_path(@timetable), :html => { :method => :get} do |f| %>
source to share