Connection between controller and view in rails - ruby-on-rails

In users_controller.rb I've got my method:
def showallusers
#users = User.all
respond_to do |format|
format.html # showallusers.html.erb
format.json { render :json => #users }
end
end
and in app/views/users I've got showallusers.html.erb and in it:
<p>test</p>
but when I type
http://localhost:3000/users/showallusers
in browser, it shows me show.html.erb instead.
My routes.rb
resources :users
resources :projects do
resources :issues
end
#resources :issues
resources :projects
resources :sessions
root :to => "users#index"
match "/auth/:provider/callback" => "sessions#create"
match "/signout" => "sessions#destroy", :as => :signout
Do you know, what is wrong and how can I fix it? thanks

In your routes.rb:
resources :users do
collection do
get :showallusers # why not :show_all_users? Isn't it more readable?
# you may also need
# post :showallusers
end
end
Then restart your server to make sure the new url-helpers are generated (the new helper should be showallusers_users_path)

Related

Error trying to logout: Couldn't find User with 'id'=sign_out

I'm getting this error when I try to logout an user. I checked many posts of this same error but no one solved my error, I hope you can help me.
The error is as follows:
ActiveRecord::RecordNotFound in UsersController#destroy
Couldn't find User with 'id'=sign_out
The following is my code:
users_controller.rb
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to unauthenticated_root_path }
format.json { head :no_content }
end
end
private
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
view/users/index.html.erb
<li>
<%= link_to "Logout", destroy_user_session_path, :method => :delete %>
</li>
routes.rb
Rails.application.routes.draw do
get 'admin/index'
resources :contacts
resources :afections
resources :injuries
resources :allergies
resources :trainers
resources :idusuarios
resources :diseases
resources :weights
resources :diets
resources :exercices
resources :profiles
resources :users
devise_for :users
get '/users/sign_out' => 'devise/sessions#destroy'
devise_scope :user do
authenticated :user do
root 'pagina#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
end
move get '/users/sign_out' => 'devise/sessions#destroy' above resources :users in your routes. Routes are given priority in terms of their order in the routes file.
When you use resources :users in your routes, you have this routes:
/users
/users/:id
/users/:id/edit
and ...
So when /users/sign_out is called, , thinks the sign_out is an ID.
so change /users/sign_out to /user/sign_out for example.
BUT
if you want customize your session paths, you can do this:
customize your session paths:
as :user do
get 'user/signin' => 'devise/sessions#new', :as => :new_user_session
post 'user/signin' => 'devise/sessions#create', :as => :user_session
delete 'user/signout' => 'devise/sessions#destroy', :as => :destroy_user_session, via: [:delete, :get]
end
and skip session in devise:
devise_for :users, :skip => [:sessions]
if you want user registration, you must skip it in devise and then can use user scaffold:
as :user do
get 'user/signin' => 'devise/sessions#new', :as => :new_user_session
post 'user/signin' => 'devise/sessions#create', :as => :user_session
delete 'user/signout' => 'devise/sessions#destroy', :as => :destroy_user_session, via: [:delete, :get]
end
scope "admin" do
resources :users
devise_for :users, :skip => [:sessions, :registrations]
end

routes error in rails

