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 %>
Related
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 %>
I'm trying to make a 'My Files' Page where the documents that users have uploaded are listed, but am having trouble filtering the documents per user.
Right now it is showing all documents uploaded, instead of the document associated with the user who uploaded it.
I use devise for users
I've tried to change the if statement to various different conditions but cannot seem to filter the documents associated with each user.
This is my code for the 'My Files' page:
<% #documents.each do |document| %>
<% if #document = current_user.documents.find_by(params[:user_id]) %>
<%= link_to document.title, document %>: <%= link_to "Download", document.pdf(:original, false) %>
<% end %>
<% end %>
This is my documents controller code for the myfiles page:
def myfiles
#documents = Document.all
end
Is there a simple way I can filter the documents that are associated with each user?
This method will show documents associated with current user only.
Controller file:
def myfiles
if current_user
#documents = current_user.documents
else
#documents = Document.all
end
end
View file:
<% #documents.each do |document| %>
<%= link_to document.title, document %>: <%= link_to "Download", document.pdf(:original, false) %>
<% end %>
Rails find_by returns the first record found. You're probably looking for where.
In the controller:
#documents = if params[:user_id]
current_user.documents.where(user_id: params[:user_id])
else
Document.all
end
In the view:
<% #documents.each do |document| %>
<%= link_to document.title, document %>: <%= link_to "Download", document.pdf(:original, false) %>
<% end %>
http://apidock.com/rails/ActiveRecord/FinderMethods/find_by
http://apidock.com/rails/ActiveRecord/QueryMethods/where
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.
I want a searchfunction that can search strings in Database.
What I tried is:
def search_user
#users_search = User.where("lastname LIKE ?", "#{#search}%")
redirect_to :back
end
For example if I type H it should show all Users with a beginning H
View:
<% if #users_search.present? %>
sdf
<% for user in #users_search %>
<%= user.lastname %>
<% end %>
<% end %>
I have the following code which I need to update...
<% #users.each do |user| %>
<tr>
<td><%= link_to user.fname, user %></td>
</tr>
<% end %>
I want to learn how to update that so instead of just showing the fname, it shows fname + lname
so for a record like James Bond, it shows james bond and links it to the user in Rails.
thanks
You can join the string right in the ERB:
<%= link_to user.fname + " " + user.lname, user %>
<!-- or, slightly better: avoids problems with nil values -->
<%= link_to "#{user.fname} #{user.lname}", user %>
<!-- or, even better -->
<%= link_to [user.fname, user.lname].join(" "), user %>
Or, you can move that ugly logic into a helper method, e.g. in app/helpers/users_helper.rb:
module UsersHelper
def full_name(user)
[user.fname, user.lname].join(" ")
end
end
<%= link_to full_name(user), user %>
Or, (this is what I would do) you can put a full_name method in the model:
class User < ActiveRecord::Base
def full_name
[fname, lname].join(" ")
end
end
<%= link_to user.full_name, user %>