Rails 3 & Devise: Error when logout - ruby-on-rails-3

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

Related

How to setup Prismjs inside rich_text_area (Rails 7)

Trying to figure out how to use Prismjs inside rich_text_area so I can use it for syntax highlighting in a blog with Rails 7.
_form.html.erb- currently working great as below
<%= form_with(model: blog) do |form| %>
<% if blog.errors.any? %>
<div style="color: red">
<h2><%= pluralize(blog.errors.count, "error") %> prohibited this blog from being saved:</h2>
<ul>
<% blog.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :content, style: "display: block" %>
<%= form.rich_text_area :content %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
As you can see in both instances where I have input the code using the "code block" and by simply adding it to the "text area" the results are the same.
When I add the same code to any other page it works as expected.

Rails 3.0 : form_tag How to convert this form_for to form_tag.

This is the login page for my app but the role selected for a user is not being reflected in the database so i want to try changing form_for into form_tag to see if the data gets submitted.
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<% if #current_method == "new" %>
<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>
<% end %>
<% for role in Role.find(:all) %>
<div>
<%= check_box_tag "user[role_ids][]", role.id, #user.roles.include?(role) %>
<%= role.name %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= form_tag users_path, methods=> post:do %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= label_tag 'email' %><br />
<%= text_field_tag :email, params[:email], :placeholder => "Email" %>
</div>
<div class="field">
<%= label_tag 'username' %><br />
<%= text_field_tag :username, params[:username], :placeholder => "Username" %>
</div>
<% if #current_method == "new" %>
<div class="field">
<%= label_tag :password %><br />
<%= password_field_tag 'password' %>
</div>
<div class="field">
<%= label_tag :password_confirmation %><br />
<%= f.password_field_tag 'password_confirmation' %>
</div>
<% end %>
<% for role in Role.find(:all) %>
<div>
<%= check_box_tag "user[role_ids][]", role.id, #user.roles.include?(role) %>
<%= role.name %>
</div>
<% end %>
<div class="actions">
<%= submit_tag 'submit' %>
</div>
<% end %>

Advices to clean this show.html.erb view (Rails)?

