Ruby - Authenticate.. Login Problems - ruby-on-rails

Hi I would like to create a authentication with Ruby on Rails.
I have no Error - Message but i cant login.
on my localhost this picture appears ( sorry i cant post a screen )
My Workingtimes
C 2011 | login
The Login - Button dont works.
Here are my code:
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Mytimes</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="container">
<div id="header">
<h1> My Workingtimes </h1>
</div>
<div id="content">
<% if flash[:notice] %>
<p id="notice">
<%= flash[:notice] %>
</p>
<% end %>
<% if flash[:alert] %>
<p id= "alert">
<%= flash[:alert] %>
</p>
<% end %>
</div>
<div id="footer">
© 2012 |
<% if user_signed_in? %>
<%= link_to "logout", logout_path, method: :delete %>
<% else %>
<%= link_to "login", login_path %>
<% end %>
</div>
</div>
</body>
</html>
routes.rb
Mytimes::Application.routes.draw do
resources :mytimes
resources :users, :only => [:new, :create]
resources :sessions, :only => [:create]
get "login" => "sessions#new", as: "login"
post "sessions" => "sessions#create", as: "sessions"
delete "logout" => "sessions#destroy", as: "logout"
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
private
def current_user
if session[:user_id]
#current_user ||= User.find(session[:user_id])
end
end
def user_signed_in?
current_user.present?
end
helper_method :user_signed_in?
end
session_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to mytimes_path,
notice: "Your are signed!"
else
flash.now.alert = "Failed Email or Password!"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to mytimes_path,
notice: "You are logged out!"
end
end
sessions/new.html.erb
<h2> Log In </h2>
<%= form_tag sessions_path do %>
<p>
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
</p>
<p>
<%= label_tag :password %>
<%= password_field_tag :password %>
</p>
<p><%= submit_tag "Log In" %></p>
<% end %>
Iam thankful for your help.
users/ new.html.erb
<h2>New User</h2>
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</p>
<p><%= f.submit %></p>
<% end %>

There is no yield in your application.html.erb. The views will therefore not be displayed.

Related

Unexpected Flash

