I have users through devise. So that is my user model.
I have home controller
class HomeController < ApplicationController
before_action :authenticate_user!
def index
#users = User.all
end
def show
#user = User.find(params[:id])
end
end
My log in is fine. My index page works fine.
My show page to show logged in user's tweets...
<h1>Profile Page for <%= #user.email %></h1>
<p><%= #user.email %></p>
<% #user.tweets.each do |tweet| %>
<p><%= tweet.content %></p>
<% end %>
when I try to go to localhost:3000/users/1
undefined method `email' for nil:NilClass (error on first line)
My routes
Rails.application.routes.draw do
get 'home/index'
get 'home/show'
devise_for :users
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root to: 'home#index'
match 'users/:id' => 'home#show', via: :get
end
How can I resolve this?
Related
I'm receiving this error message:
No route matches [POST] "/users/9"
I'm trying to figure out a way to have the update method of one controller and use it in the view of a show method of another controller. This is what my route file looks like.
routes.rb
Rails.application.routes.draw do
root 'dashboards#index'
devise_for :users
resources :users, only: [:show]
patch '/users/:id' => 'companyinfos#update'
put '/users/:id' => 'companyinfos#update'
resources :dashboards, only: [:index]
end
I'm finding the correct companyinfo through the users controller
users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#companyinfo = Companyinfo.find(current_user.id)
end
end
and I'm saying to use this method inside of my company_info controller
companyinfos_controller.rb
class CompanyinfosController < ApplicationController
def update
#companyinfo = Companyinfo.find(current_user.id)
if #companyinfo.update(companyinfo_params)
flash[:notice] = "Saved ..."
else
flash[:alert] = "cannot save"
end
render 'edit'
end
private
def companyinfo_params
params.require(:companyinfo).permit(:CompanyStage)
end
end
and finally I'm using form_for to update it on users_path
show.html.erb
<%= form_for :companyinfo do |f| %>
<%= f.text_field :CompanyStage %>
<%= f.submit "save", class: "btn btn-success" %>
<% end %>
Updating the information in the database works fine when I update it through companyinfo/:id/edit but if I try to update it on another view such as users/:id I'm getting the error message above. What is the proper way to go about this?
UPDATE
Actually if I just do what the error message is telling me it works. Miracle right? Another question. Is there another way to have multiple routes from different controllers pointing to the same location? Or is this the best way?
Error says: No route matches [POST]
Update routes:
post '/users/:id' => 'companyinfos#update'
Or form method:
<%= form_for :companyinfo, method: :patch do |f| %>
I have a users model and controller. I am trying to make a put and delete request to my controller where the put request is tied to users#allow and the delete request is suppose to be directed to users#destroy. I However keep on getting this error when I make the request:
No route matches [GET] "/users/1/allow"
I am also using devise for users and admin classes so that might have something to do with this error. Im not really sure. Here is my routes.rb:
Rails.application.routes.draw do
get 'users/index'
put 'users/:id/allow', :to => 'users#allow', :as=>:users_allow
delete 'users/:id/destroy', :to => 'users#destroy',:as => :users_destroy
devise_for :users
devise_for :admins
root 'website#show'
resources :tweets do
put '/pass', :to => 'tweets#pass',:as => :pass
put '/fail', :to => 'tweets#fail',:as => :fail
end
get '/to_json', :to=>'tweets#to_json'
end
Here is my users controller:
class UsersController < ApplicationController
before_action :find_user, only:[:allow,:destroy]
before_filter :authenticate_admin!
def index
#users = User.all
end
def allow
find_user.update(:edit_permission=>true)
redirect_to(:back)
end
def destroy
find_user.destroy
redirect_to(:back)
end
private
def find_user
#user = User.find(params[:user_id])
end
end
And here is my users view where I am calling from, users/index.html.erb:
<h1>Users#index</h1>
<p>Find me in app/views/users/index.html.erb</p>
<ul>
<% #users.each do |user|%>
<% if user.edit_permission == nil%>
<li style="color:red"> <%=link_to 'approve', users_allow_path(user), method: :put %> <%=link_to 'delete', users_destroy_path(user), method: :delete %><%= "#{user.name} #{user.email}"%> </li>
<% else%>
<li style="color:green"><%=link_to 'delete', users_destroy_path(user), method: :delete %><%= "#{user.name} #{user.email}"%> </li>
<% end%>
<%end%>
</ul>
I also want to add that when I go into the view and I do an inspect element in my browser for the link for the put request I do see this:
<a rel="nofollow" data-method="put" href="/users/1/allow">approve</a>
I would run rake routes to see your actual routes, because in your routes.rb you actually don't have a route that matches a get with users/:id/allow
#config/routes.rb
resources :users, only: [:index, :destroy] do
put :allow, action: :allow #-> url.com/users/:user_id/allow
end
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
#users = User.all
end
def allow
user.update edit_permission: true
redirect_to :back
end
private
def user
User.find params[:user_id] #-> why return an instance var if you aren't going to use it
end
end
#app/views/users/index.html.erb
<ul>
<% #users.each do |user| %>
<% options = {} %>
<% options.allow = ["allow", "red", "put"] %>
<% options.destroy = ["destroy", "green", "delete"] %>
<% option = user.edit_permission ? options.allow : [options.allow, options.destroy] %>
<% option.each do |action,colour,method| %>
<%= link_to action, eval("users_#{action}_path(user)"), method: method, class: colour %><%= "#{user.name} #{user.email}") %>
<% end %>
<% end %>
</ul>
--
To fix your actual problem, you need to make sure you have your Rails UJS driver loaded:
#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require_tree .
I am fairly new to Rails and am using Devise for user authentication in a new demo app. I have got Devise to work, along with the confirmable module and email verification.
I am now trying to build a phone verification on top of this. I updated the User model to have three additional fields: phone_number, phone_verified (boolean) and phone_verification_code. I also updated the Registrations Controller to allow the additional field of phone_number by the User model.
Now, in order to set up a phone verification system, I have created a page /user/:id/verify by adding a verify method in UsersController and updating the routes.rb. When I type in the URL, http://localhost:3000/users/10/verify, I see the view for that page.
However, I am trying to get to the view by creating a button on the /app/views/users/show.html.erb and my code below is getting an error. I am trying to get the correct path helper for the button. Can someone please help.
Here is the error:
Showing /home/ubuntu/work/depot/app/views/users/show.html.erb where line #10 raised:
undefined method `verify_user_path' for #<#:0x007fc3740fe3a0>
app/views/users/show.html.erb
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-md-offset-2 col-md-8">
<div class="panel panel-default">
<div class="panel-heading"><%= #user.email %></div>
<div class="panel-body">
<strong>Phone number: </strong><%= #user.phone_number %><br/>
<% if #user.phone_verified == nil %>
<%= button_to [:verify, #user] do %>
Verify Phone <strong><%= #user.phone_number %></strong>
<% end %>
<% else %>
<strong>Phone Verified: </strong><%= #user.phone_verified %><br/>
<% end%>
</div>
</div>
</div>
</div>
</div>
UsersController
class UsersController < ApplicationController
def index
#users = User.all
end
def show
begin
#user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access an invalid user: #{params[:id]}"
redirect_to store_url, notice: "Attempt to access an invalid user: #{params[:id]}"
else
respond_to do |format|
format.html # show html.erb
format.json { render json: #user }
end
end
end
def verify
end
def generate_pin
#user.phone_verification_code = rand(0000..9999).to_s.rjust(4, "0")
save
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
#user = nil
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params[:user]
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "registrations" }
resources :users, only: [:index, :show, :edit, :update]
resources :orders
resources :line_items
resources :carts
match "users/:id/verify" => "users#verify", via: [:get]
get 'store/index'
resources :products
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'store#index', as: 'store'
You need to use the as option in your route:
match "users/:id/verify" => "users#verify", via: [:get], as: 'verify_user'
Then restart your server. This will tell Rails to create the proper URL helper methods: verify_user_path and verify_user_url
match "users/:id/verify" => "users#verify", as: 'verify_user', via: [:get]
Or better way:
resources :users, only: [:index, :show, :edit, :update] do
get :verify
end
Using Rspec/Device/Cucumber template app
Upon logging in I wish the user to be redirected to the users#index
my routes file
Engage::Application.routes.draw do
authenticated :user do
root to: 'users#show'
end
root :to => "home#index"
devise_for :users
resources :users, only: [:index]
resources :agendas, only: [:create, :destroy]
end
Upon logging in I get a error
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User without an ID
My users_controller
class UsersController < ApplicationController
before_filter :authenticate_user!
def index
#user = current_user
#agendas = #user.agendas
end
end
I've tried adding "#user = User.find(params[:id])" instead of using the current_user helper method but i'm still having issues
my show view in my users folder under views is
<div class="row">
<div class="span8">
<% if #user.agendas.any? %>
<h2>Agendas (<%= #user.agendas.count %>)</h2>
<ol class="agendas">
<%= render #agendas %>
</ol>
<% end %>
</div>
</div>
To redirect user after log in, did you see this help :
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in-out
I'm trying to create a User show page (that will function as a profile page) but am confused about how to do this with Devise. It doesn't seem as though Devise comes with any sort of show definition - is there any way I can access the controllers Devise is implementing in order to make one or do I have to override them?
You should generate a users_controller which inherits from application_controller and define there your custom show method. Don't forget to create a view and routes for it.
Ex:
#users_controller.rb
def show
#user = User.find(params[:id])
end
#in your view
<%= #user.name %>
#routes.rb
match 'users/:id' => 'users#show', via: :get
# or
get 'users/:id' => 'users#show'
# or
resources :users, only: [:show]
Don't forget that your users routes should be below the devise_for users routes, like this:
#routes.rb
devise_for :users
resources :users, :only => [:show]
Also, if you are using a username or an email as the primary key instead of the usual id, you should avoid routing conflicts by declaring your routes as follow:
#routes.rb
devise_for :users, :path_prefix => 'd'
resources :users, :only => [:show]
showing current_user/ other_user profiles with devise:
After installing devise
Create a Users controller:
rails generate controller Users
Then create a show action and find the user with params id:
def show
#user = User.find(params[:id])
end
Create a show.html.erb file in the User view folder:
<%= #user.email %>
Linking to users show page:
<%= link_to "current_user_show", current_user %>
Now if you want to view other profiles create a index action in the users controller:
def index #users = User.all end
Create an index.html.erb in the User view folder then:
<% #users.each do |user| %>
<%= link_to user.username, user %>
<%= user.email %>
<% end %>
The link for this will be:
<%= link_to "show_index_of_users", users_path %>
This will link you to the users index.html.erb file there you will create a loop and link to users profile:
<% #users.each do |user| %>
<%= link_to user.username, user %>
<%= user.email %>
<% end %>
This should work!
you can generate the views used by devise, so you can change it as you want to.
rails g devise:views