Unexpected Flash - ruby-on-rails

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.

Related

Flash alert Error not displaying Rails

So I have two flash notice that should appear one if a post is saved successfully and another if there is an error creating a new post . I implemented it a while back but I just realized that the error flash isn't being displayed properly. All it displays is a red empty notice on top of the window, while the notice for a "successful save" does appear correctly.
for my controller i have :
def create
#topic = Topic.new
#topic.name = params[:topic][:name]
#topic.description = params[:topic][:description]
#topic.public = params[:topic][:public]
if #topic.save
redirect_to #topic, notice: "Topic was saved successfully."
else
flash.now[:alert] = "Error creating topic. Please try again."
render :new
end
end
new post view :
<div class="col-md-8">
<%= render partial: 'form', locals: { topic: #topic, post: #post } %>
</div>
</div>
_form.html:
<%= form_for [topic, post] do |f| %>
<% if post.errors.any? %>
<div class="alert alert-denger">
<h4><%= pluralize(post.errors.count, "error") %>.</h4>
<ul>
<% post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_group_tag(post.errors[:title]) do %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter post title" %>
<% end %>
<%= form_group_tag(post.errors[:body]) do %>
<%= f.label :body %>
<%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter post body" %>
<% end %>
<div class="form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<% end %>
Please put this in application.html.erb file.
<% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
Try to add flash massages in your application.html.erb:
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>">
<%= value %>
</div>
<% end %>
usually it put above <%= yield %>

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' %>

Submit button does not work unless I refresh the page form_for Rails

I have an app that has a problem model and when I go to create a record the submit button does nothing. No errors given it just simply doesnt execute, unless I refresh the page and attempt add it again. The same happens when I go to update a record.
Here is my controller
class ProblemsController < ApplicationController
include Concerns::Votes
def index
#problems = Problem.all
end
def show
#problem = find_problem
end
def new
#problem = Problem.new
end
def edit
#problem = find_problem
end
def create
#problem = current_user.problems.new(problem_params)
#problem.save
redirect_to #problem
end
def update
#problem = find_problem
if #problem.update_attributes(problem_params)
redirect_to #problem
else
redirect_to #problem
end
end
private
def find_problem
#problem = Problem.find(params[:id])
end
def problem_params
params.require(:problem).permit(:name, :description, :url)
end
end
Here is my _form.html.erb partial that I am rendering on new.html
<div class="row">
<div class="large-12 columns">
<%= form_for #problem do |f| %>
<label>Name</label>
<%= f.text_field :name, placeholder: "Name your problem!" %>
</div>
<div class="large-8 columns">
<%= f.text_field :url, placeholder: "Link to any supporting material" %>
</div>
<div class="large-12 columns">
<%= f.text_area :description %>
</div>
<div class="large-12 columns">
<%= f.submit "Create" %>
</div>
</div>
<% end %>
I have resources :problems in my routes.
Here for good measure is my show.html.erb as well.
<%= div_for #problem do %>
<%= link_to 'Edit', edit_problem_path(#problem) %>
<h2><%= #problem.name %> (<%= #problem.cached_votes_score %>)</h2>
<a =href"<%= #problem.url %>"><%= #problem.url %></a>
<p><%= #problem.description %><p>
By <%= #problem.user.name %></br>
<a class="button"<%= link_to 'Up', {:controller => 'problems', :action => 'up_vote'}, {:method => :post } %></a>
<a class="button"<%= link_to 'Down', {:controller => 'problems', :action => 'down_vote'}, {:method => :post } %></a>
<%= link_to 'Edit', edit_problem_path(#problem) %> |
<%= link_to 'Back', problem_path %>
<% end %>
Here is my index.html.erb
<div class="row">
<div class="large-12 columns">
<% #problems.each do |problem| %>
<h1><small><%= problem.cached_votes_score %></small> <%= link_to problem.name, problem %></h1>
<% end %>
</div>
<%= link_to 'New Problem', new_problem_path %>
</div>
I really cant understand why it works if i refresh the page but otherwise it doesnt work at all.
Your HTML is invalid, the submit button is actually not nested under the form tag. Try changing your view code to this:
<div class="row">
<div class="large-12 columns">
<%= form_for #problem do |f| %>
<label>Name</label>
<%= f.text_field :name, placeholder: "Name your problem!" %>
<div class="large-8 columns">
<%= f.text_field :url, placeholder: "Link to any supporting material" %>
</div>
<div class="large-12 columns">
<%= f.text_area :description %>
</div>
<div class="large-12 columns">
<%= f.submit "Create" %>
</div>
<% end %>
</div>
</div>
I had same issue
Before
<%= simple_form_for(:schedule_list, url: schedulelists_create_with_block_path, :html => { novalidate: false}) do |f| %>
<div class="row">
<div class="col-md-12">
<%= f.button :submit, class: 'pull-right' %>
<%end%>
</div>
</div>
After
<%= simple_form_for(:schedule_list, url: schedulelists_create_with_block_path, :html => { novalidate: false}) do |f| %>
<div class="row">
<div class="col-md-12">
<%= f.button :submit, class: 'pull-right' %>
</div>
</div>
<%end%>

Undefined method for edit page

I am new to Rails. My new.html.erb file works perfect as it shows at http://localhost:3000/signup. However I can't seem to get /edit to work. I receive this error:
undefined method `model_name' for NilClass:Class
Extracted source (around line #3):
1: <h1>Account Information</h1>
2:
3: <%= form_for #user do |f| %>
4: <% if #user.errors.any? %>
5: <div class="error_messages">
6: <h2>Form is invalid</h2>
Here's my edit.html file which is a replica of the new.html that works. I tried removing the error messages code and it still just displayed another error with the page.
<h1>Account Information</h1>
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br/>
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.label :username %><br/>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
<div class="field">
<%= f.label :birthday %><br/>
<%= f.text_field :birthday %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
Here's my users_controller that I'm not sure if you need to look at or not. Maybe I have the def edit part wrong.
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
session[:user_id] = #user.id
redirect_to root_url, notice: "Thank you for signing up!"
else
render "new"
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:user])
if #user.update_attributes(params[:user])
flash[:success] = "Account updated"
sign_in #user
redirect_to #user
else
render 'edit'
end
end
end
end
Your code indentation is a tell-tale sign here; you're defining the edit and update methods inside def create; the end immediately prior closes the if #user.save, not the def create.

Ruby - Authenticate.. Login Problems

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.

Resources