I am working on a project. I am making an application where a user can add an issue (like a post) and the user can comment on it.
on running this application, I get an error
Couldn't find Issue with 'id'=show
the code for routes file is
resources :issues do
resources :comments
end
get 'users/new'
get 'users/create'
get 'users/show'
get 'users/edit'
get 'issues/show/:id', :to => 'issues#show'
resources :users
resources :sessions, :only => [:create, :new,:destroy]
get '/signup', :to => 'users#new'
get '/signin' , :to => 'sessions#new'
get '/signout', :to => 'sessions#destroy'
the code for the issues controller is
class IssuesController < ApplicationController
def new
#issue = Issue.new
end
def create
#issue = Issue.new(issues_params)
if #issue.save
flash[:success]='your issue has been raised'
redirect_to :controller => 'issues', :action => 'show', :id => #issue.id
else
render 'new'
end
end
def edit
#issue = Issue.find(params[:id])
end
def update
#issue = Issue.find(params[:id])
if #issue.update_attributes(issues_params)
redirect_to :controller => 'issues', :action => 'show', :id => #issue.id
else
render 'edit'
end
end
def index
#issues = Issue.all
end
def show
#issue = Issue.find(params[:id])
end
def destroy
#issue=Issue.find(params[:id])
#issue.destroy
redirect_to :controller => 'issues', :action => 'index'
end
protected
def issues_params
params.require(:issue).permit(:title,:content)
end
end
the code for the comments controller from where I call the show method in issues controller is
class CommentsController < ApplicationController
def create
#issue = Issue.find(params[:issue_id])
#comment = #issue.comments.create(comment_params)
if #comment.save
redirect_to :controller => 'issues', :action => 'show', :id => #issue[:id]
else
render 'new'
end
end
private
def comment_params
params.require(:comment).permit(:content)
end
end
You must be trying to request the URI /issues/show? This will map to the GET /issues/:id from the resources :issues do line of your routes. The router will set the params[:id] to the string "show" and send the request to the show action of the IssuesController which, as you've shown, will then try to do Issue.find(params[:id]) ie. Issue.find("show") and hence you get your error.
Change this
resources :issues do
resources :comments
end
to
resources :issues, except: [:show] do
resources :comments
end
It will resolve your problem!

Rails - passing object's id

