Rails: My page is rendering text results with html layout - ruby-on-rails

My index page is rendering text result after html result.
[#<User id: 1, username: "mihracefendi", name: "Mihraç", bdate: nil, grad: nil, school: nil, city: nil, about: "Mikkembel bir oyuncu ve aynı zamanda seyis. Kendis...", created_at: "2017-10-04 15:49:54", updated_at: "2017-10-05 19:08:49", email: "mihracbey#gmail.com", avatar: "lohusa.jpg", gender: nil, lastname: "Cerrahoğlu">, #<User id: 2, ...>, ...]
screen shot of index
Why is it happen and how i fix this problem?
Thanks

You probably have some code that looks something like:
<%= #users.each do |user| %>
...
<% end %>
Which, instead, should look like this:
<% #users.each do |user| %>
...
<% end %>

Related

Why do I receive undefined method after dynamically building array of Objects? Ruby

I'm building a most viewed post feature for a simple blog. Each post has a view count that is increased when the Show action is called for that particular post. Then on the Dashboard , I'm trying to list the top 5 posts. So far my code works and returns an array of posts with the post with the highest number of view count being the first index and the last index in the array being the post with the lowest view count. The only thing is when I try to iterate through the array in the view , the view returns:
ERROR
undefined method `title' for nil:NilClass
WHY??? Does it have to do with the "#" infront of the object?
Heres my code.
Dashboard View
<h3> Post </h3>
<% #top_posts.each do |post| %>
<%= post.title %>
<% end %>
Controller Methods
def get_top
#top_posts = []
counts = []
#posts = Post.all
#posts.each do |post|
counts << post.view_count
end
#posts.each do |post|
if counts.max(5).include?(post.view_count)
counts.max(5).each do |n|
if n == post.view_count
#top_posts[counts.max(5).index(n)] = post
end
end
end
end
end
def dashboard
#posts = Post.all
get_top
end
The Top Podcast Array of objects
[#<Post id: 6, title: "Post 6", desc: "", tags: "", view_count: 8, s_desc: "", c_photo: nil, photos: nil, created_at: "2017-06-14 06:02:25", updated_at: "2017-06-15 01:38:40", featured: nil>, #<Post id: 3, title: "post 3", desc: "", tags: "", view_count: 5, s_desc: "", c_photo: nil, photos: nil, created_at: "2017-06-14 05:35:32", updated_at: "2017-06-14 05:35:53", featured: nil>, #<Post id: 5, title: "Post 5", desc: "", tags: "", view_count: 4, s_desc: "", c_photo: nil, photos: nil, created_at: "2017-06-14 06:02:20", updated_at: "2017-06-15 01:38:31", featured: nil>, nil, #<Post id: 4, title: "Post 4", desc: "", tags: "", view_count: 3, s_desc: "", c_photo: nil, photos: nil, created_at: "2017-06-14 05:49:29", updated_at: "2017-06-15 01:38:50", featured: nil>]
The other answer would probably solve your error, but just want to make an attempt to optimize your code.
That array, loop, etc. is unnecessary, ask your db to do that stuff and get the top posts. Fetching all posts and looping over it multiple times...nay, try the following, hopefully this is what you are looking for..
#top_posts = Post.order('view_count desc').limit(5)
That's it, your view needs no change and will work as expected.
Try:
#top_posts << post
instead of:
#top_posts[counts.max(5).index(n)] = post
You don't need to set the array index.

How to convert string to YAML in Ruby 2.3.0?

I am using delayed_job in my application and I am showing some info about the jobs.
I am ability show the id and priority attributes where I couldn't show the handler details.
In my view, when I try to view details of the job:
<% #jobs.each do |item| %>
<% obj = YAML.load(item.to_yaml) %>
<%= obj.inspect %>
<% end %>
when I use inspect, I am getting details as:
#<Delayed::Backend::ActiveRecord::Job id: 51, priority: 0, attempts: 0, handler: "--- !ruby/object:Delayed::PerformableMailer\nobject...", last_error: nil, run_at: "2016-08-25 19:56:44", locked_at: nil, failed_at: nil, locked_by: nil, created_at: "2016-08-25 19:56:44", updated_at: "2016-08-25 19:56:44", queue: nil>
Now I need to get method_name from handler, for this, inorder to list the details, I used
<%= obj.handler.inspect %>
It gave:
"--- !ruby/object:Delayed::PerformableMailer\nobject: !ruby/class
'SubscriptionNotifier'\nmethod_name: :welcome\nargs:\n-
!ruby/object:User\n raw_attributes:\n deleted_at: \n name: ESPN STAR\n
email: esp#test.com\n encrypted_password:
\"$2a$10$jlV1bljCXpto4iTHnkKVnOzE.Us6lmGDtkUVdniw4DFTk8vzkX1oS\"\n
phone: ''\n website: ''\n designations: ''\n id: 22\n
reset_password_token: \n reset_password_sent_at: \n
remember_created_at: \n sign_in_count: '0'\n so on
For showing method_name, I think we should make above string yaml, for this I tried:
<% obj = YAML.load(item.handler.to_yaml) %>
No luck. Please help me how can I convert to yaml or how can I show this method_name?
I think this should work:
<% #jobs.each do |job| %>
<%= YAML.load(job.handler)["method_name"] %>
<% end %>
As in job.handler the yaml is saved, you can parse this and then access the method_name with [].

Can not access model attributes in render partial on Rails 3

js.erb
$('#search_div').html('<%=j render :partial=> 'find_book',locals: { book: #results }, :formats => :html %>')
html.erb
<% #book=book %>
<%= #book.inspect %>
<% #book.id %>
When I do inspect it works find and show me
[#<Book id: 55, name: "consequatur laborum quidem excepturi", isbn: 9782943127358, price: 4023, comment: nil, created_at: "2013-09-01 02:59:29", updated_at: "2013-09-01 02:59:29", author: nil, sale_type: nil, publisher: nil, sn: nil, category: nil>] [#<Book id: 55, name: "consequatur laborum quidem excepturi", isbn: 9782943127358, price: 4023, comment: nil, created_at: "2013-09-01 02:59:29", updated_at: "2013-09-01 02:59:29", author: nil, sale_type: nil, publisher: nil, sn: nil, category: nil>]
But when I try to use #book.id
it gives me.
ActionView::Template::Error (undefined method `id' for #<Array:0x007faef875b260>):
1: <% #book=book %>
2: <%= #book.inspect %>
3: <% #book.id %>
There is no id because the object book is an Array, not a book object. See the [] around your inspections.
To read attribute of each book, you need to loop on this local variable.
At first some improvements on the code
Use books instead of book as variable name.
Avoid using instance variable when you have local variable available.
Then the code
# JS
$('#search_div').html('<%=j render :partial=> 'find_book',
locals: { books: #results } %>'
# partial: _find_book.html.erb
<% books.each do |book| %>
<%= book.id %>
<%= book.other_attribute %>
<% end %>
As you see, your #book is an array :
[#<Book id: 55, name: "consequatur laborum quidem excepturi", isbn: 9782943127358, price: 4023, comment: nil, created_at: "2013-09-01 02:59:29", updated_at: "2013-09-01 02:59:29", author: nil, sale_type: nil, publisher: nil, sn: nil, category: nil>] [#<Book id: 55, name: "consequatur laborum quidem excepturi", isbn: 9782943127358, price: 4023, comment: nil, created_at: "2013-09-01 02:59:29", updated_at: "2013-09-01 02:59:29", author: nil, sale_type: nil, publisher: nil, sn: nil, category: nil>]
Note the "square brackets" ([]) in the start and end. And ass the error shows you cant call id on the array, either do
#book.first.id etc
or while passing it pass the first result like :
$('#search_div').html('<%=j render :partial=> 'find_book',locals: { book: #results.first }, :formats => :html %>')
Or if your plan is to have multiple object then loop through variable #book to show all the details.

read data from table on Ruby

I just read this question I have same problem too, but I wanted more.
%= puts #note.inspect %> I have this this result.
Note id: 1, user_id: 1, note_type: 0, text: "Hello", lat: 40.2290542420142, lng: 44.420879046875, deleted: false, created_at: "2012-04-26 14:10:05", updated_at: "2012-04-26 14:10:05">,
Note id: 2, user_id: 1, note_type: 0, text: "Bye", lat: nil, lng: nil, deleted: false, created_at: "2012-04-27 08:13:44", updated_at: "2012-04-27 08:13:44"
Also I did this #note.first.text, but here I could get only one value. But I want to get this result.
Hello world 70.2290542420142 74.420879046875
Bye
How can I achieve this?
It seems like #note is an array. So try something like:
<%- for n in #note %>
<%= n.text %>
<%= n.lat %>
<%= n.lng %>
<%- end %>

In my Rails app, JSON formatted results are appearing in a HTML view response

In a Rails 3.1 app, I have a controller returning a set of objects (children) in an index view using this code:
def index
#children = Child.all
#base_class = "children-index"
#title = "Your Children"
respond_to do |format|
format.html # children/index.html.erb
format.json { render :json => #children }
end
end
The index.html.erb view is written like so:
<h1><%= title %></h1>
<ul>
<%= #children.each do |child| %>
<li><%= link_to child.fullname, child_path(child) %></li>
<% end %>
</ul>
For some reason, the JSON response is getting thrown into the HTML response and I cannot determine the reason. None of my other index views are having this issue and their code is very close to the same.
John Jake Smith Jr
Jane Ann Doe
[#<Child id: 1, firstname: "John", middlename: "Jake", lastname: "Smith", suffix: "Jr", gender: "Male", dob_actual: "2011-01-05", dob_expected: "2011-01-01", height: 30, weight: 40, created_at: "2011-10-28 21:32:54", updated_at: "2011-10-28 21:32:54">, #<Child id: 2, firstname: "Jane", middlename: "Ann", lastname: "Doe", suffix: "", gender: "Female", dob_actual: "2011-05-05", dob_expected: "2011-05-01", height: 30, weight: 12, created_at: "2011-11-07 18:08:54", updated_at: "2011-11-07 18:08:54">]
That's not JSON, that's inspect output. You're getting that because each returns #children and you're using <%= here:
<%= #children.each do |child| %>
You want just this:
<% #children.each do |child| %>
Did you forget to do #children.to_json in your controller?

Resources