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
Related
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?
I'm using the mailboxer gem to build a messaging system between users for my rails application. For some reason, I'm getting this error:
(undefined method `receipts_for' for nil:NilClass)
Maybe it's because I should define 'receipts_for' somewhere in my controller or in the mailboxer.rb? I tried a few things.. but unfortunately none of them were successful.
This is my routes.rb:
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
get 'search/index'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :conversations do
resources :messages
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :searches
end
This is my conversations controller:
class ConversationsController < ApplicationController
def show
#conversation = current_user.mailbox.conversation.find(params[:id])
end
def new
#recipients = User.find(params[:user_id])
end
def create
recipient = User.find(params[:user_id])
receipt = current_user.send_message(recipient, params[:body])
redirect_to conversation_path(receipt.conversation)
end
end
This is my messages_controller:
class MessagesController < ApplicationController
before_action :set_conversation
def create
receipt = current_user.send_message(#conversation, body)
redirect_to receipt.conversation
end
private
def set_conversation
#conversation = current_user.mailbox.conversations.find(params[:conversation_id])
end
end
The messaging system I'm building precisely, is a messaging system where the 'current_user' can go to the profile page of any user on the application and message him/her via the profile page. So this means that I'm rendering show.html.erb of the conversation model inside the show.html.erb of my users model. Here is the code of both views:
show.html.erb - Users Model:
<% provide(:title, #user.name) %>
<div class="row">
<aside class="col-md-4">
<section>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
<section class="stats">
<%= render 'shared/stats' %>
</section>
<h6>
<%= #user.gender %>
</h6>
<%= render "conversations/conversation" %>
</aside>
<div class="col-md-8">
<%= render 'follow_form' if logged_in? %>
<% if #user.microposts.any? %>
<h3>Microposts (<%= #user.microposts.count %>)</h3>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
<% end %>
</div>
</div>
_conversation.html.erb - show view of the conversations model.
<% #conversation.receipts_for(current_user).each do |receipt| %>
<div>
<%= receipt.message.body %>
</div>
<% end %>
<%= form_tag conversation_messages_path(#conversation), method: post do %>
<div>
<%= text_area_tag :body %>
</div>
<%= submit_tag %>
<% end %>
So how exactly can I define the 'receipts_for' method for my mailboxer? Or is something else wrong with my code?
Any help would be much appreciated!
Thanks!
I’ll piggy-back on what #mu is too short said. It’s important to pay attention to the whole error message. “undefined method receipts_for for nil:NilClass” is telling you that the receiver of the receipts_for method is nil. #conversation.receipts_for(current_user) looks to be the only place where receipts_for is used, so I would start debugging by making sure #conversation is assigned the value of an existing ActiveRecord-backed conversation object.
There seems to be too a lot going on, so I don't know how to offer you a quick fix. The value assignment in your ConversationsController#show method, #conversation= current_user.mailbox.conversation.find(params[:id]) looks cumbersome. That suggests to me that you're looking for a conversation based on a mailbox belonging to the current_user, which might be what you want (in which case, you need to have the appropriate associations defined in your models).
However, since the chain ends with conversation.find(params[:id]), I'm guessing that current_user.mailbox is not needed. Alternatively, if your params don't actually have a conversation id, then maybe that's what you need to focus on.
The good news is you can probably figure out how to define #conversation if you stick byebug (or binding.pry, depending on what you have installed) at the top of your show method and in your view partial:
# In the controller:
def show
binding.pry
#conversation = current_user.mailbox.conversation.find(params[:id])
end
# in the view
<% binding.pry %>
Was trying to set up a ranking score for my users depending on their like counts.
I was able to get this to work for current_user.like.count but for some reason when I want it to be for user.like.count [so that its not the same one for everyone] my app crashes and gives me this error message: "Undefined method `likes' for nil:NilClass" I have put all my relevant code below as well as my github for this. Any help would be amazing.
Github Url: https://github.com/OmarZV/TY2
_rankings.html.erb
<% if current_user.likes.count >3 %>
A Ranking
<% elsif %>
<% current_user.likes.count == 2%>
B Ranking
<% else %>
C Ranking
<% end %>
users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!
def index
#users = User.all
end
def show
#users = User.find(params[:id])
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
resources :users, :controllers => "users_controller.rb"
resources :users do
resource :ranking, module: :users
end
resources :posts do
resource :like, module: :posts
end
root to: "posts#index"
end
Index.html.erb
<h1 class="page-header">Platform Users</h1>
<% #users.each do |user| %>
<strong><%= user.username %></strong>
<div class="round-image-50"><%= image_tag(user.avatar.url(:thumb)) %></div>
div id="user_<%= #user_id%>_rankings">
<%= render partial: "rankings", locals: {user: #user} %>
</div>
<% end %>
Moving comment to answer.
Id just loop through and not use a partial probably. But im pretty sure you can just pass locals: {user: user} to use the variable from the loop and not the instance variable from your controller. checkout the docs on it for a bit more details http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
Any help and advise would be much appreciated.
i have a company model that has many recruiters (users) &
a recruiter (user) belonging to a company
also recruiters belong to a gender & gender has many recruiters
using the GEM ransack, i am trying to display all male recruiters(users) in a
company but i am unsure of the code needed for the 'recruiters' action in the companies controller - anyway help & advise would be much appreciated. Below are my files:
Models:
user.rb
class User < ActiveRecord::Base
belongs_to :company
belongs_to :category_gender
end
company.rb
class Company < ActiveRecord::Base
has_many :users
end
category_gender
class CategoryGender < ActiveRecord::Base
has_many :users
end
Companies_controller.rb
class CompaniesController < ApplicationController
respond_to :html, :xml, :json
before_action :set_company, only: [:show, :edit, :update, :destroy]
# load_and_authorize_resource
........
def recruiters
#search = Company.search(params[:q])
#companies = #search.result.includes(:users)
#user = current_user
#company = #user.company
end
end
routes.rb
Rails.application.routes.draw do
resources :companies do
resources :comments, only: [:create, :destroy]
member do
get 'recruiters'
end
end
devise_for :users
resources :users do
resources :comments, only: [:create, :destroy]
resources :adverts
resources :blogs
member do
get 'applications'
get 'applicants'
get 'settings'
get 'dashboard'
get 'management'
end
end
recruiters.html.erb (views file):
<%= search_form_for #search, url: recruiters_company_path do |f| %>
<div>
<h3>gender</i></h3>
<ul>
<% CategoryGender.all.each do |gender| %>
<li>
<%= check_box_tag('q[category_gender_name_eq_any][]', gender.name) %>
<%= gender.name %> (<%= gender.users.count %>)
</li>
<% end %>
</ul>
</div>
<button><%= f.submit 'search' %></button>
<% end %>
Since you are searching for Users. Then, .ransack method should be called on User model and not on Company model.
Try the following in your recruiters method:
def recruiters
#company = Company.find(params[:id])
#users = User.ransack(params[:q]).result.where(company: #company)
#user = current_user
end
1st line: I used #company = Company.find(params[:id]) instead of #company = #user.company because you are using resources pathing for Company, in which recruiter method is a "member" of (as you defined in your routes.rb).
The problem with #user.company is that say for example a user browses /companies/1/recruiters, the #company that will be assigned is going to be the same as when browsing /companies/3/recruiters. But by convention, #company in these two paths should already be different because of the id changing.
2nd line: I just added a the predicate .where(company: #company) so that the results will be filtered to the respective company.
Then in your recruiters.html.erb view file, you could just add something like the following:
...
...
<% #users.each do |user| %>
<%= user.name %>
<% end %>
...
...
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