Can not access model attributes in render partial on Rails 3 - ruby-on-rails

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.

Related

Rails: My page is rendering text results with html layout

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

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 [].

Sorting and grouping array, hash with all items appears at the end

I have a table with leaflets data which is sorted by date in controller:
#leaflets = Leaflet.order(:date)
And then grouped by dates in view (I found the code somewhere on the internet)
<%= #leaflets.group_by(&:date).each do |date, leaflets| %>
<p><%= l(date, :format => '%A, %d %b %Y') %></p> <!-- localized date formatting: weekday, day month year -->
<table>
<tr>
<% leaflets.each do |leaflet| %>
<td style="border:dotted 1px; padding:5px;">
<strong><%= leaflet.place %></strong><br/>
<%= leaflet.description %><br/>
<%= link_to "Zapisz się", leaflet_path(leaflet) %>
</td>
</a>
<% end %>
</tr>
</table>
<% end %>
Everything works great, but it prints all items as a hash at the end:
{Tue, 11 Nov 2014=>[#<Leaflet id: 14, date: "2014-11-11", place: "Miejsce11-11-A", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:58:01", updated_at: "2014-12-05 21:58:01">, #<Leaflet id: 15, date: "2014-11-11", place: "Miejsce11-11-B", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:58:04", updated_at: "2014-12-05 21:58:04">, #<Leaflet id: 16, date: "2014-11-11", place: "Miejsce11-11-C", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:58:07", updated_at: "2014-12-05 21:58:07">], Mon, 01 Dec 2014=>[#<Leaflet id: 4, date: "2014-12-01", place: "Miejsce1", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:56:42", updated_at: "2014-12-05 21:56:42">, #<Leaflet id: 5, date: "2014-12-01", place: "Miejsce2", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:56:55", updated_at: "2014-12-05 21:56:55">, #<Leaflet id: 6, date: "2014-12-01", place: "Miejsce3", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:01", updated_at: "2014-12-05 21:57:01">], Tue, 02 Dec 2014=>[#<Leaflet id: 7, date: "2014-12-02", place: "Miejsce02-A", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:21", updated_at: "2014-12-05 21:57:21">, #<Leaflet id: 8, date: "2014-12-02", place: "Miejsce02-B", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:24", updated_at: "2014-12-05 21:57:24">, #<Leaflet id: 9, date: "2014-12-02", place: "Miejsce02-C", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:28", updated_at: "2014-12-05 21:57:28">], Fri, 05 Dec 2014=>[#<Leaflet id: 10, date: "2014-12-05", place: "Miejsce05-A", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:40", updated_at: "2014-12-05 21:57:40">, #<Leaflet id: 11, date: "2014-12-05", place: "Miejsce05-B", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:43", updated_at: "2014-12-05 21:57:43">, #<Leaflet id: 12, date: "2014-12-05", place: "Miejsce05-C", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:46", updated_at: "2014-12-05 21:57:46">, #<Leaflet id: 13, date: "2014-12-05", place: "Miejsce05-D", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:48", updated_at: "2014-12-05 21:57:48">]}
Any idea what's wrong? Is it normal behaviour for grouping in Rails? How can I delete this big hash?
Screenshot: http://imgur.com/Nc9sYS7
Just remove the = from:
<%= #leaflets.group_by(&:date).each do |date, leaflets| %>
So that it reads:
<% #leaflets.group_by(&:date).each do |date, leaflets| %>
The = is outputing the results of the each call.
The each method returns the object you called each on. In ERB, <%= ... %> will display the result of the evaluated code, which is #leaflets.group_by(&:date) in this case. Simply remove the = sign to silently evaluate the code:
<% #leaflets.group_by(&:date).each do |date, leaflets| %>
...
<% end %>

How do I output in Views just the variables' values I want in Ruby On Rails?

Now in my application.html.erb in views folder, I wrote this.
<p>List of all post IDs: <%= Post.all.each {|i| print i.id } %></p>
I would like it to output just the post.id of each post. But instead it shows this
List of all post IDs: [#<Post id: 1, title: "Our First Post", content: "Content for our first post", created_at: "2012-11-24 11:22:02", updated_at: "2012-11-26 17:40:54", user_id: 1>, #<Post id: 3, title: "Our Second Post", content: "Content for our second post", created_at: "2012-11-24 11:51:32", updated_at: "2012-11-26 17:41:33", user_id: 2>, #<Post id: 8, title: "Our Second Post", content: "Content of Our mandatory Second Post", created_at: "2012-11-24 19:42:02", updated_at: "2012-11-27 20:46:57", user_id: 1>, #<Post id: 10, title: "C Post", content: "Hi I'm Cee nice to meet you", created_at: "2012-11-26 17:51:20", updated_at: "2012-11-26 17:51:20", user_id: 3>, #<Post id: 20, title: "11", content: "11", created_at: "2012-11-27 19:58:48", updated_at: "2012-11-27 19:58:48", user_id: 4>, #<Post id: 21, title: "22", content: "22", created_at: "2012-11-27 19:58:53", updated_at: "2012-11-27 19:58:53", user_id: 4>, #<Post id: 25, title: "I'm Super Singha!", content: "Yessar!!!", created_at: "2012-11-27 20:45:07", updated_at: "2012-11-27 20:45:07", user_id: 6>, #<Post id: 26, title: "Should this be a blog or a forums or a whatever-wha...", content: ";asljdfi;asfi;asdf;lasbfurbofioboboeifhosdsdbvisbvw...", created_at: "2012-11-27 20:46:28", updated_at: "2012-12-02 14:17:14", user_id: 1>, #<Post id: 27, title: "Hullow", content: "Yoyoyo", created_at: "2012-11-30 07:35:38", updated_at: "2012-11-30 07:35:54", user_id: 6>, #<Post id: 649, title: "um", content: "hey", created_at: "2012-11-30 12:20:58", updated_at: "2012-11-30 12:20:58", user_id: 2>, #<Post id: 82692, title: "LALALALAL", content: "hiopsdahfiosadhfioahfio", created_at: "2012-12-02 13:59:04", updated_at: "2012-12-02 14:22:41", user_id: 2>, #<Post id: 82693, title: "ggg", content: "fff", created_at: "2012-12-02 14:29:42", updated_at: "2012-12-02 14:29:42", user_id: 2>, #<Post id: 82694, title: "sick", content: "sick", created_at: "2012-12-02 14:41:32", updated_at: "2012-12-02 14:41:32", user_id: 5>]
I have tried, puts instead of print, that doesn't work either.
Further: I would also like to make a link_to each post show page from the intended result, how can I achieve that?
Here's my repo: https://github.com/nixor/cpblog , here's heroku site: http://still-plains-5469.herokuapp.com/
Thanks.
The problem is with how you are using the ERB tags here:
<%= Post.all.each {|i| print i.id } %>
Whenever you use <%=, the result of the block will be rendered. In your case, Post.all.each {} returns an array object, which is exactly what you are seeing in your rendered HTML.
In order to print out each item, you will need to loop through the items using <% and then print out what you want using <%=.
<p>List of all post IDs:
<% Post.all.each |post| do %>
<%= link_to post.id, post_path(post) %>
<% end %>
</p>
<p>List of all post IDs:
</p>
<%= Post.all.each do |e| %>
<p>
<%= e.Id %>
<p>
<%= link_to "Show", e %>
<% end %>

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

Resources