What happened to the flash messages in Rails 3.2.1? - ruby-on-rails-3.1

I use this code to display flash messages in Rails 3.2.1 apps (well, I did):
<% flash.each do |name, msg| %>
<div class="alert alert-<%= name == :notice ? "success" : "error" %>">
<%= msg %>
</div>
<% end %>
But I get nothing. So I put <%= debug flash %> to see what I was getting back after an update and I get this:
--- !ruby/object:ActionDispatch::Flash::FlashHash
used: !ruby/object:Set
hash: {}
closed: false
flashes: {}
now:
How to I do the same thing?

The code I use for displaying flash messages and alerts is:
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :class => "flash flash_#{name}" %>
<% end %>
But if your flash hash is empty, it's not going to display anything. Are you sure you're setting your flash hash properly?

Related

Why isn't this simple elsif working in Rails/Ruby?

If there is a flash notice > "Flash notice"
If there isn't a flash notice and you're on the homepage > "Welcome"
Code:
<% if flash %>
<% flash.each do |name, msg| %>
<%= content_tag :span, msg, id: "flash_#{name}" %>
<% end %>
<% elsif current_page('/') %>
<% print 'Hi' %>
<% end %>
It prints the flashes correctly but not the welcome on the home page. Doesn't seem to matter if I try current_page or root_url or print 'Welcome' or just plain "Welcome" with no code wrapper. Why?
Yes, almost, I wasn't checking the flash properly at the beginning of the block. This works now:
<% if flash.present? %>
<% flash.each do |name, msg| %>
<%= content_tag :span, msg, id: "flash_#{name}" %>
<% end %>
<% elsif current_page?('/') %>
Welcome to The Spain Report.
<% end %>
Thank you all!

Ruby on rails 4: showing error messages

I have trouble displaying error message for invalid inputs using Ruby on rails. If anyone can help me with it? Currently new to the area.
I suspect the area is due to :user and #user.
This is my form generated on index.html.erb
<%= form_for :user, url: '/login', html:{id:"form"} do |f| %>
<h2 class="login">Login</h2>
<div class="form">
<input type="name" placeholder="Name" name="user[name]" class="form-input" required /></div>
<% end %>
My ActiveRecord Model:
class User < ActiveRecord::Base
validates :name, :uniqueness => true
has_secure_password
end
I try to use the following error handling, by passing the following code after <%= form_for :user, url: '/login', html:{id:"form"} do |f| %> but it did not work with and showed:
NoMethodError in Users#register
: undefined method `errors' for nil:NilClass
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
You should be able to do something like this.
/app/views/shared/_error_messages.html.haml:
- if object.errors.any?
.alert.alert-danger
The form contains #{pluralize(object.errors.count, "error")}.
%ul#error_explanation
- object.errors.full_messages.each do |msg|
%li= msg
Then, wherever you want errors to be shown in your views, something like:
= render 'shared/error_messages', object: f.object
Note object in the method. When you call, you could pass either an object instance from your controller or (in the above case), the object of a form builder.

Rails 4 Error: undefined method[]' for nil:NilClass`

I'm trying to setup a messaging system in my site using the Mailboxer gem in Rails 4.
When my app gets to this code:
def fetch_params(key, *subkeys)
params[key].instance_eval do
case subkeys.size
when 0 then self
when 1 then self[subkeys.first]
else subkeys.map{|k| self[k] }
end
end
it returns the error: undefined method '[]' for nil:NilClass
I've tried to figure this out, but I'm not very experienced. I've been following this tutorial: http://jamestansley.com/2014/02/22/customizing-the-mailboxer-ruby-gem-2/
I tried removing the line of code returning the error just to see what would happen, after which I received the error wrong number of arguments (1 for 2..6).
Can anyone attempt to explain this code to me? Been having a really rough time implementing this feature.
view/conversations/_form:
<%= simple_form_for :conversation, url: :conversations do |f| %>
<%= f.input :recipients %>
<%= f.input :subject %>
<%= f.input :body %>
<div class="form-actions">
<%= f.button :submit, class: 'btn-primary' %>
<%= submit_tag 'Cancel', type: :reset, class: 'btn btn-danger' %>
</div>
<% end %>
view/conversations/show:
<h1><%= #conversation.subject %> </h1>
<!--may have to work on this view. don't know if i did the haml conversion correctly-->
<h1> <%= conversation.subject %> </h1>
<ul>
<% content_tag_for(:li, conversation.receipts_for(current_user)) do |receipt| %>
<%= message = receipt.message %>
<h3><%= message.subject %> </h3>
<p><%= message.body %> </p>
</ul>
<% render 'messages/form', conversation: conversation %>
<% end %>
And here's my github repo: https://github.com/portOdin/GoFavorIt-Heroku/blob/8cd19ff5b61eb805dc694a6586f50df608752be2/app/views/conversations/show.erb

add form (non-nested) dynamically in rails

pls can someone explain how I can dynamically add another copy of a form in rails?...been working on this for almost 2hrs. I messed around with .clone() and .appendTo in jquery, but it didnt work. Also, most of the materials I found online (like on railscast #196 and stackoverflow) focused heavily on nested forms. My form is nested, but I actually just want to add the parent form again. The photos which are nested use the html multiple attributes so I'm guessing that will handle multiple files upload for each parent form (btw I'm using paperclip).
If I just need to modify the railscast code please let me know.
Thanks.
<%= form_for(#user_book, html: { multipart: true }) do |f| %>
<% if #user_book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user_book.errors.count, "error") %> prohibited this user_book from been saved:</h2>
<ul>
<% #user_book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<p>hello</p>
<% end %>
</ul>
</div>
<% end %>
<%= f.text_field :title, placeholder: "enter title...", id: "book_title" %>
<%= f.text_field :category, placeholder: "enter category..." %>
<%= file_field_tag 'user_book[user_book_photos_attributes][][photo]', :multiple => true do |p| %>
<%= p.file_field :photo %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

undefined method `safe_concat' for []:Array in rails 3

I am trying to execute a flash partial like the following:
<% flash.each do |key, value| %>
<div class="flash <%= "#{key}" %>"><%= "#{value}" %></div>
<% end %>
And am getting the following error:
undefined method `safe_concat' for []:Array
The partial is being updated via an ajax request as follows:
page.replace_html "flash_messages", :partial => 'layouts/flash', :locals => { :flash => flash }
Any ideas? I have never seen something like this before.
Thanks.
Shouldn't it be the following?
<% flash.each do |key, value| %>
<div class="flash #{key}"><%= value %></div>
<% end %>
There's no need to wrap the key and value into "#{}". I suppose the nested <> is what made it choke on your line.
I can't try so I wouldn't know if it actually can access key outside of <% %>, so maybe you would need something like:
<% flash.each do |key, value| %>
<% class_names = "flash #{key}" %>
<div class="<%= class_names %>"><%= value %></div>
<% end %>

Resources