How to convert string to YAML in Ruby 2.3.0? - ruby-on-rails

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

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

Object string are appearing in the loop

The object string log appearing in some pages, how can I remove this?:
"Tv id: 1, name: "SUPER HD", juridica: false, created_at: "2017-06-12 15:55:24", updated_at: "2017-06-12 15:55:24", points: 10>, #<Tv id: 2, name: "ULTRA HD", juridica: true, created_at: "2017-06-12 15:55:35", updated_at: "2017-06-12 15:55:40", points: 15"
Check the image:
Remove the = from your iterator, maybe you have something like:
<%= #variable.each do |value| %>
So, that's printing the each block, try changing it to:
<% #variable.each do |value| %>
In order to just print the content that's being iterated.
<%=#variable%>
It Used to display values
So u dont use
=
in your conditions
you use "=" below
<%=form_for%>

Create a new hash out of Active Record results

I need to extract some of the questions i have in my database based on current_user and present them to the Current User.
So i have all questions in database, but i need only those that the current_user does not exist on the relationship between users and questions.
My code so far goes like this
#all_questions = Hash.new
counter = 0
ill_questions = Question.all
ill_questions.each do |q|
if !q.users.include? current_user
#all_questions[counter] = q
counter += 1
end
end
puts "these are all unanswered questions #{#all_questions}"
when i print #all questions it gives this
{0=>#<Question id: 9, question_type: "multiple", description: "This is a question", expl
anation: "", category_id: 2, created_at: "2015-11-05 20:02:05", updated_at: "2016-02-11 19:23:02", link_name: "", link: "",
video_url: "", image: nil, image_position: "explanation", metric: "metric test", imperial: "testers", description_type: tr
ue, restriction: false>, 1=>#<Question id: 10, question_type: "single", description: "This is another question", explanatio
n: "test", category_id: 10, created_at: "2015-12-10 12:57:10", updated_at: "2016-01-12 23:36:25", link_name: "", link: "",
video_url: "", image: nil, image_position: "question", metric: nil, imperial: nil, description_type: true, restriction: tru
e>, 2=>#<Question id: 11, question_type: "single", description: "correct-wrong", explanation: "", category_id: 11, created_
at: "2016-01-29 19:53:48", updated_at: "2016-01-29 19:53:48", link_name: "", link: "", video_url: "", image: nil, image_pos
ition: "question", metric: nil, imperial: nil, description_type: true, restriction: true>, 3=>#<Question id: 12, question_t
ype: "single", description: "New question", explanation: "", category_id: 10, created_at: "2016-01-29 19:54:18", updated_at
: "2016-01-29 19:54:18", link_name: "", link: "", video_url: "", image: nil, image_position: "question", metric: nil, imper
ial: nil, description_type: true, restriction: true>}
in my view i have this
<% #all_questions.each do |q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>
getting error on <p class="question-title"> <%= q.description %> </p>
undefined method "description" for #<Array:0x007fe15ba18c88>
the relationship between User and Question
User has_many :questions, :through => :trackers
Question has_many :users,through: :trackers
Tracker model
Tracker(id: integer, user_id: integer, question_id: integer, answered: boolean, correct: boolean, created_at: datetime,
updated_at: datetime, category_id: integer)
What i would like to show to the current user is the questions that he did not touch, as in he does not have a relationship yet.
i assume i have something wrong in my new structure, as i see not it became an array??
Try with:
#all_questions = Question.where.not(id: current_user.question_ids)
And then use #all_questions in your view just like you wrote.
Change
<% #all_questions.each do |q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>
To:
<% #all_questions.each do |_, q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>
If you really don't need the key, a better solution will be:
<% #all_questions.each_value do |q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>
#all_questions is a Hash, when use each on it, it passing the key-value pair as block parameters, you need two parameters for them, if you only provide one parameter, it will be an array like [key, value].
In your controller:
#all_questions = Question.includes(:users).where("questions.id NOT IN (?)", current_user.questions.pluck(:id))
In views:
<% #all_questions.each do |q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>

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

Resources