Couldn't find Timetable with 'id'=clas - ruby-on-rails

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| %>

Related

How can I get post parameter in controller

I need to get post parameter deal_id in controller.
My view -> show.html.erb
<%= form_for([#deal, #contract], :url => pay_payments_path, html: { method: :post }) do |f| %>
<%= f.hidden_field :deal_id, value: #deal.id %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.submit "Send", :class => "btn btn-danger" %>
<% end %>
My controller -> payments_controller
def pay
#deal = Deal.find(params[:deal_id])
end
My code in controller dont works, I got this:
Couldn't find Acordo with 'id'=
I want to get the #deal according deal_id parameter sent via post.
Routes
resources :payments, only: [:index, :new, :create] do
collection do
post :pay
end
end
If you are getting an empty value for params[:deal_id] you are probably missing adding it to the allowed parameters which may not be part of the default fields for a Payment resource:
def params
params.require(:payment).permit(:deal_id)
end
Make sure you have the field in the params method on your payments controller.
I am not a Ruby expert but maybe this can help you.
Accessing POST parameters

Resource defines an update using PATCH but app wants POST in Rails 4

I'm trying to add edit functionality to my web app, and am having some trouble. The error page I get back when I try to complete an edit of my Request object indicates that it couldn't find the right route, but the same error page contains a list of routes which includes the route it's looking for. So I'm a bit flummoxed.
The "new" method is almost identical to this edit method and the pages are almost identical as well.
The error page begins No route matches [POST] "/requests/19/edit" and then, partway down the route listing, I see this:
requests_path GET /requests(.:format) requests#index
POST /requests(.:format) requests#create
new_request_path GET /requests/new(.:format) requests#new
edit_request_path GET /requests/:id/edit(.:format) requests#edit
request_path GET /requests/:id(.:format) requests#show
PATCH /requests/:id(.:format) requests#update
PUT /requests/:id(.:format) requests#update
DELETE /requests/:id(.:format) requests#destroy
So Rails seems to be generating a request_path which expects a PATCH, not a POST, right?
routes.rb
Rails.application.routes.draw do
root "pages#index"
resources :destinations
resources :users
resources :manifests
resources :requests
:
request.rb
class Request < ActiveRecord::Base
validates_presence_of :justification
validates_presence_of :required_by
belongs_to :user
belongs_to :manifest
belongs_to :network
has_many :uploaded_files
scope :sorted, lambda{ order("required_by") }
end
edit.html.rb
<% #page_title = "Update Request" %>
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="requests edit">
<h2>Update Request</h2>
<%= form_for :request, url: request_path(#request) do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>
<div class="form-buttons">
<%= submit_tag("Update Request") %>
</div>
<% end %>
</div>
requests_controller.rb
def update
#request = Request.find(params[:id])
p = {'file' => params[:request][:uploaded_file], 'request_id' => #request.id}
uf = UploadedFile.create(p)
if #request.update_attributes(request_params)
flash[:notice] = "Request updatred succesfully"
redirect_to :action => 'show', :id => #request.id
else
render 'edit'
end
end
What have I missed?
Change
<%= form_for :request, url: request_path(#request) do |f| %>
to
<%= form_for :request, url: request_path(#request), method: :patch do |f| %>
in your edit.html.erb
form_for(as you are using it) sets POST as default HTTP verb. You need to alter it by setting method :patch which responds to the update action.
You can simplify it to just
<%= form_for #request do |f| %>
Check the APIdoc for more Info.

undefined local variable or method `subjects_path'

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.

Undefined method which is not used

Hello i'm new one in ruby on rails. I have strange problem. I use some tutorial and get error which i shouldn't get.
I have controller
class DiaryController < ApplicationController
before_action :authenticate_user!
respond_to :html, :xml, :json
respond_to :js, :only => [:create, :update, :destroy]
def create
#record = Record.create(record_params)
#record.userId=current_user.id
if request.xhr? || remotipart_submitted?
sleep 1 if params[:pause]
render :layout => false, :template => (params[:template] == 'escape' ? 'comments/escape_test' : 'diary/create'), :status => (#record.errors.any? ? :unprocessable_entity : :ok)
else
redirect_to diary_path
end
end
def add
#record = Record.new
#respond_with(#record, :layout => false)
respond_with do |format|
format.html { render :layout => ! request.xhr? }
end
end
# PUT /comments/1
# PUT /comments/1.xml
def update
#record = Record.find(params[:id])
respond_with do |format|
format.html{ redirect_to #record }
end
end
def delete
#comment = Comment.destroy(params[:id])
end
def edit
#record = Record.find(params[:id])
end
def index
#records = Record.where(userId: current_user.id)
end
private
def record_params
params.require(:record).permit(:photo, :comment, :date, :photo_cache)
end
end
Have view
<h1 align="centre">
Добавить запись
</h1>
<%= render 'form' %>
<%= link_to 'Отмена', diary_path, :id => 'cancel-button' %>
and
<%= form_for(#record, :remote => (params[:action] == 'add' ? true : false)) do |f| %>
<fieldset>
<div class="field">
<%= f.label :date, :class => 'required' %><br />
<%= f.date_select :date %>
</div>
<div class="field">
<%= f.label :comment %><br />
<%= f.text_area :comment %>
</div>
<div class="field">
<%= image_tag(#record.photo_url(:thumb)) if #record.photo? %><br />
<%= f.label :photo %><br/>
<%= f.file_field :photo %><br/>
<%= f.hidden_field :photo_cache %>
</div>
</fieldset>
<table>
<tr>
<td>
<div class="actions">
<%= f.submit "Добавить", :data => {:'disable-with' => "Submitting..."} %>
</div>
</td>
<td>
<%= link_to 'Отмена', diary_path, :id => 'cancel-button' %>
</td>
</tr>
</table>
And get ActionView::Template::Error (undefined method `records_path' for #<#:0x000000054461c8>): error on "<%= form_for(#record, :remote => (params[:action] == 'add' ? true : false)) do |f| %>" line. Even records_path i did'n use.
I Have routes
devise_for :users
get 'welcome/index'
root 'welcome#index'
get 'diary' => 'diary#index'
get 'diary/add_record', to: 'diary#add', as: 'add_record'
post 'diary/add_record', to: 'diary#create'
get 'diary/edit_record/:id', to: 'diary#edit'
delete 'diary/edit_record/:id' => 'diary#delete
And and try to use add_record route. Maybe it would be better to use resources :records.But i want to figure out why my routes doesn't work.
view name "diary".
Because you're new to RoR, let me explain why you're receiving the error
form_for
form_for is the likely reason why you're receiving this error (oh, I just saw it actually states this is where the error occurs - sweet)
The problem you have is that form_for is meant as a way to render a form around an ActiveRecord object. It's mean to give some semi-persistence to the data, by using AR in both the new and create actions (allowing you to show the in-putted data on the form after submission)
When you pass an object to form_for, Rails automatically "builds" the form from the ActiveRecord object, one of the options it uses being the url
--
Routes
The problem you have is the object you pass to the form_for takes the model_name attribute to build the route. This means if you want to use the form_for method by just passing an object, it's going to look for routes pertaining directly to that object
If you don't have any [model]_path route set up, you'll likely receive the error you're getting. The fix firstly involves the routes, and secondly involves the controller:
#config/routes.rb
root 'welcome#index'
devise_for :users
resources :diary, path_names: { new: "add_record", create: "add_record", edit: "edit_record", destroy: "edit_record" }
resources :welcome, only: :index
This is down to the idea that Rails' routing structure is built around resources - every route you have should lead to a specific controller action. Whilst including custom actions is completely fine, you have to appreciate that the basis of the routing structure is to construct resourceful routing, which essentially means that Rails perceives every controller / model to have corresponding routes:
--
URL
The second thing to observe is the url of the form
If you have your routes set up as above, and if your routing structure differs from your model structure (different names), you'll want to use the following setup to define the url explicitly:
<%= form_for #record, url: your_custom_path do |f| %>

Howto write form_for without :url for multiple object editing?

I am following http://railscasts.com/episodes/52-update-through-checkboxes. There a form can be written like this: <% form_tag update_multiple_products_path, :method => :put do %>. This does not generate the right action (action="/products") so i have to manually setup the action url.
Is there a away to write form_for without :url parameter?
My setup:
# index.html.erb
<%= form_for update_multiple_products_path, :url => {:action => 'update_multiple'}, :method => :put do %>
# routes.rb
resources :products do
collection do
put :update_multiple
end
end
# productscontroller
class ProductsController < ApplicationController
...
def update_multiple

Resources