Yasuda
Yasuda

Reputation: 17

Rails showing ActiveRecord_Relation in my view

Well my problem is when i call my model Question and show in a .each do, my view show the ActiveRecord but i don't now how to hide this or the right way to show my questions without the ActiveRecord.

In my view the content show like this:

enter image description here

<%= @questions = Question.all.order(:id).reverse_order %>

<% @questions.each do |question|  %>
  <% if @course.id == 1 %>

      <h5><%= link_to question.title, question ,  class: 'reply text-light text-decoration-none' %></h5>
<% end %>
<% end %>

Upvotes: 0

Views: 51

Answers (3)

Jignesh Gohel
Jignesh Gohel

Reputation: 6552

You should use url_helpers provided by Rails. See Examples provided under link_to docs.

Also I would suggest to follow the recommendation at https://stackoverflow.com/a/66470651/936494 and in your view change following

<h5><%= link_to question.title, question ,  class: 'reply text-light text-decoration-none' %></h5>

to

<h5><%= link_to question.title, question_path(question),  class: 'reply text-light text-decoration-none' %></h5>

And if you don't have url_helpers available, then the 2nd argument should be a URL to a resource in your application.

Hope that helps. Thanks.

Upvotes: 0

eux
eux

Reputation: 3282

What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

You could use <% %> instead of <%= %>:

<% @questions = Question.all.order(id: :desc) %>

And it's better to put that to controller:

class QuestionsController
  def index
    @questions = Question.all.order(id: :desc)
  end
end

Upvotes: 1

Yasuda
Yasuda

Reputation: 17

I found a solution if i want to call a Model in view, just create a input type="hidden" like this:

<input type="hidden" value="<%= @questions = Question.all.order(:id).reverse_order %>">

Upvotes: 0

Related Questions