Showing a general list of booked users - ruby-on-rails

I created an application where users can book a hour of training. I want to give to admin users the option to see a general list of booked users in every training hour.
Im trying to create this index, but it gives me the following error:
ActionController::UrlGenerationError in Trainings#index
No route
matches {:action=>"index", :controller=>"bookings", :training_id=>nil}
missing required keys: [:training_id]
Im trying to create this button at index of training, like this:
training index.html.erb view:
<h1>Hours</h1>
<ul class="trainings">
<% #trainings.each do |training| %>
<li>
<%= link_to training.hour, training_path(training) %>
</li>
<% end %>
</ul>
<%= render 'bookings/general_list' if logged_in? %>
_general_list.html.erb view:
<ul class="bookings">
<% #trainings.each do |training| %>
<%= link_to "General list", training_bookings_path(#training), class: "btn btn-primary" %>
<% end %>
</ul>
I would like to now how i can get this done, i think my idea is not working.
my routes.rb:
Rails.application.routes.draw do
root 'static_pages#home'
get '/signup', to: 'users#new'
get '/contact', to: 'static_pages#contact'
get '/about', to: 'static_pages#about'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :users
resources :trainings do
resources :bookings
end
end
rake routes command:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
signup GET /signup(.:format) users#new
contact GET /contact(.:format) static_pages#contact
about GET /about(.:format) static_pages#about
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
password_resets POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
training_bookings GET /trainings/:training_id/bookings(.:format) bookings#index
POST /trainings/:training_id/bookings(.:format) bookings#create
new_training_booking GET /trainings/:training_id/bookings/new(.:format) bookings#new
edit_training_booking GET /trainings/:training_id/bookings/:id/edit(.:format) bookings#edit
training_booking GET /trainings/:training_id/bookings/:id(.:format) bookings#show
PATCH /trainings/:training_id/bookings/:id(.:format) bookings#update
PUT /trainings/:training_id/bookings/:id(.:format) bookings#update
DELETE /trainings/:training_id/bookings/:id(.:format) bookings#destroy
trainings GET /trainings(.:format) trainings#index
POST /trainings(.:format) trainings#create
new_training GET /trainings/new(.:format) trainings#new
edit_training GET /trainings/:id/edit(.:format) trainings#edit
training GET /trainings/:id(.:format) trainings#show
PATCH /trainings/:id(.:format) trainings#update
PUT /trainings/:id(.:format) trainings#update
DELETE /trainings/:id(.:format) trainings#destroy
Thanks.

In _general_list.html.erb you should change #training for training

Related

form_for helper gives me an Error missing a param allthough its there

I have a problem with the form_for Helper in Rails. I want to edit a Post in my Forum app
I am getting this error when trying to call the edit page:
ActionView::Template::Error (No route matches
{:action=>"show",
:category=>#<Post id: 1,
user_id: 1,
title: "Die Webseite ist nun online",
body: "<div>Viel Spaß euch allen. Und haltet euch an die ...",
category: "general/announcements",
slug: "die-webseite-ist-nun-online",
created_at: "2018-09-16 01:00:55",
updated_at: "2018-09-16 01:00:55">,
:controller=>"posts",
:id=>"die-webseite-ist-nun-online",
:locale=>:de},
possible unmatched constraints: [:category]):
This is my Edit action:
def edit
#post = Post.find(params[:id])
end
And this is the call to form_for
<%= form_for(#post) do |f| %>
What I have tried:
<%= form_for(#post, category: params[:category]) do |f| %>
params[:category] has the right value, I tested it to not be empty or nil
Providing the category param like that did NOT change the error I get!
Edit:
as requested my routes.rb:
Rails.application.routes.draw do
concern :paginatable do
get '(page/:page)', action: :index, on: :collection, as: ''
end
CATEGORY_FILTERS = /(?x)general\/suggestions|general\/member-introductions|
general\/announcements|off-topic\/jobs-and-projects|
off-topic\/miscellaneous|off-topic\/funny-stuff|
ruby-on-rails\/news|ruby-on-rails\/developers|
ruby-on-rails\/tutorials/
scope '(:locale)', locale: /en|de/ do
root 'forum#home'
get '/help', to: 'forum#help'
get '/about', to: 'forum#about'
get '/contact', to: 'forum#contact'
get '/general', to: 'forum#general'
get '/ruby-on-rails', to: 'forum#rubyonrails'
get '/off-topic', to: 'forum#offtopic'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users, concerns: :paginatable
resources :password_resets, only: %i[new edit create update]
scope '*category', category: CATEGORY_FILTERS do
get '', to: 'posts#index'
resources :posts, concerns: :paginatable
end
end
resources :account_activations, only: [:edit]
end
Rails Routes gives
Prefix Verb URI Pattern Controller#Action
root GET /(:locale)(.:format) forum#home {:locale=>/en|de/}
help GET (/:locale)/help(.:format) forum#help {:locale=>/en|de/}
about GET (/:locale)/about(.:format) forum#about {:locale=>/en|de/}
contact GET (/:locale)/contact(.:format) forum#contact {:locale=>/en|de/}
general GET (/:locale)/general(.:format) forum#general {:locale=>/en|de/}
ruby_on_rails GET (/:locale)/ruby-on-rails(.:format) forum#rubyonrails {:locale=>/en|de/}
off_topic GET (/:locale)/off-topic(.:format) forum#offtopic {:locale=>/en|de/}
signup GET (/:locale)/signup(.:format) users#new {:locale=>/en|de/}
login GET (/:locale)/login(.:format) sessions#new {:locale=>/en|de/}
POST (/:locale)/login(.:format) sessions#create {:locale=>/en|de/}
logout DELETE (/:locale)/logout(.:format) sessions#destroy {:locale=>/en|de/}
users GET (/:locale)/users(/page/:page)(.:format) users#index {:locale=>/en|de/}
GET (/:locale)/users(.:format) users#index {:locale=>/en|de/}
POST (/:locale)/users(.:format) users#create {:locale=>/en|de/}
new_user GET (/:locale)/users/new(.:format) users#new {:locale=>/en|de/}
edit_user GET (/:locale)/users/:id/edit(.:format) users#edit {:locale=>/en|de/}
user GET (/:locale)/users/:id(.:format) users#show {:locale=>/en|de/}
PATCH (/:locale)/users/:id(.:format) users#update {:locale=>/en|de/}
PUT (/:locale)/users/:id(.:format) users#update {:locale=>/en|de/}
DELETE (/:locale)/users/:id(.:format) users#destroy {:locale=>/en|de/}
password_resets POST (/:locale)/password_resets(.:format) password_resets#create {:locale=>/en|de/}
new_password_reset GET (/:locale)/password_resets/new(.:format) password_resets#new {:locale=>/en|de/}
edit_password_reset GET (/:locale)/password_resets/:id/edit(.:format) password_resets#edit {:locale=>/en|de/}
password_reset PATCH (/:locale)/password_resets/:id(.:format) password_resets#update {:locale=>/en|de/}
PUT (/:locale)/password_resets/:id(.:format) password_resets#update {:locale=>/en|de/}
GET (/:locale)/*category(.:format) posts#index {:category=>/(?x)gener...rials/, :locale=>/en|de/}
posts GET (/:locale)/*category/posts(/page/:page)(.:format) posts#index {:category=>/(?x)genera...torials/, :locale=>/en|de/}
GET (/:locale)/*category/posts(.:format) posts#index {:category=>/(?x)genera...torials/, :locale=>/en|de/}
POST (/:locale)/*category/posts(.:format) posts#create {:category=>/(?x)genera..torials/, :locale=>/en|de/}
new_post GET (/:locale)/*category/posts/new(.:format) posts#new {:category=>/(?x)genera...torials/, :locale=>/en|de/}
edit_post GET (/:locale)/*category/posts/:id/edit(.:format) posts#edit {:category=>/(?x)genera...orials/, :locale=>/en|de/}
post GET (/:locale)/*category/posts/:id(.:format) posts#show {:category=>/(?x)genera...rials/, :locale=>/en|de/}
PATCH (/:locale)/*category/posts/:id(.:format) posts#update {:category=>/(?x)gene...rials/, :locale=>/en|de/}
PUT (/:locale)/*category/posts/:id(.:format) posts#update {:category=>/(?x)genera...orials/, :locale=>/en|de/}
DELETE (/:locale)/*category/posts/:id(.:format) posts#destroy {:category=>/(?x)gene...rials/, :locale=>/en|de/}
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
Maybe try to specify all the options for the form_for helper:
<%= form_for :post, url: post_path(#post, category: params[:category]), method: :patch do |f| %>

Rails having a naming error on a form_for that is causing an unknown path error

The issue is I get this nasty little controller error...
NoMethodError in Contacts#new
undefined method `contacts_path' for #<#:0x0000000124b228>
Did you mean? contact_path
on my view: contact#new
<%= form_for #contact do |f| %>
<div class="col-xs-6 form-group contact-input">
<h1><%= f.label :Feedback %></h1>
<%= f.text_area :text, class: "input-lg form-control", rows: "10" %>
<%= f.submit "Send Feedback", class: "btn btn-primary btn-lg" %>
</div>
<% end %>
controller: contacts_controller.rb
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new
if #contact.save
redirect_to '/'
flash[:success] = "Thanks for the Post!"
else
redirect_to contact_path
flash[:alert] = "Please provide input!"
end
end
end
migration:
class CreateContacts < ActiveRecord::Migration
def change
create_table :contacts do |t|
t.text :text
t.timestamps null: false
end
end
end
The error is complaining about line 3 in the view. My model is contact.rb incase I have a plural issue somewhere, but I really do not think that I do. Any help would be much appreciated. This is rails 4.2... I am also a rails beginner.
I know I don't have the flash even setup yet.
rake routes:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
about GET /about(.:format) static_pages#about
news GET /news(.:format) static_pages#news
advertise GET /advertise(.:format) static_pages#advertise
fishing GET /fishing(.:format) static_pages#fishing
signup GET /signup(.:format) users#new
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
contact GET /contact(.:format) contacts#new
contact_index GET /contact(.:format) contact#index
POST /contact(.:format) contact#create
new_contact GET /contact/new(.:format) contact#new
edit_contact GET /contact/:id/edit(.:format) contact#edit
GET /contact/:id(.:format) contact#show
PATCH /contact/:id(.:format) contact#update
PUT /contact/:id(.:format) contact#update
DELETE /contact/:id(.:format) contact#destroy
forum_comments GET /forums/:forum_id/comments(.:format) comments#index
POST /forums/:forum_id/comments(.:format) comments#create
new_forum_comment GET /forums/:forum_id/comments/new(.:format) comments#new
edit_forum_comment GET /forums/:forum_id/comments/:id/edit(.:format) comments#edit
forum_comment GET /forums/:forum_id/comments/:id(.:format) comments#show
PATCH /forums/:forum_id/comments/:id(.:format) comments#update
PUT /forums/:forum_id/comments/:id(.:format) comments#update
DELETE /forums/:forum_id/comments/:id(.:format) comments#destroy
forums GET /forums(.:format) forums#index
POST /forums(.:format) forums#create
new_forum GET /forums/new(.:format) forums#new
edit_forum GET /forums/:id/edit(.:format) forums#edit
forum GET /forums/:id(.:format) forums#show
PATCH /forums/:id(.:format) forums#update
PUT /forums/:id(.:format) forums#update
DELETE /forums/:id(.:format) forums#destroy
logout DELETE /logout(.:format) sessions#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
routes.rb :
Rails.application.routes.draw do
root 'static_pages#home'
get 'about' => 'static_pages#about'
get 'news' => 'static_pages#news'
get 'advertise' => 'static_pages#advertise'
get 'fishing' => 'static_pages#fishing'
get 'signup' => 'users#new'
resources :users
get 'contact' => 'contacts#new'
resources :contact
resources :forums do
resources :comments
end
delete 'logout' => 'sessions#destroy'
resources :sessions, only: [:new, :create]
end
Change resources :contact to resources :contacts in your routes.rb
I am an idiot and thanks to Zepplock just thinking about my routes showed my problem. The get
get 'contact' => 'contacts#new'
was the problem I had. I also switched my resources :contact to :contacts. All my issues were solved. Thanks for asking for the right stuff again Zepplock!

Rails - link_to a user#show

In rails how do I do a link_to a user show page of the current user. Basically i want a link that go to a account page.
I tried it but i get this.
Routing Error No route matches {:action=>"show", :controller=>"users"}
Routes.rb
LootApp::Application.routes.draw do
get "password_resets/new"
get "sessions/new"
get "static_pages/home"
get "static_pages/help"
resources :sessions
resources :password_resets
resources :email_activations
resources :users do
collection do
get :accept_invitation
end
end
root to: 'static_pages#home'
match "sign_up", to: "users#new"
match '/help', to: 'static_pages#help'
match '/log_in', to: 'sessions#new'
match '/log_out', to: 'sessions#destroy'
Application.html.haml
- if current_user
= link_to "log out", log_out_path
- if current_user.admin?
# your admin
- else
= link_to "My account", user_path
- else
= link_to "log in", log_in_path
Users controller
class UsersController < ApplicationController
before_filter :admin_and_user, only: [:destory, :edit, :show]
def show
#user = User.find(params[:id])
end
rake routes
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
just
<%= link_to current_user.name, current_user%>
The current answer is the best solution, but just to elaborate: The problem that was causing an error in your code was that you needed to specify which user the link should link to. So instead of just user_path should you do like this:
= link_to "My account", user_path(current_user)
A shortcut for this is (as shown in the current answer by #Muntasim)
= link_to "My account", current_user

error when routing

I have routes defined to some static pages and also to some controllers (users and usymptoms).
When I navigate to localhost:3000/users/1 or localhost:3000/users/1/usymptoms/new everything is fine. Once I finish filling in the form on symptoms/new, I have the usymptoms controller redirecting to #users. This works fine.
In the model file, the association is users has many usymptoms and usymptoms belongs to user.
However, now my static pages are not accessible. For example, when I navigate to /learn, I get the following error:
No route matches {:action=>"new", :controller=>"usymptoms", :user_id=>nil}
I am new to rails. Can you please help me figure out the error?
I have provided my routes file and the output from "rake routes" below.
My routes.rb file
root to: 'static_pages#home'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
match '/learn', to: 'static_pages#learn'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
resources :users do
resources :usymptoms
end
resources :sessions, only: [:new, :create, :destroy]
======
Output from rake routes
root / static_pages#home
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
learn /learn(.:format) static_pages#learn
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
user_usymptoms GET /users/:user_id/usymptoms(.:format) usymptoms#index
POST /users/:user_id/usymptoms(.:format) usymptoms#create
new_user_usymptom GET /users/:user_id/usymptoms/new(.:format) usymptoms#new
edit_user_usymptom GET /users/:user_id/usymptoms/:id/edit(.:format) usymptoms#edit
user_usymptom GET /users/:user_id/usymptoms/:id(.:format) usymptoms#show
PUT /users/:user_id/usymptoms/:id(.:format) usymptoms#update
DELETE /users/:user_id/usymptoms/:id(.:format) usymptoms#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
You must have the following link somewhere: new_user_usymptom_path(#user), but #user is nil for some reason, hence the error.

Tests say no routes match, but they work in browser

For my test I have the following:
test "should update holder" do
holder = Holder.create(name: "name", user_id: 10)
put :update, holder: holder
assert_redirected_to holder_path(assigns(:holder))
end
And when I run them I get the following Error:
ERROR (0:00:00.185) test_should_update_holder
No route matches {:holder=>"980190963", :controller=>"holders", :action=>"update"}
# /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:532:in `raise_routing_error'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:528:in `rescue in generate'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:520:in `generate'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:561:in `generate'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:557:in `generate_extras'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:553:in `extra_keys'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:147:in `assign_parameters'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:453:in `process'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:49:in `process'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:390:in `put'
test/functional/holders_controller_test.rb:36:in `block in <class:HoldersControllerTest>'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/testing/setup_and_teardown.rb:35:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:458:in `_run__4148286245602197272__setup__4285546581512185515__callbacks'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:405:in `__run_callback'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/testing/setup_and_teardown.rb:34:in `run'
But in my routes I have:
Teacherjoy::Application.routes.draw do
get "users/new"
resources :questions
resources :pages
resources :holders
resources :users
resources :sessions, only: [:new, :create, :destroy]
root :to => 'pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
end
and rake routes returns:
[teacherjoy (master)]$ rake routes
users_new GET /users/new(.:format) users#new
questions GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question GET /questions/new(.:format) questions#new
edit_question GET /questions/:id/edit(.:format) questions#edit
question GET /questions/:id(.:format) questions#show
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
pages GET /pages(.:format) pages#index
POST /pages(.:format) pages#create
new_page GET /pages/new(.:format) pages#new
edit_page GET /pages/:id/edit(.:format) pages#edit
page GET /pages/:id(.:format) pages#show
PUT /pages/:id(.:format) pages#update
DELETE /pages/:id(.:format) pages#destroy
holders GET /holders(.:format) holders#index
POST /holders(.:format) holders#create
new_holder GET /holders/new(.:format) holders#new
edit_holder GET /holders/:id/edit(.:format) holders#edit
holder GET /holders/:id(.:format) holders#show
PUT /holders/:id(.:format) holders#update
DELETE /holders/:id(.:format) holders#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
root / pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
If you look at rake routes, there clearly is a an action for update, which is a put, in the holders controller, which is what my test is doing, right?
Notice that the route is actually PUT /holders/:id, but you're passing a :holder option to your put method, not an :id. Try changing that line in your test to this:
put :update, id: holder

Resources