No route matches missing required keys: [:id] - ruby-on-rails

I'm very new to rails, and am editing code that was originally written by someone else, so I may need a more in-depth response than the average person...
I'm running into this error when I run rake:
No route matches {:controller=>"users", :action=>"show", :id=>nil, :format=>nil} missing required keys: [:id]
The line that appears to be causing the problem is this:
<%= link_to("My Account", user_path(current_user)) %>
The link actually works correctly on localhost, but is failing this test at the line "render :template":
it "renders new initiative form" do
assign(:initiative, Initiative.new(location: Location.new,rewards: [FactoryGirl.create(:reward)]))
render :template => "initiatives/new.html.erb" (...etc.)
Not sure what else is helpful to include here, but the UsersController is:
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
end
and results from rake routes:
`batch_action_admin_users POST /admin/users/batch_action(.:format) admin/users#batch_action
admin_users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
new_admin_user GET /admin/users/new(.:format) admin/users#new
edit_admin_user GET /admin/users/:id/edit(.:format) admin/users#edit
admin_user GET /admin/users/:id(.:format) admin/users#show
PATCH /admin/users/:id(.:format) admin/users#update
PUT /admin/users/:id(.:format) admin/users#update
DELETE /admin/users/:id(.:format) admin/users#destroy
batch_action_admin_comments POST /admin/comments/batch_action(.:format) admin/comments#batch_action
admin_comments GET /admin/comments(.:format) admin/comments#index
POST /admin/comments(.:format) admin/comments#create
admin_comment GET /admin/comments/:id(.:format) admin/comments#show
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_omniauth_authorize GET|POST /users/auth/:provider(.:format) omniauth_callbacks#passthru {:provider=>/twitter|facebook/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) omniauth_callbacks#(?-mix:twitter|facebook)
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
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user GET /users/:id(.:format) users#show`
Maybe someone can help?

You should use Devise login helper to login before you render the template.
https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara

Related

How to pass control from one controller to other in Rails?