The following view shows a single post and its comments:
views/posts/show.html.erb:
<h2>posts show</h2>
<span>Title: <%= #post.title %></span><br />
<span>Content: <%= #post.content %></span><br />
<span>User: <%= #post.user.username %></span><br />
<div class="post-<%= #post.id %>">
<h3><span class="vote-count"><%= #post.total_votes %></span> votes</h3><br />
<div class='voted-user'>
<% #post.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</div>
<%= link_to "Vote Up", vote_up_path(#votable, :votable_type => "Post"), :remote => true, :class => "vote-up" %><br />
<%= link_to "Vote Down", vote_down_path(#votable, :votable_type => "Post"), :remote => true, :class => "vote-down" %><br />
<h2>Comments</h2>
<p><%= link_to 'Order by Date', post_path(#post, :order_by => "created_at ASC") %></p>
<p><%= link_to 'Order by Votes', post_path(#post, :order_by => "total_votes DESC") %></p>
<% #comments.map do |comment| %>
<div class="comment-<%= comment.id %>">
<p>
<b>Comment:</b>
<%= comment.content %>
</p>
<p>
<b>Vote:</b>
<span class="vote-count"><%= comment.total_votes %></span>
<div class='voted-user'>
<% comment.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</div>
</p>
<p>
<b>Commenter</b>
<%= link_to comment.user.username, comment.user %>
</p>
<p>
<b>Link</b>
<%= link_to "Show Post Comment", [#post, comment] %>
</p>
<p>
<b>Vote</b>
<%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %><br />
</p>
</div>
<% end %>
<%= will_paginate #comments %>
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% if current_user.id == #post.user_id %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<% end %>
<%= link_to 'Back', posts_path %>
I just focused on making things work so I totally forgot to make it clean.
I'm a Rails beginner and I would like some suggestions or advises to clean this view
(if you suggest to move code to another file please mention the name of the file and the directoy). Thanks in advance.
Good on you for wanting to clean it up. This is some of what I would do. I've included some examples of a few things here: Partials, Helpers, and also cleaned up the HTML a little to allow more control over the style in your stylesheets (which I left out, but you can figure out that part I'm sure). If this was my project, I would extract everything even more (for example, I would probably move the entre "edit/back" links at the bottom, as well as the "Order" links, to their own partials or helpers, since I would probably be using them in a lot of places throughout the application.) And if your comments are polymorphic (i.e., not just for posts), then definitely move that into its own view file. Also, I did this a rather quickly, so it might have some errors, sorry if you find any.
_post.html.erb
<article class="post-info">
<span class="title"><b>Title:</b> <%= post.title %></span>
<p><%= post.content %></p>
<span class="user"><b>User:</b> <%= post.user.username %></span>
</article>
<div class="post" id="post-<%=post.id%>">
<div class="vote-count"><%= post.total_votes %></span> votes
<ul class="voted-user">
<% post.votes.each do |vote| %>
<li><%= link_to vote.user.username, vote.user %></li>
<% end %>
</ul>
<ul class="voting">
<li><%= link_to "Vote Up", vote_up_path(post, :votable_type => "Post"), :remote => true, :class => "vote-up" %></li>
<li><%= link_to "Vote Down", vote_down_path(post, :votable_type => "Post"), :remote => true, :class => "vote-down" %></li>
</ul>
_comment.html.erb
<div class="comment" id="comment-<%=comment.id%>">
<article class="comment">
<b>Comment:</b> <%= comment.content %>
</article>
<div class="votes-total">
<b>Votes:</b> <span class="vote-count"><%= comment.total_votes %></span>
<ul class='voted-user'>
<% comment.votes.each do |vote| %>
<li><%= link_to vote.user.username, vote.user %></li>
<% end %>
</ul>
</div>
<div class="commenter">
<b>Commenter:</b> <%= link_to comment.user.username, comment.user %>
</div>
<div class="link">
<b>Link:</b> <%= link_to "Show Post Comment", [comment.post, comment] %>
</div>
<div class="vote">
<b>Vote:</b> <%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %>
</div>
</div> <!-- .comment -->
show.html.erb
<h2>Posts</h2>
<%= render #post %>
<h2>Comments</h2>
<ul class="order">
<li><%= link_to 'Order by Date', post_path(#post, :order_by => "created_at ASC") %></li>
<li><%= link_to 'Order by Votes', post_path(#post, :order_by => "total_votes DESC") %></li>
</ul>
<%= render #comments %>
<%= will_paginate #comments %>
<h2>Add a Comment</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<ul class="manage">
<li><%= edit_link_if_allowed(current_user, #post) %></li>
<li><%= link_to 'Back', posts_path %></li>
</ul>
Posts Helper
def edit_link_if_allowed(current_user, post)
link_to "Edit", edit_post_path(post) if post.user_id == current_user.id
end
You could use Partials

Paperclip error rails 3

So I have been trying to follow some paperclip tutorials and adjusting them to rails 3. I follow the steps and got an error once I started to add the code needed for the _form and show.htm.erb files. This is the error I get.
Error Message:
ActionView::Template::Error (undefined method `label' for nil:NilClass):
11: </div>
12: <% end %>
13: <div class="field">
14: <%= form.label :photo, "Photo" %>
15: <%= form.file_field :photo %>
16: </div>
17: <div class="field">
Form:
<%= form_for #user, :html => { :multipart => true } do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<div>
<% end %>
<div class="field">
<%= form.label :photo, "Photo" %>
<%= form.file_field :photo %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.text_field :password %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>`
form.label doesn't make sense, since you're calling your form variable f inside your block, with the line form_for ... do |f|.
You need to use f.label etc.

Rails 3 - Forms - Jquery :remote make duplicates and prompts 2x?

Hi
Im working with a form and have added :remote => true to the form and the destroy link. But when I do that this happends:
I submit the form and it makes 2 versions that are the same.
I destroy, I get prompted twice?
If i remove the :remote => true it only makes one version and only prompt once.
This is my form
<%= form_for([#comment], :remote => true) do |f| %>
<% if #comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %>
<br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %>
<br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.hidden_field :user_id %>
<%= f.hidden_field :retailer_id %>
<%= f.hidden_field :product_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This is my destroy link
<%= link_to 'Destroy', comment, :confirm => 'Are you sure?', :method => :delete, :remote => true %>
Am I doing something very wrong here? Thanks in advance!
I think you have included your rails.js file twice. that might be the problem. check it.