looping through flash and getting active model object - ruby-on-rails

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] %>

Related

Stuck with active records on 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

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.

Listing Users in another controller

Good Morning All,
I seem to be having trouble with this and just wonder what I'm doing wrong, I haven't slept for 24 hours so a little tired...
I have a pages_controller.rb which I want to display certain column of users so I have placed the following code in my pages controller:
def index
#users = User.all
end
However when I do the loop shown below:
<%= #users.each do |u| %>
<%= u.company_name %>
<% end %>
I get the entire hash in my view:
Employer1 [#<User id: 1, email: "email#email.com", encrypted_password: "$2a$10$Nk9zpI4NdXrsuDxXcSyxAeyr2gWVfIfHEIxcMZzUU19e...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: "2013-12-18 19:59:12", sign_in_count: 2, current_sign_in_at: "2013-12-18 19:59:12", last_sign_in_at: "2013-12-17 19:14:12", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2013-12-17 19:13:57", updated_at: "2013-12-18 19:59:12", role_type: "employer", company_name: "Employer1", employer_desc: "Testing Description", area_of_focus: "Rails", number_of_employees: 50, first_name
Why is the happening and what am I doing incorrectly.
Update your loop in your view as follows:
<% #users.each do |u| %>
<%= u.company_name %>
<% end %>
That is, replace <%= #users.each do |u| %> with <% #users.each do |u| %>. Note erb scriptlets <%= vs. <% in each. With <%= you are outputting anything that's within <%= ... %>

Weird code appearing when I loop over model instances in Rails

In my Rails app I'm trying to loop over the Submission instances inside my Folder instances with Rails templating code. It works. However, it's also returning each instance in code which doesn't seem to be JSON. It's what's returned when you look up an instance in the Rails console. Here's an example:
#<Submission id: 112, title: nil, content: nil, created_at: "2013-10-10 23:29:39", updated_at: "2013-10-10 23:29:39", user_id: 1, folder_id: 1, parent_id: nil>
Here's what the code looks like for the loop:
<%= #folder.submissions.each do |x| %>
<% if x.title != nil %>
<div id="<%= x.id %>" class="submission-textual">
<h1><%= x.title %></h1>
</div>
<% else %>
<% end %>
<% end %>
I checked my Folder and Submissions controllers but am not sure what this is. Why are these strings being rendered whenever I try and render an instance in my view? I'm still new to Ruby so that explains why I haven't seen this.
Try replacing the first line with
<% #folder.submissions.each do |x| %>
It's a small diffrerence, the equal sign after the first % was removed. I think that's what's causing the unwanted rendering.
The processing is as follows :
<% "ERB will evaluate this!" %>
<%= "ERB will evaluate and output this!" %>

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!

Resources