Loop in view is showing more result as expected by calling super(value.to_s) - ruby-on-rails-3

I am running Rails 3 with the following code in the view
View:
<%= #found_docs.each do |doc| %>
<%= doc.id | doc.content %>
<% end %>
As a result I get two objects, as expected - but in addition a third result is displayed. It is created by calling super(value.to_s) on the result set. Doing #founds_doc.count returns 2 as expected.
Why is the third object displayed when running the block, when #found_docs has only two objects ?

I found the solution - and I did a silly mistake !
The view code is as follow:
<%= #found_docs.each do |doc| %>
I put the "=" sign in front of the loop, hence the result of the loop is printed with value.to_s in the view.
Correct code is:
<% #found_docs.each do |doc|%>
Must have been blind :-)

Related

Rails List record and count for each

I have a model called Note. Each note belongs_to :call_reason. And call_reason has_many :notes.
What I want to do in a view is display a list of call_reasons and a total count of each next to it so we can see what the most popular call reasons are.
Here's what I have so far:
dashboard_controller:
def index
#notes = Note.all
end
dashboard view:
<% #notes.each do |n| %>
<%= n.call_reason.reason %>
<% end %>
This lists all notes' call_reasons.
I'm stumbling on how to list each call_reason once with a total count next to it. What I have now just lists all the call_reasons per note which is a mess. I think I could scope this out somehow or change the instance variable but I'm having a hard time getting it right.
Any thoughts?
Since you want to list call reasons, you should start with that:
def index
#call_reasons = CallReason.all
end
Then in your view you can do this:
<% #call_reasons.each do |call_reason| %>
<%= call_reason.reason %> <%= call_reason.notes.count %>
<% end %>
Note that this will perform a query for every call reason in your database. To prevent this you can use a counter cache. Check out the section on counter cache on Rails Guides too.

Rails ERB -- get the generated HTML content to a variable I can access in a controller

I am doing an online contract agreement which is generated dynamically based on the current state of our contract language (stored in an ERB partial).
When the user clicks "I Agree" we save the variable data, but I also want to store the contents of the rendered partial (html fragment) in the database.
I tried using ActionView::Helpers::CaptureHelper.capture -- wrapping the whole partial in a block and rendering at the end, like
<% #saved_output = capture do %>
...
The rest of the erb with instance variable <%= #my_class.name %> and so on
...
<% end %>
<%= #saved_output %>
<% logger.debug "Rendered output is: #{#saved_output}" %>
And this produced the same result and also sent the correct text to the log. But it appears to go out of scope -- even if I declare #saved_output = nil prior to render it's nil when I end the block.
I tried using content_for as well ... which makes some sense to me, but ... huh, just not getting it. Any helpful pointers appreciated.
Have you tried render_to_string in your controller when you're creating the record? That may allow you to just go ahead and "snapshot" the page.

Access variables dynamically in views on RoR3

I have 10 instance variables called #foo_01, #foo_02, #foo_03,.. #foo_10 and I need to show the result sets of each variable on a page, these results must be displayed in a correct order with a rather lengthy algorithm that I rather not copy and paste 10 times changing the name of the variables as I find this extremely annoying, so I'm looking for a way to loop through the algorithm 10 times and dynamically access the variables, something sort of like this...
<% (1..10).each do |i| %>
<% lengthy algorithm here %>
<%= #foo_i .atribute.value %>
<% end %>
<% end %>
Can't #foo be an array with your ten elements ? Then you could iterate over it with #foo.each.

Rails 3 - how to work with data got from database

I am loading from database the only row. The data are stored in variable (e.g.) #data.
In view, if I want to display the value got from database, I have to do following:
<% #data.each do |d| %>
<%=d.name %>
<%end%>
And I would like to ask you - exist any better way? I think it's a bit silly for the only row to use loop... I tried something like
<%= #data.name %>
OR
<%= #data.each.name %>
But in both cases I got the error message about bad syntax...
So to my question - is possible to get display data a bit more elegantly?
EDIT: my query: #data = Car.includes(:tyres).where("param1 = ?", params[:param1])
If you've loaded more than one model (row), then a loop is the natural construct for displaying each value. If you're really set on a one-liner, you could use some of Ruby's list comprehensions:
<%= #data.map(&:name).join(" ") -%>
I think that you are loading .all instead of .first.
In your controller,
#data = Data.where(:some => 'condition').first
or
#data = Data.find(params[:id])

Mapped array error in Rails 3

I've spent 3 days trying to write scopes in models. At this point i don't care about producing the optimum solution... i just want to get this to work. In a rails 3 app i have the following code in the controller.
#questions = Question.all
#ans = Answer.where(user_id = current_user.id)
#answers = #questions.map { |q| [q, #ans.find_by_question_id(q.id)] }
Each answer record has a question_id field so it can be linked to the appropriate question. I am trying to get the answers array to list out in the same order as the questions.
The following code in a view renders the answers but not in the correct order.
<% #ans.each do |q| %>
<%=q.score%><br/>
<% end %>
I then changed the array to the mapped array which should produce the answers in the appropriate order.
<% #answers.each do |q| %>
<%=q.score%><br/>
<% end %>
I get the following error:
undefined method `score' for #<Array:0x10335ef90>
Any help is appreciated. Thanks.
Instead of q.score you probably want q[1].score, since q is really a two-item array of the question and answer. The second item (q[1]) will give you the answer.
This is because q is not an Answer, but an array containing both a Question and an Answer. So you could just change it to q[1].score. A nicer way is to break out the array in the block variables, like so (note the parentheses):
<% #answers.each do |(question, answer)| %>
<%= answer.score %><br/>
<% end %>