How do I get the attributes of an object in ruby? - ruby-on-rails

I'm new to Ruby and Rails. I've been trying to get a view of all users to work and am not sure what I'm doing wrong.
I have a view with:
<% provide(:title, "View all Users") %>
<h1>Users#viewall</h1>
<p>Find me in app/views/users/viewall.html.erb</p>
<%
#users = User.all
#users.each do |user|
user.name
end
%>
The output of this is a list of objects in the database, with all the object's data. When I want to target (for example) just the name, it doesn't work.
[#<User id: 1, name: "user name", email: "mail#mail.com", created_at: "2016-08-03 15:40:41", updated_at: "2016-08-03 15:40:41", password_digest: "$2a$10$KmWWK86H/dj.HAp9zcHOUOCbph1rawIer41kyH4dIrV...">]
What am I missing here? I'm not even really sure what to google as I don't know what the loop is spitting out.

You are not displaying the data to the user.
In your controller method, is where you should add
#users = User.all
And in your view
<% #users.each do |user| %>
<h1><%= user.name %></h1>
<% end %>
<% %>: These brackets are used to evaluate an expression
<%= %>: These brackets evaluate an expression and render the output

You need to output the information. So in ERB when you have a tag like:
<% i = 4 %>
That executes code. When you have:
<%= "hi" %>
That outputs the return value. So what you actually want is this:
<% User.all.each do |user| %>
<%= user.name %>
<% end %>
What you should do with the users though, is setup an instance variable in your controller:
def viewall
#users = User.all
end
Then use it in your view:
<% #users.each do |user| %>
<%= user.name %>
<% end %>
Just better to keep SQL calls and a lot of logic out of your views. Leverage the controllers, models and helpers to do that.

Related

Why Rails doesn't print output?

I need your help: I have a simple application in Rails and I don't know why it doesn't print attributes on the screen with whatever model I am working on.
For example I have a "Account" model with attributes "first_name", "last_name", "username", etc .. and 4 entries inserted for users in the database table Account.
These are the controller and views files:
class PublicController < ApplicationController
def main
#users = Account.all
end
end
And this is the view file (public/main.html.erb):
<% #users.each do |user| %>
<% user.first_name %>
<% user.last_name %>
<% user.username %>
<% end %>
This doesn't print anything but if I change the second line of the view file with:
<% if user.first_name? %> first_name exists <% end %>
The output is 4 times: first_name exists.
So why it doesn't print the attributes first_name, last_name, username?
In ERB the <% %> tag evaluates Ruby code but does not print the output, where as <%= %> does print the output
This will correct the issue
<%= user.first_name %>
<%= user.last_name %>
<%= user.username %>

no implicit conversion of Symbol into Integer, trying to list certain users

I am dealing with main users (teachers) and their students. They share the user's id and the student's teacher_id. I want to list all the students that have the same teacher_id as the current user's id, so the user can see all their students. This is my code:
<% #students = Student.all %>
<% if #students[:teacher_id] == #current_user.id %>
<% #students.each do |students| %>
Username: <%= students.username %><br/>
Name: <%= students.name %><br/>
<% end %>
<% end %>
And I am getting the following error:
no implicit conversion of Symbol into Integer
What is going on? The logic seems sound: if the teacher_id is the same as the current_user id, print the students.
You are calling [:teacher_id] on #students, which is an ActiveRecord relationship. You can think about it as an array. You should rather call it on an individual student by moving the if inside the iteration:
<% #students = Student.all %>
<% #students.each do |student| %>
<% if student[:teacher_id] == #current_user.id %>
Username: <%= student.username %><br/>
Name: <%= student.name %><br/>
<% end %>
<% end %>
There are some improvements you can do:
Do not call Active Record query (Student.all) in view, move it to the controller.
Filter in AR query, not in application: Student.where(teacher_id: #current_user.id) instead of Student.all and checking hash value.
Finally you'll get
# controller:
#students = Student.where(teacher_id: #current_user.id)
# view:
<% #students.each do |student| %>
Username: <%= student.username %><br/>
Name: <%= student.name %><br/>
<% end %>

Iterate over an Active Record Relation in Rails 3.2

I have the following code:
#items = QuestionGroup.search(params[:search]).limit(50)
This returns an ActiveRecord relation. In the view I want to iterate through it so I use:
<% if #items.present? %>
<%= #items.each do |r| %>
<%= div_for r do %>
<div><%= r.subject %></div>
<% end %>
<% end %>
<% end %>
This does print r.subject to the view but it then follows it with the entire relation. e.g.
the pipe
[#<QuestionGroup id: **, subject: "the pipe", created_at: "*******", updated_at: "******"]
Why is this and how can I fix it?
Problem is here:
<%= #items.each do |r| %>
This line of code iterates over each of the relations and due to the '=' you output its content. Change it to:
<% #items.each do |r| %>
and you are good to go!

Looping through a related model and comparing id's

The relation is that a user has many treatments and a treatment belong to user, one-to-many.
Now i want to print out all the users that have this particular treatment
Inside my treatments show view i have this double loop
<% User.all do |user| %>
<%= user.treatments.each do |t| %>
<% if (t.id).to_i == (#treatment.id).to_i %>
<%= link_to user.name, user_path(user) %><br />
<% end %>
<% end %>
<% end %>
if i change <% User.all do |user| %> to <%= User.all do |user| %> it prints out everything in my users table
can you guys spot why im not getting any users ?
i put a message in the beginning of the inner loop and it didnt display either, guess the problem is there but im not seeing it
.all returns an array. Array doesn't accept a block. Most likely, you want to do .each but forgot to write it. Try this:
<% User.all.each do |user| %>
but a better way is to not iterate all users like this, but get the correct list from the database directly.

Ruby on Rails 3 .each do Problem

I'm sort of new to Ruby on Rails and have been learning just fine, but have seem to run into a problem that I can't seem to solve.
Running Rails 3.0.9 & Ruby 1.9.2
I'm running the following statement in my View:
<%= #events.each do |f| %>
<%= f.name %><%= link_to "View", event_path(f) %><br/><hr/>
<% end %>
And this in my controller:
class AdminController < ApplicationController
def flyers
#events = Event.all
end
end
This goes through each of the records and outputs the appropriate name, but the problem is that at the end it displays all of the information for all of the records like so:
[#<User id: 1, username: "test account", email: "test#gmail.com", password_hash: "$2a$10$Rxwgy.0ZEOb0lMGEIliPBeB/jPSp8roeKdbMvXcLi32R...", password_salt: "$2a$10$Rxwgy.0ZEOb0lMGEIliPBe", created_at: 2111359287.2303703, updated_at: 2111359287.2303703, isadmin: true>]
I'm new to this site, so I'm not sure if you need any more details, but any help is appreciated, after all, I'm still learning.
Thanks in advance.
You should be using <%, not <%= for your .each line, so
<%= #events.each do |f| %>
should be
<% #events.each do |f| %>
.each returns the entire array at the end once it is finished the loop.
<%= ... %> prints out the value of the statment, which is the value returned by .each
<% ... %> does not. So you want:
<% #events.each do |f| %>
<%= f.name %><%= link_to "View", event_path(f) %><br/><hr/>
<% end %>

Resources