황완식
황완식

Reputation: 3

rails error message String can't be coerced into Fixnum

I'm trying to make variable in .each do loop.

<article class='lecture-list' >
    <% @lectures.each do |lecture| %>
      <div class='lecture-list-wrapper'>

        <p class='activity'>
            <% name = lecture.id + "th - "+ lecture.title %>
            <span><%= link_to name, lecture, class: 'btn-lightgreen' %></span>
            <span><%= link_to 'Edit', edit_lecture_path(lecture), class: 'btn-darkyellow' %></span>
            <span><%= link_to 'Destroy', lecture, method: :delete, class: 'btn-darkorange', data: { confirm: 'Are you sure?' } %></span></p>
      </div>
    <% end %>
</article>

<% name = lecture.id + "th - "+ lecture.title %> part makes

String can't be coerced into Fixnum

error. How can I solve it?

Upvotes: 0

Views: 114

Answers (2)

Lazarus Lazaridis
Lazarus Lazaridis

Reputation: 6029

OJ1987 answer is fine, though I prefer using interpolation in such cases:

<% name = "#{lecture.id}th - #{lecture.title}" %>

Upvotes: 0

user5245636
user5245636

Reputation:

Instead of:

<% name = lecture.id + "th - "+ lecture.title %>

Try: Note: If you want it as variable, ideally use _ underscore not - dash. As code below.

<% name = lecture.id.to_s + "th_"+ lecture.title %>

Upvotes: 0

Related Questions