I'm trying to display the flash whenever i want to sign in, sign up, delete a user but the browser displays two same flashes at one time:
So I think the problem is something wrong happened with my layout/application.html.erb. This is my application file:
<!DOCTYPE html>
<html>
<head>
<title>LibraryVLC</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-
turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-
track': 'reload' %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<%= yield %>
</div>
</body>
</html>
and this is my users_controller:
def index
#users = User.all.page params[:page]
# byebug
end
def new
#user = User.new
end
def edit
##user = User.find_by(params[:id])
end
def show
#user = User.find(params[:id])
end
def create
#user = User.new(user_params)
if #user.save
# hand a successful saves
log_in #user
flash[:success] = "Welcome to VLC Library!!!!"
redirect_to #user
else
render 'new'
end
end
def update
if #user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to #user
else
render "edit"
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User delected"
redirect_to users_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confrimation)
end
def find_user
#user = User.find_by id: params[:id]
end
end
Is there anyway I can solve this problem ?
view/users/edit.html.erb
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="col-md-7 col-md-offset-3">
<%= form_for(#user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control'
%>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>
</div>
</div>
view/users/index.html.erb
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= paginate #users %>
<ul class="users">
<%= render #users %>
</ul>
<%= paginate #users %>
view/users/show.html.erb
<% provide(:title, #user.name) %>
<div class="row">
<aside class="col-md-4">
<section class ="user_info">
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
</aside>
</div>
view/users/_user.html.erb
<% provide(:title, #user.name) %>
<div class="row">
<aside class="col-md-4">
<section class ="user_info">
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
</aside>
</div>
layout/header
<%= form_tag(search_page_path, :method => "get",
class: 'navbar-form navbar-right') do %>
<div class="input-group">
<%= search_field_tag :search, params[:search], placeholder:
"Search", class: "form-control" %>
<div class="input-group-btn">
<%= button_tag "", :class => 'btn btn-info glyphicon glyphicon-
search',:name => nil%>
</div>
</div>
<% end %>
layout/shim
<!--[if lt IE 9]>
<script
src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
On the line in your application layout:
<%= flash.each do |key, value| %>
you have an equal sign, which is saying to output the results of running the each (i.e., <%= instead of <%). The return value of an each statement is the collection that was iterated, so you are iterating the collection and prettifying it with your html/css and then outputting the entire collection in one giant dump at the end.

error.full_messages are hidden and I can't figure out why

I'm working through a rails tutorial and seem to be stuck on getting some error messages to display on my signup window when an e-mail address, or password is typed in incorrectly. I've included my user controller, new user erb, and error message erb below. Any help would be great. I feel like I've been staring at this forever and getting nowhere. Thanks in advance for helping.
Error_Message erb
<% if #user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= #user.errors.count %> errors.
</div>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li> <%= msg %> </li>
<% end %>
</ul>
</div>
<% end %>
User Controller
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
# Handle a succesful save
else
render "new"
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
New User erb
<%provide(:title, 'Sign up')%>
<h1>Sign up</h1>
<div class="row">
<div class ="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<% render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Just change this line:
<% render 'shared/error_messages' %>
to
<%= render 'shared/error_messages' %>

I receive an error that "No Route Matches" {...id=>nil} *Wicked*

I have seen a lot of similar routing issues and id=>nil posts, but none of the solutions have resolved my error.
First here is the entire error:
ActionController::UrlGenerationError in ProfileSteps#personal
Showing ...profile_steps/personal.html.erb where line #1 raised:
No route matches {:action=>"show", :controller=>"profile_steps", :id=>nil
I am using Wicked to create a multi step form and it seems like the I am not fetching the :id properly.
Here is the profiles_controller.rb following 'signup' and creating the 1st step of profile
def create
#profile = Profile.new(profile_params[:profile])
if #profile.save
session[:profile_id] = #profile.id
redirect_to profile_steps_path
else
render :new
end
end
Here is the profile_steps_controller.rb which is the next step of form it is redirected to
class ProfileStepsController < ApplicationController
include Wicked::Wizard
steps :personal
def show
#profile = Profile.new(params[:profile])
session[:profile_id] = #profile.id
render_wizard
end
def update
#profile = Profile.new(profile_params)
#profile.attributes = (profile_params)
render_wizard #profile
end
private
def profile_params
params.require(:profile).permit(:description, :name, :website)
end
def redirect_to_finish_wizard
redirect_to root_url, notice: "Thank you for signing up."
end
end
views/profile_steps/personal.html.erb
<%= form_for #profile, url: wizard_path do |f| %>
<br>
<div class="field">
<%= f.label :name, "Company Name" %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :website %><br>
<%= f.text_field :website %>
</div>
<br>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes.rb
project::Application.routes.draw do
resources :profiles
resources :profile_steps
devise_for :users, :controllers => { :registrations => "registrations" }
root "pages#home"
get "profile" => "pages#profile"
match "profile_steps/personal", to: "profile_steps#personal", via: "post"
Thanks in advance and my apologies if this has been addressed in previous posts.
Updated:
Here is the 1st page of the multistep form:
new.html.erb
<%= form_for(#profile) do |f| %>
<% if #profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#profile.errors.count, "error") %> prohibited this profile from being saved:</h2>
<ul>
<% #profile.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<h1>New profile</h1>
<div>
<%= f.radio_button ..., true %> <%= f.label ... %>
</div>
<div>
<%= f.radio_button ..., false %> <%= f.label ... %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I had this problem in the view on the first page of the wizard, I solved it using a helper method:
def safe_previous_wizard_path(id)
id ? previous_wizard_path : root_path
end
or even:
<% if applicant.persisted? %>
Back
<% end %>

Rails - Couldn't find User with id=edit

I am using Rails 3 and Devise for authentication. When I try to update my current_user, an exception is thrown stating:
Couldn't find User with id=edit
app/controllers/users_controller.rb:49:in `update'
Here is update and edit in UsersController:
#UsersController.rb
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(params[:user])
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
def edit
#user = User.find(params[:id])
end
And here is /views/layouts/_navigation.html.erb:
<%= link_to "Home", root_path, :class => 'brand' %>
<ul class="nav">
<% if user_signed_in? %>
<li>
<%= link_to('Logout', destroy_user_session_path, :method=>'delete') %>
</li>
<% else %>
<li>
<%= link_to('Login', new_user_session_path) %>
</li>
<% end %>
<li>
<%= link_to('About', help_about_path) %>
</li>
<% if user_signed_in? %>
<li>
<%= link_to('Edit Account', edit_user_path(current_user)) %>
</li>
<% end %>
<% if user_signed_in? and current_user.role == "gen_admin" || current_user.role == "teacher" %>
<li>
<%= link_to('View Students', users_path ) %>
</li>
<% end %>
<% if user_signed_in? and current_user.role == "gen_admin" || current_user.role == "teacher" %>
<li>
<%= link_to('New User', new_user_path ) %>
</li>
<% end %>
<% if user_signed_in? %>
<li>
<%= link_to('Student Data', data_path) %>
</li>
<% end %>
</ul>
Finally, here is /views/devise/registrations/edit.html.erb:
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, :as => resource_name, :url => edit_user_registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :teacher %><br />
<%= f.text_field :teacher %></div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div><%= f.label :new_password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, :autocomplete => "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></div>
<div><%= f.submit "Update" %></div>
<% end %>
<%= link_to "Back", :back %>
Why is the user id=edit? Here is my hash, if that helps:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"7g1WsNCVuNY/5ZTeBUdv97tbdAPacvvDAzBSMGCcuNY=",
"user"=>{"email"=>"email#admin.com",
"teacher"=>"Demont",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"current_password"=>"[FILTERED]"},
"commit"=>"Update",
"id"=>"edit",
"format"=>"user"}
Try using:
#user = current_user
Something looks to be wrong with your route or edit form. Try using the form like in their wiki page...
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password
But the above code should get around this
Most likely you I've got conflict in your routes when added UsersController.
There is devise's wiki page explaining that.
Default devise's route for edit user is /users/edit but in your controller path for update action is /users/:id. What's why you've got "edit" instead of user_id
Use
<%= form_for(#resource, :as => resource_name, :url => edit_user_registration_path(resource_name), :html => { :method => :put }) do |f| %>
to your form view and
#resource = User.find(params[:id])
in your edit action.

NoMethodError in Pages#contact

I got this error I Created pages model and for pages controller i created a contact, thank you and _form page. but I getting the error "NoMethodError in Pages#contact"
`Showing /home/rohit/Desktop/inertiiaproject/inertiiatwo/app/views/pages/_form.html.erb where line #1 raised:
undefined method model_name' for NilClass:Class
pages_controller.rb file
class PagesController < ApplicationController
def new
#pages = pages.new
end
def create
#pages = pages.new(params[:pages])
if #pages.deliver
render :thank_you
else
render :new
end
end
end
contact.html.erb
<h1>Want to get in touch?</h1>
<p>Please fill out the form below and we'll get back to you as soon as possible.</p>
<%= render 'form', :pages => #pages %>
_form.html.erb
<%= form_for pages do |f| %>
<% if pages.errors.any? %>
<ul>
<% pages.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div style="display: none;">
<%= f.label :nickname %><br/>
<%= f.text_field :nickname %>
</div>
<div>
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div>
<%= f.label :message %><br/>
<%= f.text_area :message %>
</div>
<div>
<%= f.submit "Send" %>
</div>
<% end %>

Resources