bourg-ismael
bourg-ismael

Reputation: 93

Rails TypeError when trying to display an array

I try to create and Display errors in Rails, I have a problem for that.

Here my code:

controller:

flash[:errors] = []
flash[:errors] << [:message => t('clubs.errors.no_contact'), :strong => new_member_params[:email] + ': ']

view:

<% if flash[:errors].present? %>
        <% flash[:errors].each do |error| %>
        <div class="alert alert-danger">
            <a class="close" aria-hidden="true" href="#" data-dismiss="alert">×</a>
            <strong><%= error[:strong] %></strong>
            <%= error[:message] %>
        </div>
        <% end %>
    <% end %>

and I have this error :

TypeError in Clubs#members
no implicit conversion of Symbol into Integer

on this line:

<strong><%= error[:strong] %></strong>

Any idea ?

Upvotes: 0

Views: 37

Answers (1)

FJM
FJM

Reputation: 613

If you look at flash[:errors] after you do this:

flash[:errors] << [:message => t('clubs.errors.no_contact'), :strong => new_member_params[:email] + ': ']

it will probably look something like [[{message: 'no_contact'}]] because you're wrapping a hash in square brackets... Basically putting a Hash in an Array in an Array. I think what you want to do is change that line above so that you're just appending a Hash instead of a hash inside of an Array, like this:

flash[:errors] << { :message => t('clubs.errors.no_contact'), :strong => (new_member_params[:email] + ': ') }

Upvotes: 1

Related Questions