I have one controller called Page_controller and another one called Categories_controller. Now whenever the home method in the Page_controller is called I wanna be redirected to the a´categories controller, as I wanna control the Category view from that moment on. This is what I have written:
class PagesController < ApplicationController
def home
#categories = Category.all
redirect_to category_url(#category)
end
end
but I get an error when I open the server saying:
undefined method `category_url' for #<PagesController:0x000000000b24ce20>
for the line:
redirect_to category_url(#category)
Does anyone know how I could pass control from one controller to the other? I am really desperate so apreciate any help you can provide
EDIT: this is my routes.rb file:
Rails.application.routes.draw do
resources :comments
#resources :categories
root to: 'pages#home'
get 'index', to: 'controller_category#index'
devise_for :users
end
And the routes printed out from the console:
C:\Users\andri\Desktop\proj>bundle exec rake routes
Prefix Verb URI Pattern Controller#Action
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
root GET / pages#home
index GET /index(.:format) controller_category#index
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
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
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
Uncomment this line in your routes.rb file:
#resources :categories
That will define a set of routes for categories, which will provide you with methods such as category_url(category).
You'll also need a CategoriesController, otherwise your next problem will be that the redirect produces an error (no such controller). And you'll need to implement CategoriesController#show.
See https://guides.rubyonrails.org/getting_started.html
and https://guides.rubyonrails.org/routing.html

Rails Redirect to an Anchor Tag from the Controller

I am using Comments table as polymorphic associations. When i save the comment and it redirects to the respective associated model. I want it to pass an anchor tag to auto scroll down to display the comments. Am trying the following but the anchor tag is not passing to the url
#comment = #commentable.comments.new comment_params
#comment.user = current_user
#comment.save
redirect_to #commentable, :anchor => '#comments'
Rake Routes
Prefix Verb URI Pattern Controller#Action
rate POST /rate(.:format) rater#create
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
PATCH /users/password(.:format) devise/passwords#update
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
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / welcome#index
artists GET /artists(.:format) artists#index
POST /artists(.:format) artists#create
new_artist GET /artists/new(.:format) artists#new
edit_artist GET /artists/:id/edit(.:format) artists#edit
artist GET /artists/:id(.:format) artists#show
PATCH /artists/:id(.:format) artists#update
PUT /artists/:id(.:format) artists#update
DELETE /artists/:id(.:format) artists#destroy
album_comments GET /albums/:album_id/comments(.:format) albums/comments#index
POST /albums/:album_id/comments(.:format) albums/comments#create
new_album_comment GET /albums/:album_id/comments/new(.:format) albums/comments#new
edit_album_comment GET /albums/:album_id/comments/:id/edit(.:format) albums/comments#edit
album_comment GET /albums/:album_id/comments/:id(.:format) albums/comments#show
PATCH /albums/:album_id/comments/:id(.:format) albums/comments#update
PUT /albums/:album_id/comments/:id(.:format) albums/comments#update
DELETE /albums/:album_id/comments/:id(.:format) albums/comments#destroy
albums GET /albums(.:format) albums#index
POST /albums(.:format) albums#create
new_album GET /albums/new(.:format) albums#new
edit_album GET /albums/:id/edit(.:format) albums#edit
album GET /albums/:id(.:format) albums#show
PATCH /albums/:id(.:format) albums#update
PUT /albums/:id(.:format) albums#update
DELETE /albums/:id(.:format) albums#destroy
song_comments GET /songs/:song_id/comments(.:format) songs/comments#index
POST /songs/:song_id/comments(.:format) songs/comments#create
new_song_comment GET /songs/:song_id/comments/new(.:format) songs/comments#new
edit_song_comment GET /songs/:song_id/comments/:id/edit(.:format) songs/comments#edit
song_comment GET /songs/:song_id/comments/:id(.:format) songs/comments#show
PATCH /songs/:song_id/comments/:id(.:format) songs/comments#update
PUT /songs/:song_id/comments/:id(.:format) songs/comments#update
DELETE /songs/:song_id/comments/:id(.:format) songs/comments#destroy
songs GET /songs(.:format) songs#index
POST /songs(.:format) songs#create
new_song GET /songs/new(.:format) songs#new
edit_song GET /songs/:id/edit(.:format) songs#edit
song GET /songs/:id(.:format) songs#show
PATCH /songs/:id(.:format) songs#update
PUT /songs/:id(.:format) songs#update
DELETE /songs/:id(.:format) songs#destroy
GET /get_albums_of_artist/:artist_id(.:format) songs#get_albums_of_artist
Remove the # symbol in your anchor string and call anchor: within the url helper method: redirect_to comments_path(anchor: 'anchor_tag')
Alright, from rails DOC url_for() method should return URL of given object.
redirect_to "#{url_for(#commentable)}#comments"
if its nested then url_for([#product, #comment])

Ruby app: link_to 'delete' looking for GET

I realize this question has been asked many times, but I've tried all the solutions I can find, and have gotten nowhere.
In my view, I have
= link_to "Delete", evint_path(e), :method => :delete, data: {:confirm => 'Are you sure?'}
where e is an instance of evint. I keep getting error messages declaring that I'm missing a GET method for evint:
No route matches [GET] "/evints/5"
This isn't even the case! When I rake routes, I have a GET route for evints.
I have jquery-rails in my Gemfile; I have "require jquery_ujs" in my application.js file. I have five other apps on the same system that work, so I don't think it's a system problem.
The html being generated by my code looks OK:
<a rel="nofollow" href="/evints/5" data-method="delete" data-confirm="Are you sure?">Delete</a>
I've tried removing "//= require tree ." from my application.js file, but that didn't make any difference.
I'm also having trouble signing out of my app, as devise's destroy_session also isn't working!
Does anybody have any ideas? This is driving me crazy!
EDIT: Here are my routes:
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
PATCH /users/password(.:format) devise/passwords#update
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
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
interests GET /interests(.:format) interests#index
POST /interests(.:format) interests#create
new_interest GET /interests/new(.:format) interests#new
edit_interest GET /interests/:id/edit(.:format) interests#edit
interest GET /interests/:id(.:format) interests#show
PATCH /interests/:id(.:format) interests#update
PUT /interests/:id(.:format) interests#update
DELETE /interests/:id(.:format) interests#destroy
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PATCH /events/:id(.:format) events#update
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
images GET /images(.:format) images#index
POST /images(.:format) images#create
new_image GET /images/new(.:format) images#new
image GET /images/:id(.:format) images#show
DELETE /images/:id(.:format) images#destroy
evints GET /evints(.:format) evints#index
POST /evints(.:format) evints#create
new_evint GET /evints/new(.:format) evints#new
evint DELETE /evints/:id(.:format) evints#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
projectimages GET /projectimages(.:format) projectimages#index
POST /projectimages(.:format) projectimages#create
new_projectimage GET /projectimages/new(.:format) projectimages#new
projectimage GET /projectimages/:id(.:format) projectimages#show
DELETE /projectimages/:id(.:format) projectimages#destroy
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
welcome_readpdf GET /welcome/readpdf(.:format) welcome#readpdf

link_to not passing params properly

the view
<td><%= link_to "+1", :controller => :profiles, :action => :addpoints, :profile_id => profile.id, :task_id => #task.id%></td>
When trying to access profile.id via :profile_id rails returns an error, the id was not passed at all. How do I fix this? What's the best way to pass two params through two models? rake routes
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_dashboard GET /static_pages/dashboard(.:format) static_pages#dashboard
static_pages_pending GET /static_pages/pending(.:format) static_pages#pending
static_pages_taskcompleted GET /static_pages/taskcompleted(.:format) static_pages#taskcompleted
tasks_index GET /tasks/index(.:format) tasks#index
tasks_complete GET /tasks/complete(.:format) tasks#complete
tasks_delete GET /tasks/delete(.:format) tasks#delete
profiles_addpoints GET /profiles/addpoints(.:format) profiles#addpoints
profiles_index GET /profiles/index(.:format) profiles#index
root GET / static_pages#home
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
PATCH /users/password(.:format) devise/passwords#update
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
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
tasks GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
new_task GET /tasks/new(.:format) tasks#new
edit_task GET /tasks/:id/edit(.:format) tasks#edit
task GET /tasks/:id(.:format) tasks#show
PATCH /tasks/:id(.:format) tasks#update
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
profiles GET /profiles(.:format) profiles#index
POST /profiles(.:format) profiles#create
new_profile GET /profiles/new(.:format) profiles#new
edit_profile GET /profiles/:id/edit(.:format) profiles#edit
profile GET /profiles/:id(.:format) profiles#show
PATCH /profiles/:id(.:format) profiles#update
PUT /profiles/:id(.:format) profiles#update
DELETE /profiles/:id(.:format) profiles#destroy
Replace profile.id with #profile.id. Verify that profile is actually an instance. A nil value may be being passed in (as opposed to things being wired incorrectly).
Can we see the method content ?
If the error is about the id, the route is correct. What's in params[:profile_id] ? We need more info here..
I think this is what you wanted to do in Rails 4 way.
<%= link_to "+1", profiles_addpoints_path(:profile_id => profile.id, :task_id => #task.id) %>
That code above will call addpoints method in profiles controller with parameter {:profile_id => profile.id, :task_id => #task.id}. You can access parameter like this.
params[:profile_id] and params[:task_id].
Another thing that's worrysome about your code is that in the routes.rb, profiles_addpoints route is called via GET HTTP verb. This is potentially dangerous because Google search bots will visit these paths and result in a spike in upvotes. Respect HTTP convention and use PUT.

Rails routing to the wrong controller action

Here is the output of 'rake routes'
$ rake routes
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
admin_root /admin(.:format) admin/dashboard#index
admin_dashboard /admin/dashboard(.:format) admin/dashboard#index
admin_user PUT /admin/users/:id(.:format) admin/users#update
batch_action_admin_users POST /admin/users/batch_action(.:format) admin/users#batch_action
admin_users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
new_admin_user GET /admin/users/new(.:format) admin/users#new
edit_admin_user GET /admin/users/:id/edit(.:format) admin/users#edit
GET /admin/users/:id(.:format) admin/users#show
PUT /admin/users/:id(.:format) admin/users#update
DELETE /admin/users/:id(.:format) admin/users#destroy
root / home#index
And here is an excerpt from the logs
Started POST "/admin/users/batch_action" for 127.0.0.1 at 2013-01-18 23:07:07 +0530
Processing by Admin::UsersController#create as HTML
Why is /admin/users/batch_action getting routed to Admin::UsersController#create, when the routes shows
batch_action_admin_users POST /admin/users/batch_action(.:format) admin/users#batch_action
I am using ActiveAdmin and these are the routes that it generates.
It seems like you are using resources for routing admin/users controller, so POST http verb is defaulted to create action in the controller.
If you would like to add another restful POST controller action, do something like this,
scope "admin" do
resources :users do
member do
post 'batch_action'
end
end
end
I don't know how your routes.rb looks like, so it might look different in your routes.rb file. But it should look similar.

Resources