I have a Training model that's nested under a devise User model.
/config/routes.rb
devise_for :users, path: 'u'
resources :users, only: ['index','show'], shallow: true do
resources :trainings
end
> rake routes
new_user_session GET /u/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /u/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /u/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /u/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /u/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /u/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /u/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /u/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /u(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /u/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /u/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /u(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /u(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
user_trainings GET /users/:user_id/trainings(.:format) {:action=>"index", :controller=>"trainings"}
POST /users/:user_id/trainings(.:format) {:action=>"create", :controller=>"trainings"}
new_user_training GET /users/:user_id/trainings/new(.:format) {:action=>"new", :controller=>"trainings"}
edit_training GET /trainings/:id/edit(.:format) {:action=>"edit", :controller=>"trainings"}
training GET /trainings/:id(.:format) {:action=>"show", :controller=>"trainings"}
PUT /trainings/:id(.:format) {:action=>"update", :controller=>"trainings"}
DELETE /trainings/:id(.:format) {:action=>"destroy", :controller=>"trainings"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
root / {:controller=>"team", :action=>"index"}
teams GET /teams(.:format) {:action=>"index", :controller=>"teams"}
POST /teams(.:format) {:action=>"create", :controller=>"teams"}
new_team GET /teams/new(.:format) {:action=>"new", :controller=>"teams"}
edit_team GET /teams/:id/edit(.:format) {:action=>"edit", :controller=>"teams"}
team GET /teams/:id(.:format) {:action=>"show", :controller=>"teams"}
PUT /teams/:id(.:format) {:action=>"update", :controller=>"teams"}
DELETE /teams/:id(.:format) {:action=>"destroy", :controller=>"teams"}
I'm having the following outputs when testing views:
Failures:
1) trainings/edit renders the edit training form
Failure/Error: render
ActionView::Template::Error:
No route matches {:controller=>"trainings"}
# ./app/views/trainings/edit.html.erb:6:in `_app_views_trainings_edit_html_erb___268980906337666865_45435220'
# ./spec/views/trainings/edit.html.erb_spec.rb:12:in `block (2 levels) in <top (required)>'
2) trainings/index renders a list of trainings
Failure/Error: render
ActionView::Template::Error:
No route matches {:action=>"new", :controller=>"trainings"}
# ./app/views/trainings/index.html.erb:25:in `_app_views_trainings_index_html_erb___2979006340799666900_47907140'
# ./spec/views/trainings/index.html.erb_spec.rb:18:in `block (2 levels) in <top (required)>'
3) trainings/show renders attributes in <p>
Failure/Error: render
ActionView::Template::Error:
No route matches {:controller=>"trainings"}
# ./app/views/trainings/show.html.erb:10:in `_app_views_trainings_show_html_erb__2049701071134787176_46584480'
# ./spec/views/trainings/show.html.erb_spec.rb:12:in `block (2 levels) in <top (required)>'
4) trainings/new renders new training form
Failure/Error: render
ActionView::Template::Error:
undefined method `trainings_path' for #<#<Class:0x00000004a54688>:0x0000000686bec8>
# ./app/views/trainings/_form.html.erb:1:in `_app_views_trainings__form_html_erb__4221989810981841567_45123700'
# ./app/views/trainings/new.html.erb:3:in `_app_views_trainings_new_html_erb__472203814357098165_54200400'
# ./spec/views/trainings/new.html.erb_spec.rb:12:in `block (2 levels) in <top (required)>'
I won't post all tests and all layout files because it would get too big. Instead I'll post test and layout file for one view, and try to extend the correction to the others.
The test for training#index is
/spec/views/trainings/index.html.erb_spec.rb
require 'spec_helper'
describe "trainings/index" do
before(:each) do
assign(:trainings, [
stub_model(Training,
:user_id => 1,
:training => "MyText"
),
stub_model(Training,
:user_id => 1,
:training => "MyText"
)
])
end
it "renders a list of trainings" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => 1.to_s, :count => 2
assert_select "tr>td", :text => "MyText".to_s, :count => 2
end
end
/app/views/index.html.rb
<h1>Listing trainings</h1>
<table>
<tr>
<th>User</th>
<th>Training</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #trainings.each do |training| %>
<tr>
<td><%= training.user_id %></td>
<td><%= training.training %></td>
<td><%= link_to 'Show', training %></td>
<td><%= link_to 'Edit', edit_training_path(training) %></td>
<td><%= link_to 'Destroy', training, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Training', new_user_training_path(#user) %>
It indicates that routes doens't match, but they are there. Why is it happening?!
Your index view has new_user_training_path(#user), but your index spec is not creating #user. Just like you created #trainings you also need to create #user in your view spec, something like:
before(:each) do
assign(:user,
stub_model(User,
:id => 1
)
)
# ...
end
Also, the ActionView::Template::Error: undefined method 'trainings_path' for #<#<Class:...>> error is fixed by changing your new view from form_for #training to form_for [#user, #training] (nested).
Related
I'm trying to update a single databasefield of my current_user (devise).
I want to do this just with the link_to helper:
<%= link_to "Grid", edit_user_registration_path(current_user, :project_view => "grid"), :method => :put %>
But I can't get it to work since the result it allways an error:
Routing Error
No route matches [PUT] "/users/edit.1"
My Routes for Devise:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
What's wrong with the routing? I'm using devise without any custom modifications.
Thanks!
Wrong path helper:
$ rake routes
new_user_registration GET ... registrations#new
cancel_user_registration GET ... registrations#cancel
user_registration POST ... registrations#create
edit_user_registration GET ... registrations#edit
users PUT ... registrations#update # You want this one
DELETE ... registrations#destroy
Result:
<%= link_to "Grid", users_path(:user => {:project_view => 'grid'}), :method => :put, :confirm => "Are you sure?" %>
Update for your specific routes:
<%= link_to "Grid", registration_path(resource_name, :user => {:project_view => 'grid'}), :method => :put, :confirm => "Are you sure?" %>
I have an application where a user can make microposts. But enter one into the form and press post, it says No route matches [POST] "/users/1". Here is my current routes.rb file .
SampleApp::Application.routes.draw do
resources :users do
resources :comments
member do
get :following, :followers
end
end
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy] do
resources :comments
end
resources :relationships, only: [:create, :destroy]
root to: 'static_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
# The priority is based upon order of creation:
and this is the output when I run rake routes:
user_comments GET /users/:user_id/comments(.:format) comments#index
POST /users/:user_id/comments(.:format) comments#create
new_user_comment GET /users/:user_id/comments/new(.:format) comments#new
edit_user_comment GET /users/:user_id/comments/:id/edit(.:format) comments#edit
user_comment GET /users/:user_id/comments/:id(.:format) comments#show
PUT /users/:user_id/comments/:id(.:format) comments#update
DELETE /users/:user_id/comments/:id(.:format) comments#destroy
following_user GET /users/:id/following(.:format) users#following
followers_user GET /users/:id/followers(.:format) users#followers
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
micropost_comments GET /microposts/:micropost_id/comments(.:format) comments#index
POST /microposts/:micropost_id/comments(.:format) comments#create
new_micropost_comment GET /microposts/:micropost_id/comments/new(.:format) comments#new
edit_micropost_comment GET /microposts/:micropost_id/comments/:id/edit(.:format) comments#edit
micropost_comment GET /microposts/:micropost_id/comments/:id(.:format) comments#show
PUT /microposts/:micropost_id/comments/:id(.:format) comments#update
DELETE /microposts/:micropost_id/comments/:id(.:format) comments#destroy
microposts POST /microposts(.:format) microposts#create
micropost DELETE /microposts/:id(.:format) microposts#destroy
relationships POST /relationships(.:format) relationships#create
relationship DELETE /relationships/:id(.:format) relationships#destroy
root / static_pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
I guess what I really need is for [POST] users/:id to route to microposts#create, but I don't know the syntax for it.
p.s. If I add post post "users/:id", :controller => "users/update", I get the error:
ArgumentError (missing :action):
config/routes.rb:4:in `block (2 levels) in <top (required)>'
config/routes.rb:2:in `block in <top (required)>'
config/routes.rb:1:in `<top (required)>'
here is the form which is talking to the controller (i think)
<%= form_for :micropost do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
In your form_for you just need to specify url. I think this should work.
<%= form_for :micropost, :html => {:method => :post, :url => microposts_path} do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
Also let me know what is html output. In form_for :html parameter is given as a hashmap, where key are form attributes and values are attribute values.
Thanks
my routes.rb
TerritoryManagement::Application.routes.draw do
resources :addresses
resources :territories, :shallow => true do
resources :addresses
end
end
rake routes says
$ rake routes
addresses GET /addresses(.:format)
{:action=>"index", :controller=>"addresses"}
POST /addresses(.:format)
{:action=>"create", :controller=>"addresses"}
new_address GET /addresses/new(.:format)
{:action=>"new", :controller=>"addresses"}
edit_address GET /addresses/:id/edit(.:format)
{:action=>"edit", :controller=>"addresses"}
address GET /addresses/:id(.:format)
{:action=>"show", :controller=>"addresses"}
PUT /addresses/:id(.:format)
{:action=>"update", :controller=>"addresses"}
DELETE /addresses/:id(.:format)
{:action=>"destroy", :controller=>"addresses"}
territory_addresses GET /territories/:territory_id/addresses(.:format)
{:action=>"index", :controller=>"addresses"}
POST /territories/:territory_id/addresses(.:format)
{:action=>"create", :controller=>"addresses"}
new_territory_address GET /territories/:territory_id/addresses/new(.:format)
{:action=>"new", :controller=>"addresses"}
GET /addresses/:id/edit(.:format)
{:action=>"edit", :controller=>"addresses"}
GET /addresses/:id(.:format)
{:action=>"show", :controller=>"addresses"}
PUT /addresses/:id(.:format)
{:action=>"update", :controller=>"addresses"}
DELETE /addresses/:id(.:format)
{:action=>"destroy", :controller=>"addresses"}
territories GET /territories(.:format)
{:action=>"index", :controller=>"territories"}
POST /territories(.:format)
{:action=>"create", :controller=>"territories"}
new_territory GET /territories/new(.:format)
{:action=>"new", :controller=>"territories"}
edit_territory GET /territories/:id/edit(.:format)
{:action=>"edit", :controller=>"territories"}
territory GET /territories/:id(.:format)
{:action=>"show", :controller=>"territories"}
PUT /territories/:id(.:format)
{:action=>"update", :controller=>"territories"}
DELETE /territories/:id(.:format)
{:action=>"destroy", :controller=>"territories"}
in my list of territories I have the link
<td><%= link_to 'Show', new_territory_address_path %></td>
I get the error
No route matches {:controller=>"addresses", :action=>"new"}
Extracted source (around line #14):
11: <% #territories.each do |territory| %>
12: <tr>
13: <td><%= territory.name %></td>
14: <td><%= link_to 'Show', new_territory_address_path %></td>
15: <td><%= link_to 'Delete', territory, :confirm => 'Are you sure?', :method => :delete %>
16: </tr>
17: <% end %>
What am I missing?
Thomas
Have you tried passing it a territory_id? Notice how it's included in the full route.
new_territory_address_path(:territory_id => territory.id)
I am using resources :users in routes.rb. This provides the following paths as rake routes unveils.
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
Further, I comment out the legacy wild controller route.
#match ':controller(/:action(/:id(.:format)))'
To add a delete link on my users page I add the following.
<%= link_to "Delete user", user, :method => :delete, :confirm => "Are you sure?" %>
This generated the following html code.
Delete user
I click the link and it takes me to the show page?! What's wrong?
You need to include the default Javascript files for that to work properly:
<%= javascript_include_tag :defaults %>
I'm trying to add a link so the user can destroy his/her own account. I'm using the built-in registration class.
In my view I have <%= link_to 'Destroy', current_user, :confirm => 'Are you sure you want to destroy your account?', :method => :delete %> pointing to localhost:3000/users/4 by example
First of all, is that the correct link to use?
Secondly, how to redirect to root path because presently it looks like it tries to redirect to user with id 4 (and it fails because it is protected).
Rake routes gives DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
Thanks in advance.
Try
<%= link_to 'Destroy', user_registration_path, :confirm => 'Are you sure you want to destroy your account?', :method => :delete %>
It's because of devise treat registration as Singular Resource.
Besides, run rake routes and you can see details about registration routing:
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
user_registration PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
user_registration DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
It means:
user_registration_path is a helper method that returns /users(.format)
Perform DELETE request on /users(.format) will delete the registration