I'm using twitter bootstrap in a rails 3 project, and having trouble getting the flash message to show on successful post creation.
Here is the code - can you let me know what I'm doing wrong??
Thanks,
Faisal
POSTS CONTROLLER
def create
#post = Post.new(params[:post])
#post.user = current_user
respond_to do |format|
if verify_recaptcha && #post.save
format.html { redirect_to '/home'}
format.json { render :json => #post, :status => :created, :location => #post }
flash[:notice] = "Thank you, your request has been submitted."
else
format.html { render :action => "new" }
format.json { render :json => #post.errors, :status => :unprocessable_entity }
end
end
end
APPLICATION.HTML.ERB VIEW
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The little chits that ended 3 years of unemployment for me.</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag "application", :media => 'all' %>
<link href="images/favicon.ico" rel="shortcut icon">
<link href="images/apple-touch-icon.png" rel="apple-touch-icon">
<link href="images/apple-touch-icon-72x72.png" rel="apple-touch-icon" sizes="72x72">
<link href="images/apple-touch-icon-114x114.png" rel="apple-touch-icon" sizes="114x114">
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-target=".nav-collapse" data-toggle="collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="/home">LittleChits</a>
<div class="container nav-collapse">
<ul class="nav">
<li><%= link_to "Home", "/home" %></li>
<li><%= link_to "How it Works", "/howitworks" %></li>
<li><%= link_to "About Us", "/thestory" %></li>
<li><%= link_to "Plans & Pricing", "/posts/new" %></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
<div class="content">
<div class="row">
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
<%= yield %>
</div><!--/row-->
</div><!--/content-->
</div> <!-- /container -->
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<%= javascript_include_tag "application" %>
</body>
</html>
Make sure something like this is in your view (e.g. views/application.html.erb)
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
Related
I'm having trouble with an odd :delete method behaviour for links. I configured a devise sign_out route, which is linked in a dropdown box. When clicking that link, it leads to a route error (No route matches [GET] "/users/sign_out").
The strange thing is, that when I copy that link to another position in the navigation bar, it works perfectly.
The RoR navbar code is:
<div class="container nav-collapse">
<ul class="nav">
<li class="active">
<%= link_to t('activeview.navigation.home'), home_index_path %>
</li>
<li>
<%= link_to t('activeview.navigation.sign_in'), new_user_session_path %>
</li>
<li>
<%= link_to t('activeview.navigation.sign_up'), new_user_registration_path %>
</li>
<li><%= link_to t('activeview.navigation.sign_out'), destroy_user_session_path, :method => :delete %></li>
<li class="dropdown">
<% if user_signed_in? %>
<%= link_to (current_user.email + ' <span class="caret"></span>').html_safe, '#', {
:class => 'dropdown-toggle',
'data-toggle' => 'dropdown' } %>
<ul class="dropdown-menu">
<li><%= link_to t('activeview.navigation.settings'), edit_user_registration_path(current_user) %></li>
<li class="divider"></li>
<li><%= link_to t('activeview.navigation.sign_out'), destroy_user_session_path, :method => :delete %></li>
<% else %>
<%= link_to (t('activeview.navigation.not_connected') + ' <span class="caret"></span>').html_safe, '#', {
:class => 'dropdown-toggle',
'data-toggle' => 'dropdown' } %>
<% end %>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
My application.js looks as follows:
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require_tree .
Below is my navigation bar. The case is that sign_out_1 works perfectly, but sign_out_2 uses the GET method instead of DELETE.
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-target=".nav-collapse" data-toggle="collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">Brand</a>
<div class="container nav-collapse">
<ul class="nav">
<li>sign_out_1</li>
<li class="dropdown">
mail#hotmail.com <span class="caret"></span>
<ul class="dropdown-menu">
<li>Configuración</li>
<li class="divider"></li>
<li>sign_out_2</li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
My destroy route is:
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
Finally, the .js loading section is:
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-transition.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-alert.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-modal.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-dropdown.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-scrollspy.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-tab.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-tooltip.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-popover.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-button.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-collapse.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-carousel.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-typeahead.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-affix.js?body=1" type="text/javascript"></script>
A quick workaround is to change sign_out to use HTTP GET instead of HTTP DELETE in ./config/initializers/devise.rb
# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get
And then change calls to sign_out to send GET as well:
<%= link_to t('activeview.navigation.sign_out'), destroy_user_session_path, :method => :get %></li>
it's js bug of twitter bootstrap, and it's fixed at https://github.com/twitter/bootstrap/commit/3568146b28e3eb22e4347062b1e4f923f87daeb8
also see https://github.com/trimentor/foundation/commit/f5ab92cdcc536032f7f6bd7abf991efc92781005
Problem solved
klaustopher his answer fixed the problem.
The problem is that i can't logout when i have <%= current_user.email %> or <%= current_user.username$> in my layouts/application.html.erb
When i delete this line out of my application.html.erb, i am able to logout without errors.
I've searched on google but could not find anything about this problem.
My application.html.erb:
<!DOCTYPE html>
<html id="html">
<head>
<title><%= content_for?(:title) ? yield(:title) : "Contractbeheersysteem" %></title>
<%= stylesheet_link_tag "application", "simple_form", "gegevens", "drop-down-menu", "table", :media => "all" %>
<%= javascript_include_tag "autocomplete-rails.js" %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", "application" %>
<%= javascript_include_tag 'jquery.dataTables.min' %>
<%= csrf_meta_tag %>
<%= yield(:head) %>
</head>
<body>
<body link="#999" vlink="black" alink="black">
<div id="menu">
<ul id="drop-down-menu">
<li><%= link_to "Home Page", home_index_path, :class => current_page?(home_index_path) ? "current" : "" %></li>
<li><%= link_to "Bedrijfsgegevens", bedrijfsgegevens_path, :id => 'bednav' %>
<ul>
<li><%= link_to "Nieuw Bedrijf toevoegen", new_bedrijfsgegeven_path %></li>
</ul>
</li>
<li><%= link_to "Contactpersonen", contactpersoons_path, :id => 'contanav' %>
<ul>
<li><%= link_to "Nieuw Contactpersoon", new_contactpersoon_path %></li>
</ul>
</li>
<li><%= link_to "Contractgegevens", contractgegevens_path(#contractgegevens), :id => 'contrnav' %>
<ul>
<li><%= link_to "Nieuwe Contractgegevens", new_contractgegeven_path %></li>
</ul>
<li><%= link_to "Contactformulier", contact_index_path, :id => 'formnav' %> </li>
<% if user_signed_in? %>
<li><%= link_to "My Account", users_path(#users) %>
<ul>
<li><%= link_to "Uitloggen", destroy_user_session_path, :method => :delete %>
<li><%= link_to "Gebruikers", user_registration_path %>
</ul>
<% else %>
<li><%= link_to "Inloggen", new_user_session_path %>
<ul>
<li><%= link_to "Registreren", new_user_registration_path %>
</ul>
<% end %>
</ul>
<p class="clear_all"></p>
</div>
<div id="login">
Inlogd als: <%= current_user.username %>
</div>
<div id="content">
<hr>
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
<%= yield %>
</div>
<div id="footer">
<hr color="#999", width="100%">
<h4>
<b>Piter Jelles © 2012 </b> | Ruby on Rails
</h4>
</div>
</font>
</body>
</div>
</html>
Note: i'm from the netherlands so there are dutch words in my application.html.erb
When you are logged out, current user will return nil. Calling a method on nil will return in an error
1.9.3p125 :002 > nil.username
NoMethodError: undefined method `username' for nil:NilClass
You have to check if current_user returns something other than nil. You can do this by using
<%= current_user.username if current_user %>
you need to check the existence of current_user like:
<% if current_user.present? %>
<%= current_user.email %> or <%= current_user.username %>
<% end %>
I've created a header layout and currenlty it appears on every page of my website. I would like it to dissapear on the signup page (having multiple logo's looks poor).
Here's the content of my signup page /app/views/users/new.html.erb/
<%= provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= 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-large btn-primary" %>
<% end %>
</div>
</div>
And here's the content of my /app/views/layouts/applications.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
The <%= render 'layouts/header' %> is calling the header that I'd like to be ignored on my signup page.
I'm not sure if i need an <% if .... %> statement placed in the application.html.erb file or if i can somehow ignore the header in the new.html.erb file
You could combine content_for and a yield as described in the Ruby on Rails Guides on Nested Layouts. You would do something like this:
In /app/views/layouts/applications.html.erb
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<style type="text/css"><%= yield :stylesheets %></style>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<div id="header_id">
<%= render 'layouts/header' %>
</div>
<div class="container">
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
At the top of /app/views/users/new.html.erb
<% content_for :stylesheets do %>
#header_id { display: none }
<% end %>
Give the div containing your header a unique id and then replace #header_id above with that. It's not the most elegant solution, but it should work.
-- edit.html.erb --
<h1>Edit User</h1>
<%= render 'form' %>
<%= link_to 'Show', #user %> |
<%= link_to 'Back', users_path %>
-- _form.html.erb --
<% form_for(#user) do |f| -%>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this event from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end -%>
</ul>
</div>
<% end -%>
<p><%= label_tag 'login' %><br/>
<%= f.text_field :login %></p>
<p><%= label_tag 'email' %><br/>
<%= f.text_field :email %></p>
<p><%= label_tag 'password' %><br/>
<%= f.password_field :password %></p>
<p><%= label_tag 'Confirm Password' %><br/>
<%= f.password_field :password_confirmation %></p>
<p><%= submit_tag 'Update' %></p>
<% end -%>
-- users controller --
def edit
#user = User.find(params[:id])
end
def update
if #user.update_attributes(params[:user])
flash[:notice] = 'User was successfully updated'
redirect_to(user_path(#users))
else
render :action => 'edit'
end
end
-- generated page --
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Admin pages</title>
<link href="/assets/.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/prototype.js?body=1" type="text/javascript"></script>
<script src="/assets/scriptaculous.js?body=1" type="text/javascript"></script>
<script src="/assets/opentip.js?body=1" type="text/javascript"></script>
<script src="/assets/effects.js?body=1" type="text/javascript"></script>
<script src="/assets/builder.js?body=1" type="text/javascript"></script>
<script src="/assets/controls.js?body=1" type="text/javascript"></script>
<script src="/assets/dragdrop.js?body=1" type="text/javascript"></script>
<script src="/assets/slider.js?body=1" type="text/javascript"></script>
<script src="/assets/sound.js?body=1" type="text/javascript"></script>
<script src="/assets/workers.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="j5tGtFyn08+fEMzSDCX15fv0c4fyGxlxOmp9CMpeBv8=" name="csrf-token" />
</head>
<body>
<h1>Edit User</h1>
Show |
Back
</body>
</html>
This is part of an install restful_authentication, I have other scaffolds designed the same way and they all work. Spent 2 or 3 hours chasing this, any guidance appreciated.
there is a strange partial in the users/views named _user_bar.html.erb, but I don't see where it gets used.
In rails 3.0+ you need to output the form_for block, change:
<% form_for(#user) do |f| -%>
to:
<%= form_for(#user) do |f| %>
Am working on my first Rails app, and cannot get my image file (logo.png) to display in the browser at the localhost:3000/pages/home page. The image is held in the public/images/logo.png file in my Rails app. Any idea why it might not be showing?
Here is the code:
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/trunk/html5.js"></script>
<![endif]-->
<%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
<%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
<!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]-->
<%= stylesheet_link_tag 'custom', :media => 'screen' %>
</head>
<body>
<div class="container">
<header>
<%= image_tag("logo.png", :alt => "Sample App", :class => "round") %>
<nav class="round">
<ul>
<li><%= link_to "Home", '#' %></li>
<li><%= link_to "Help", '#' %></li>
<li><%= link_to "Sign in", '#' %></li>
</ul>
</nav>
</header>
<section class="round">
<%= yield %>
</section>
</div>
</div>
</body>
</html>
Added source in container in :
<div class="container">
<header>
<img alt="Sample App" class="round" src="/images/logo.png" />
<nav class="round">
<ul>
<li>Home</li>
<li>Help</li>
<li>Sign in</li>
</ul>
</nav>
</header>
<section class="round">
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
Sign up now!
</section>
</div>
</div>
you will need to specify the link like
"/images/logo.png"
or there are also ways to create short path rules if the images is used more often
In regular views you can access images in the public/assets/images directory like this:
<%= image_tag "rails.png" %>