How to pass control from one controller to other in Rails? - ruby-on-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

Related

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])

No route matches missing required keys: [:id]

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

Controller with single action as root

I have problem with pointing as root controller which has only one action called index. My current routes.rb looks like this:
get 'dashboard', to: 'dashboard#index'
resources :categories
resources :projects do
resources :issues
resources :attachments
end
devise_for :users
devise_scope :user do
authenticated :user do
root :to => 'dashboard#index'
end
unauthenticated :user do
root :to => 'devise/sessions#new'
end
end
Any my controller:
class DashboardController < ApplicationController
authorize_resource
def index
#test = 'test'
end
end
But each time I enter localhost:3000 I get "NameError at /
uninitialized constant Dashboard". What am I doing wrong?
Thanks in advance!
edit:
rake routes shows this:
dashboard GET /dashboard(.:format) dashboard#index
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
project_issues GET /projects/:project_id/issues(.:format) issues#index
POST /projects/:project_id/issues(.:format) issues#create
new_project_issue GET /projects/:project_id/issues/new(.:format) issues#new
edit_project_issue GET /projects/:project_id/issues/:id/edit(.:format) issues#edit
project_issue GET /projects/:project_id/issues/:id(.:format) issues#show
PUT /projects/:project_id/issues/:id(.:format) issues#update
DELETE /projects/:project_id/issues/:id(.:format) issues#destroy
project_attachments GET /projects/:project_id/attachments(.:format) attachments#index
POST /projects/:project_id/attachments(.:format) attachments#create
new_project_attachment GET /projects/:project_id/attachments/new(.:format) attachments#new
edit_project_attachment GET /projects/:project_id/attachments/:id/edit(.:format) attachments#edit
project_attachment GET /projects/:project_id/attachments/:id(.:format) attachments#show
PUT /projects/:project_id/attachments/:id(.:format) attachments#update
DELETE /projects/:project_id/attachments/:id(.:format) attachments#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 GET /projects/:id(.:format) projects#show
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
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
root / dashboard#index
root / devise/sessions#new

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.

Weird issue with my rails path and its routing

I'm getting a strange error with my routing. I've installed the blogit gem but I don't think this is causing this problem.
undefined local variable or method `locations_path'
Here is my routes file:
AppName::Application.routes.draw do
devise_for :users
root :to => 'locations#index'
mount Blogit::Engine => "/blog", :as => "blog"
resources :locations do
collection do
get 'location'
end
end
and here's my 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
root / locations#index
blog /blog Blogit::Engine
location_locations GET /locations/location(.:format) locations#location
locations GET /locations(.:format) locations#index
POST /locations(.:format) locations#create
new_location GET /locations/new(.:format) locations#new
edit_location GET /locations/:id/edit(.:format) locations#edit
location GET /locations/:id(.:format) locations#show
PUT /locations/:id(.:format) locations#update
DELETE /locations/:id(.:format) locations#destroy
Routes for Blogit::Engine:
/posts/page/:page(.:format) blogit/posts#index
tagged_blog_posts /posts/tagged/:tag(.:format) blogit/posts#tagged
post_comments POST /posts/:post_id/comments(.:format) blogit/comments#create
post_comment DELETE /posts/:post_id/comments/:id(.:format) blogit/comments#destroy
posts GET /posts(.:format) blogit/posts#index
POST /posts(.:format) blogit/posts#create
new_post GET /posts/new(.:format) blogit/posts#new
edit_post GET /posts/:id/edit(.:format) blogit/posts#edit
post GET /posts/:id(.:format) blogit/posts#show
PUT /posts/:id(.:format) blogit/posts#update
DELETE /posts/:id(.:format) blogit/posts#destroy
root / blogit/posts#index
Any suggestions as to why this may be happening would be great.
Thanks,
James
This was an issue with my blogit configuration.
People can find the full thread here:
https://github.com/KatanaCode/blogit/issues/8
Thanks for all your help!
James

Resources