I want to count the number of comment on my page called view
here is my controler code
def view
#gallery = Gallery.find_by!(id: params[:id]).decorate
#comments = Comment.select(:user_id, :description).includes(:user).where(gallery_id: #gallery.id)
if user_signed_in?
#like = current_user.likes.where(gallery_id: #gallery.id).first
end
end
this is view page
.text-container
p: strong Komentar
- #comments.each do |comment|
.media.testimoni-box
.col-md-12.jaminan
.media-heading.strong = comment.user_personal_name
= comment.description
- # want to show the number of comment here
Please help me. and thanks for advance
you can loop the #comments collection with_index and use that as counter
.text-container
p: strong Komentar
- #comments.each.with_index(1) do |comment, count|
.media.testimoni-box
.col-md-12.jaminan
.media-heading.strong = comment.user_personal_name
= comment.description
= count
Related
I had a value that is inserted in a input at index and led me to a show page.
But, when I insert and submit, I gain a Blank Page! However, if I type in the URL the path to the show item, it appears!
Print from the input on client side:
https://imgur.com/a/ZSnacmM
I tried and get the following result with biding pry
https://imgur.com/a/d3H9VAi
The "entrega" really comes with the input that is the cient_number, but #entrega does'nt
So, When goes to if, all the values are nil. I really don't understand
I've tried the following code at Controller:
def sac_index
#binding.pry
#objectives = DeliveryObjective.all
#search = params["search"]
if #search.present?
#entrega = #search["client_number"]
#objectives = DeliveryObjective.where(client_number: #entrega)
end
##search_uol = DeliveryObjective.where(client_number: params[:id])
end
def sac_show
##delivery_objective = DeliveryObjective.where(client_number: #search)
#delivery_objective = DeliveryObjective.where(client_number: params[:id])
binding.pry
end
When I input the client_number, I want to be rendered to the show page from the item.
Based in an earlier response, I resolved the conflict, changing the Controller:
def index
#objectives = DeliveryObjective.all
#search = params["search"]
if #search.present?
#entrega = #search["client_number"]
#objective = DeliveryObjective.where(client_number: #entrega)
end
end
def show
#search = params["search"]
if #search.present?
#entrega = #search["client_number"]
#delivery_objective = DeliveryObjective.where(client_number: #entrega)
end
end
It's a little bit dirty, but can help someone.
I am a newbie at rails and i wanted help on how to merge collections of two active record objects?
An article has many comments
class Article < Content
has_many :comments
I want to create a new article with comments merged from the comments of source and target article objects.Below, source_id and target_id are ids of two Articles. I want to get comments from them and merge them and add to the new article.
source_id = params[:id]
target_id = params[:merge_with]
#article = Article.get_or_build_article()
#article.allow_comments = true
article_source = Article.find(source_id)
article_target = Article.find(target_id)
#reassign all comments of first article
first_comments = article_source.comments
first_comments.each do |c|
c.article_id = #article.id
c.save
end
#reassign all comments of second article
second_comments = article_target.comments
second_comments.each do |d|
d.article_id = #article.id
d.save
end
#article.title = article_source.title
#article.body = article_source.body + " " + article_target.body
#article.author = article_source.author
#article.save
I see that the new article is created but it doesnt display any comments. So, the linking is broken somewhere. I appreciate any help! Thanks!
You have not saved #article in database. So #article.id is nil.
first_comments.each do |c|
c.article_id = #article.id
c.save
end
So this loop, assigns nil to c.article_id. So first save the #article then update comments. Also use update_all for updating comments instead of looping.
source_id = params[:id]
target_id = params[:merge_with]
#article = Article.get_or_build_article()
#article.allow_comments = true
article_source = Article.find(source_id)
article_target = Article.find(target_id)
#article.title = article_source.title
#article.body = article_source.body + " " + article_target.body
#article.author = article_source.author
#article.save
#reassign all comments of first article
first_comments = article_source.comments
first_comments.update_all(article_id: #article.id)
#reassign all comments of second article
second_comments = article_target.comments
second_comments.update_all(article_id: #article.id)
Or even better to update comments use this
Comment.where(article_id: [article_source.id, article_target.id]).update_all(article_id: #article.id)
Hi I create a controller Game to display a Q/A game
And I am blocked with <<, here is the code
def play
lvlup(lvl)
if lvl == 1
set_questions
else
get_questions
end
#answers = Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()")
#answer ||= []
#answers << question.answer
#answers = #answers.shuffle
render 'play'
end
I create an array and I put the related answer in the global answers I want to display 4 Max.
Why does the undefined is here?
Here is the total code
class GamesController < ApplicationController
attr_accessor :lvl
def welcome
end
def congrat
end
def play
lvlup(lvl)
if lvl == 1
set_questions
else
get_questions
end
#answers = Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()")
#answer ||= []
#answers << question.answer
#answers = #answers.shuffle
render 'play'
end
def loose
#question = Question.find(params[:question])
flash.now[:alert] = "Miss..."
render 'loose'
end
def check
#lvl = params[:lvl].to_i
answer_id = params[:id].to_i
question = Question.find(params[:question])
if #lvl == lvlmax
render action: 'congrat' and return
elsif answer_id == question.answer_id
flash.now[:notice] = "Well done !"
play
else answer_id != question.answer_id
loose
end
end
private
def lvlup(value)
#lvl = 1 + value.to_i
end
def lvlmax
#lvlmax = Question.all.count
end
def set_questions
#questionsids = []
Question.all.shuffle.each do |d|
#questionsids << d.id
end
cookies[:questions] = #questionsids
end
def get_questions
#questions = cookies[:questions].split('&')
end
def questions
#questions = cookies[:questions]
end
def question
#question = Question.find(questions[lvl])
end
end
Thank you for your help.
You are trying to append to the #answers result - this is an ActiveRecord relation, you cannot append data to that array.
Add .to_a in the end of your line where you set #answers to allow you to append to the array.
#answers = Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()").to_a
mtrolle's answer might be correct, but I have my doubts as to why ActiveRecord::Relation was not returned as Array by default. (Also as mentioned by BroiStatse in his comment.)
I too noticed the same problem with my local setup however it was attributed to another issue all together. I am sharing this here in case you too happen to use MySQL.
Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()")
returns an ActiveRecord::Relation object. And it translates to following SQL:
SELECT `answers`.* FROM `answers` WHERE (id != ID) ORDER BY RANDOM() LIMIT 2
When I try running the same in MySQL, I get:
ERROR 1305 (42000): FUNCTION database.RANDOM does not exist
Apparently MySQL does not have RANDOM() function, instead it uses RAND().
Converting ActiveRecord query accordingly returned correct Array to me:
Answer.where.not(id: question.answer_id).limit(2).order("RAND()")
Working on final class project. I need to calculate the GPA of my major credits and non major credits separately for a transcript page. When I run this code below as a controller it works fine and show my total credit hours for major and non major but when I put this code
#GPA_for_major = (course.credits * course.grade.scale) / course.credits
in the If statement I get NoMethodError in TransController#transcript
undefined method 'credits' for # Course::ActiveRecord_Relation:0x00000007b99798>
class Transcript
def initialize (course_array)
#course = course_array
#total_non_major_credits = 0
#total_major_credits = 0
#GPA_for_major = 0
#GPA_for_non_major = 0
for item in #course
if item.is_for_major
#total_major_credits = #total_major_credits + item.credits
else
#total_non_major_credits = #total_non_major_credits + item.credits
end
end
end
def course
#course
end
def total_non_major_credits
#total_non_major_credits
end
def total_major_credits
#total_major_credits
end
def GPA_for_major
#GPA_for_major
end
def GPA_for_non_major
#GPA_for_non_major
end
end
This is the Controller for my transcript page
class TransController < ApplicationController
def transcript
#courses = Course.all
#transcript =Transcript.new(#courses)
end
end
I'm not sure what else to include because this is my first post but any help will be awesome! Thanks!
#course appears to refer to an array of courses and the if statement is within a loop that iterates over the items setting a local variable item for each course. Given that, you should use item instead of course:
#GPA_for_major = (item.credits * item.grade.scale) / item.credits
#preComments = Comment.where(:resource_hash => resource_hash).sort(:created_at.desc).all
#preComments.each do |comment|
u = ::User.find_by_id comment.user_id
p u
#comments << #preComments
p "HERE!!!!!"
end
That's my code, but #comments isn't defined so I get an error:
You have a nil object when you didn't expect it! You might have
expected an instance of Array. The error occurred while evaluating
nil.<<
If I create an array first, then my view can't read it. So how do I do this?
The problem is that the first time you iterate, you want to create the #comments array (containing that item), but all subsequent times you want to push that item onto the existing array. There's probably a more elegant way to do this, but I generally just do this:
#comments ? #comments = [comment] : #comments << comment
Create the array before the loop using #comments = [], then in the loop make sure you're using #comments << comment, not #comments << #preComments.
I think you must initialize array
#preComments = Comment.where(:resource_hash => resource_hash).sort(:created_at.desc).all
#comments = []
#preComments.each do |comment|
u = ::User.find_by_id comment.user_id
p u
#comments << comment
p "HERE!!!!!"
end
or when loop is finished then pass values of #preComments to #comments
#preComments = Comment.where(:resource_hash => resource_hash).sort(:created_at.desc).all
#preComments.each do |comment|
u = ::User.find_by_id comment.user_id
p u
p "HERE!!!!!"
end
#comments = #preComments