I'm creating a message-board site using ruby on rails.
I generated two scaffolds: Topic and Forum. Topic belongs_to Forum.
when the user is creating a new topic, I should pass the forum_id (a GET var). something like:
http://example.com:3000/topics/new/1
and then, when the user submit the form he passes back the forum_id with the POST request (through hidden html field?).
what is the right way doing it?
thanks
routes:
resources :forums
get "admin/index"
resources :posts
resources :topics
resources :users
match '/signup', :to => 'users#new'
get '/login', :to => 'sessions#new', :as => :login
match '/auth/:provider/callback', :to => 'sessions#create'
match '/auth/failure', :to => 'sessions#failure'
match '/topics/new/:id', :to => 'topics#new'
A good way to do it is to nest topics resources inside forums resources like this:
resources :forums do
resources :topics
end
Then in your TopicsController
class TopicsController < ApplicationController
def new
#forum = Forum.find params[:forum_id]
#topic = Topic.new
end
def create
#forum = Forum.find params[:forum_id] # See the redundancy? Consider using before_filters
#topic = #forum.topics.build params[:topic]
if #topic.save
redirect_to #topic
else
render action: :new
end
end
end
And finally in your views/topics/_form.html.erb:
<%= form_for [#forum, #topic] do |f| %>
# Your fields
<% end %>

Uninitialized Constant Error in Nested Controller

I am getting this error:
Routing Error
uninitialized constant RegistrationsController
This is my log:
Started GET "/registrants/1/registrations/new" for 127.0.0.1 at 2012-07-03 00:55:44 -0500
ActionController::RoutingError (uninitialized constant RegistrationsController):
Rendered /.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (8.5ms)
This is the part of my routes.rb that is appropriate:
registrant_registrations GET /registrants/:registrant_id/registrations(.:format) {:action=>"index", :controller=>"registrations"}
POST /registrants/:registrant_id/registrations(.:format) {:action=>"create", :controller=>"registrations"}
new_registrant_registration GET /registrants/:registrant_id/registrations/new(.:format) {:action=>"new", :controller=>"registrations"}
edit_registrant_registration GET /registrants/:registrant_id/registrations/:id/edit(.:format) {:action=>"edit", :controller=>"registrations"}
registrant_registration GET /registrants/:registrant_id/registrations/:id(.:format) {:action=>"show", :controller=>"registrations"}
PUT /registrants/:registrant_id/registrations/:id(.:format) {:action=>"update", :controller=>"registrations"}
This is my Registrations Controller, which is at /app/controllers/portal/registrations_controller.rb:
class Portal::RegistrationsController < ApplicationController
def create
#registrant = current_member.registrants.find(params[:registrant])
#registration = #registrant.registrations.build
respond_to do |format|
if #registration.save
format.html { redirect_to root_path, :notice => "Registration successfully created!" }
format.json { head :ok }
else
format.html { redirect_to root_path, :notice => "There was a problem with the registration." }
end
end
end
end
I get this error when I call new_registrant_registration_path(registrant) from a view in Portal#index.
Edit:
For completeness, here is my routes.rb file in its entirety before the solution was found. Radar mentioned the gist I posted, but I figured I would put the code here in case that gist is deleted.
MyApp::Application.routes.draw do
match 'admin' => 'admin/dashboard#index', :as => :admin_root
match 'portal' => 'portal#index', :as => :member_root
namespace 'admin' do
match 'profile' => 'profile#show', :as => :profile
resources :members
resources :admins
resources :packages
resources :products
resources :levels
resources :lessons
resources :registrations
resources :registrants, :controller => 'customers/registrants'
resources :locations
resources :schools
resources :terms
resources :customers
resources :invoices
resources :payments
end
namespace 'member' do
resources :registrations
end
match 'register' => 'member/registrations#new', :as => :signup
match 'login' => 'member_sessions#new', :as => :login
match 'logout' => 'member_sessions#destroy', :as => :logout
match 'admin/login' => 'admin_sessions#new', :as => :admin_login
match 'admin/logout' => 'admin_sessions#destroy', :as => :admin_logout
match 'member/activate/:activation_code' => 'member/activations#create', :as => :member_activation
match 'member/:id/activate/resend' => 'member/activations#resend', :as => :resend_member_activation
match 'member/:id/activate' => 'member/activations#new', :as => :new_member_activation
match 'member/password/reset/begin' => 'member/password_resets#new', :as => :new_member_password_reset, :via => :get
match 'member/password/reset' => 'member/password_resets#create', :as => :member_password_resets, :via => :post
match 'member/password/:token/finish' => 'member/password_resets#edit', :as => :edit_member_password_reset, :via => :get
match 'member/password/reset' => 'member/password_resets#update', :as => :member_password_reset, :via => :put
match 'admin/packages/new/:partial' => 'admin/packages#new'
resources :admins
resources :registrants, :controller => 'portal/registrants' do
resources :registrations
end
resource :admin_session
resource :member_session
root :to => 'portal#index'
end
To make things clear: marcamillion came into #rubyonrails and Freenode and shared his config/routes.rb. That's where I figured out the error that he was making and then figured out the answer.
Two things.
First: Admin root route
Rather than defining the root route for your admin namespace like this:
match 'admin' => 'admin/dashboard#index', :as => :admin_root
Define it inside the namespace :admin call like this:
namespace 'admin' do
root :to => "dashboard#index"
end
This will automatically define it as admin_root_path and admin_root_url, so you won't need to do that, and it will automatically prefix the controller with the admin/ prefix, so you won't need to do that either.
Second: Referencing RegistrantsController
You have this currently in your config/routes.rb:
resources :registrants, :controller => 'portal/registrants' do
resources :registrations
end
What this will do is define resources routes for registrants correctly using the Portal::RegistrantsController, but won't define the nested routes underneath this using Portal::RegistrationsController. What it will do is attempt to use RegistrationsController, which is why you're getting that error.
To fix this, I'd recommend you use the scope method, like this:
scope :module => Portal do
resources :registrants do
resources :registrations
end
end
The scope method, when used in this way, will tell Rails that the controllers for the routes inside the block are under the Portal namespace. This means that it will know that resources :registrants is for Portal::Registrants and that resources :registrations is for Portal::Registrations, thereby fixing the error that you're seeing.
What if you change this line:
class Portal::RegistrationsController < ApplicationController
to:
class RegistrationsController < ApplicationController
?
This is strange. I think, your controller should be located at /app/controllers/registrants/registrations_controller.rb and looks like this:
class Registrants::RegistrationsController < Registrants::ApplicationController
def create
#registration = registrant.registrations.build
...
end
end
class Registrants::ApplicationController < ApplicationController
def registrant
current_member.registrants.find(params[:registrant_id])
end
end
Registrants::ApplicationController (/app/controllers/registrants/application_controller.rb) controller aim is to avoid code duplication.

Reload template after error

When I am in tickets/new and create a new ticket with errors, my URL gets reconstructed to tickets/. How can I stay on the same page and reload the page with errors instead of redirecting?
In my ticket controller:
if #ticket.valid?
#ticket.save
redirect_to tickets_path
else
render :template => 'tickets/new'
end
UPDATE
routes.rb:
devise_for :users
root :to => 'tickets#index'
resources :users do
resources :tickets
end
resources :tickets
Instead of:
render :template => 'tickets/new'
try this:
render 'new'

Resources