Stuck with active records on ruby on rails - ruby-on-rails

hi ive just started to learn ruby on rails so i might make some little obvious mistakes but theres one thing i cant get around,
my controller action currently passes the variable to my view:
class HomepageController < ApplicationController
def index
end
def Value
#users = User.all
render "homepage/Value"
end
end
and this is my current view:
<ul class="users">
<% #users.each do |user| %>
<%= user.first_name %>
<% end %>
</ul>
this outputs nothing on my view,
if i change the each statement to:
<%= #users.each do |user| %>
it displays :
[#<User id: 1, first_name: "hello", last_name: "world", email: "", password: nil, created_at: "2017-08-31 10:33:01", updated_at: "2017-08-31 10:33:01">, #<User id: 2, first_name: nil, last_name: "anewworld", email: "emailnew", password: "123", created_at: "2017-08-31 12:35:41", updated_at: "2017-08-31 12:35:41">]
i have absolutely no clue why <%= user.first_name %> wouldnt work

<ul class="users">
<% if #users %>
<% #users.each do |user| %>
<li>
<%= user.first_name %>
</li>
<%else%>
<li> No Users </li>
<%end%>
</ul>
"li" tag missing

Related

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

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.

An active record array of post objects is being returned after my actual post objects?

I am displaying all of a user's posts in my users#show view, and it is working fine except for an array of all the user's posts is being returned after the actual posts. Here is the show action for the users controller:
def show
#user = User.find(params[:id])
#posts = #user.posts.all
end
users/show.html.erb
<div class="profile">
<%= image_tag #user.image.url(:thumb) %>
<h4><%= #user.name %></h4>
</div>
<div class="userposts">
<%= #posts.each do |post| %>
<h8><%= link_to post.title, post_path(post) %></h8>
<p><%= truncate(post.content, length: 250, omission: '... (continued)') %></p>
<% end %>
</div>
Here is the unwanted array being returned:
Post id: 1, title: "post", content: "content", created_at: "2014-12-02 14:07:43", updated_at: "2014-12-02 14:07:43", user_id: 1 Post id: 2, title: "Another Post", content: "more content", created_at: "2014-12-02 14:29:26", updated_at: "2014-12-02 14:29:26", user_id: 1
Change this <%= #posts.each do |post| %> to this <% #posts.each do |post| %>.
In erb <%= means the evaluated code will be printed out.

looping through flash and getting active model object

I am trying to loop through my flash messages but I am getting an active model object as the message.
<% flash.each do |key, msg| %>
<%= msg %>
<% end %>
#<ActiveModel::Errors:0x007f84c4cdb108 #base=#<User id: nil, email: "cd", created_at: nil, updated_at: nil, password_digest: "$2a$10$lmh2.rb9bAmsQ3lo6SEYqu1f2L1JOVrD8VxmPXJ5Jobx...", remember_token: nil, admin: false, username: nil, password_reset_token: nil, password_reset_sent_at: nil, provider: nil, uid: nil>, #messages={:email=>["is invalid"], :password=>["is too short (minimum is 6 characters)"]}>
If the then loop through the object I can get to the messages
<% flash.each do |key, msg| %>
<% msg.each do |k,m| %>
<%= m %>
<% end %>
<% end %>
is invalid
is too short (minimum is 6 characters)
What am I doing wrong? Or must I do it this way?
It looks like you are populating flash with error object instead of the messages. You can do this in your view to show all the error messages
<%= flash[:error].full_messages.join("\n")%>
I would advise you to just populate the error messages in flash by doing this in controller
flash[:error] = #model.errors.full_messages
And in your view you can just do
<%= flash[:error] %>

iterate through active record results - ruby on rails

I've been trying got find this answer, and maybe it's too simple...
In rails, how is the best way to iterate through results from activerecord pull the specific fields that you want?
I have a controller for comments (named posts) that pulls all records:
def index
#posts = Post.find(:all)
end
Then in the index view, when I use <%= #posts %> I get all of the data...which is great...
#<Post id: 1, user_id: "9", picture: nil, comments: "here's a first comment", title: nil, twitter: nl, frame: nil, created_at: "2012-05-09 04:21:16", updated_at: "2012-05-09 04:21:16"> #<Post id: 2, user_id: "9", picture: nil, comments: "here's a second comment", title: nil, twitter: "please", frame: nil, created_at: "2012-05-09 05:20:03", updated_at: "2012-05-09 05:20:03">
How can I now iterate through test so that the view shows the data from the comments and created_at fields:
Here's the first comment, 2012-05-09 04:21:16
Here's the second comment, 2012-05-09 05:20:03
I've tried the following and get an error.
<% #posts.each do |c| %>
<%= c.posts.comments %>
<%= c.posts.created_at %>
<% end %>
The "c" in #posts.each do |c| represents the specific post object in the #posts collection.
So, in a sense you are trying to do <%= post.posts.comments %>.
Here's how the code should look:
<% #posts.each do |p| %>
<%= p.comments %>
<%= p.created_at %>
<% end %>
Change things to this:
<% #posts.each do |post| %>
<%= post.comments %>
<%= post.created_at %>
<% end %>
I find it makes it easier for people to follow if you name the inner variable as the singular of the out variable -- therefore #posts on the outside becomes post on the inside.
Good luck!

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