Show specific data in a index view that belongs to the view - ruby-on-rails

Creating a simple todolist. Trying to show the description for a task that belongs to a list in the list index view. With the current code all data fields shows up from all tasks that belongs to the list when i use list.tasks, but i only want the task.desc. How do i specifically tell rails that i only want the task.desc to show up?
lists/index.html.erb
<h1>My Lists</h1>
<%= link_to 'New List', new_list_path, class: "btn btn-primary" %>
<h3>Name | Time Ago</h3>
<% #lists.each do |list| %>
<h4><%= link_to list.name, list %></td></h4>
<% if list.created_at > Time.now.beginning_of_day %>
<%="#{time_ago_in_words(list.created_at)} ago"%>
<% else %>
<h6><%= list.created_at.strftime("%b %d, %Y") %></h6>
<%= list.tasks %><br />
<% end %>
<%= link_to 'Edit', edit_list_path(list) %> |
<%= link_to 'Destroy', list, method: :delete, data: { confirm: 'Are you sure?' } %>
<br />
<% end %>
<br />

If i understand correctly your problem, you can use .each, which you have used for list iteration:
<% list.tasks.each do |task| %>
<%= task.desc %><br />
<% end %>

To get an array of each description
list.tasks.collect(&:desc)
The & is a Rails shortcut that got into Ruby 1.9 - it says, apply the method after the & to each member of the collection.
I think you might want
list.tasks.collect(&:desc).join("<br />").html_safe
:)
(you might not need html_safe, it's used to show you're deliberately putting markup in a string and you don't want it to be escaped)
If this works for you I would take it out of the view and put it in the list class. As in
def current_tasks
tasks.collect(&:desc)
end
This means you can test this and reuse it later.

Related

how to sort a printed out list by DESC

I'm printing out a list of users that have answered questions asked by company:
<% #applications.all.each do |application| %>
<% application.answers.each do |answer| %>
<p>Your question1: <%= answer.application.question_1 %></p>
<p>Their answer 1: <%= answer.answer_1 %></p>
<p>Your question 2: <%= answer.application.question_2 %></p>
<p>Their answer 3: <%= answer.answer_2 %></p>
<p>Your question 3: <%= answer.application.question_3 %></p>
<p>Their answer 3: <%= answer.answer_3 %></p>
<i>Answered by <b><%= link_to "#{answer.user.fullname}", user_path(answer.user.slug) %></b>, <%= time_ago_in_words(answer.created_at) %> ago</i> | <%= button_to "delete this applicant", root_url, data: { confirm: "Are you sure?"}, method: :delete %>
<hr>
problem now is that they aren't in any specific order. I would like to sort 'created_at DESC' but haven't been able to successfully do so. Do I do it in the view, or in the model? What's recommended here and how do I do it.
You do this sort of thing in the controller's method that returns your page, ex: index:
#applications = Application.all.order("created_at DESC")
If you want to further organize your logic, you can start defining scopes in your model and then use them in your controller.
Do not define anything other than basic conditional view logic in your views.

Display Acts_as_taggable Tags seperatly

I'm trying to Display each Tag a Question has in a Span. I use acts_as_taggable_on.
I managed to get the below Code working, but it's a Tag cloud. Meaning that ALL the tags are displayed.
<% tag_cloud Question.tag_counts, %w[s m l] do |tag, css_class| %>
<span class="label label-default">
<%= link_to tag.name, tag_path(tag.name), class: "css_class" %>
</span>
<% end %>
To retrieve the Tags:
question.tag_list
Can someone help me refactor that code so only the CURRENT TAGS ON THE QUESTION are shown ?
question.tag_list will return you a string, and you can not loop through it.
question.tags will return an array,
<% question.tags.each do |tag| %>
<span class="label label-default">
<%= link_to tag.name, tag_path(tag.name), class: "css_class" %>
</span>
<% end %>
I am not aware of the plugin, but one problem I see in your code-snippet in general is that you are not operating on a specific object #question, but on the class Question. If I had to take a guess, I would say that this is the source of your problem.
Edit:
So, I just checked out the documentation for the gem and I found this code-snippet directly there:
<% tag_cloud(#tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
<% end %>
As you can see, this is pretty much what I just told you. Instead of working on the class you have to work on a specific object or, like in the shown case, a collection of objects.

Ruby on Rails: text filed

I have a code where a user can select a specific file to be deleted or analyzed.
<% if #files%>
<%= form_tag what_to_do_files_path, method: :get do %>
<%= submit_tag "Delete selected", :name => 'delete' %>
<%= submit_tag "Analyse", :name => 'analyse' %>
<% #files.each do |file| %>
<% if (file.analyzed=="no") %>
<p><td> <%= check_box_tag "files[]", file.id %></td><%= file.name %></p>
<% else %>
<div class="my_profile_info">
<p><td> <%= check_box_tag "files[]", file.id %></td> <%= file.name %></p>
<td class="Info">
Info
</td>
</div>
<% end %>
<%end%>
<%end%>
<%else%>
<%end%>
I need to be able to give a name to every analysis.
For example: user selects 3 files, enters a name in the text field "Analysis of annual profit" and clicks on the button "Analyse".
The name "Analysis of the annual profit" and the names of the files that were selected have to be saved into the table group_analysis.
I have tried something like this after submit_tag "Analyse":
<%= form_for #groupanalysis do |f| %>
<div class="field_label">
<%= f.label :group_name, "Type group name hier" %>
</div>
<br class="clear" />
<br />
<% end %>
but it tells me undefined method model name
Thanks in advance.
I think you may need to take a step back and think of how this form represents the model that you're trying to create or update. Generally speaking the first argument to form_for and form_tag is an object and symbol, respectively, which represent the model that you're working with. The form fields map to each attribute of the object.
According to conventions and/or the :url argument, this will get routed to the appropriate controller and call an action according to the HTTP verb (again, part of many conventions in rails).
Going back to your code examples, you are using the form_tag helper incorrectly and the example using form_for may not be the right implementation. For example you're just displaying a label, with no input nor submit.
I hate to just post a link here and just tell you to read the docs, but in this case I think this is the best first step.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

How do I wrap link_to around some html ruby code?

How do I wrap a link around view code? I can't figure out how to pass multiple lines with ruby code to a single link_to method. The result I am looking for is that you click the column and get the show page:
<div class="subcolumns">
<div class="c25l">
<div class="subcl">
<%= image_tag album.photo.media.url(:thumb), :class => "image" rescue nil %>
</div>
</div>
<div class="c75r">
<div class="subcr">
<p><%= album.created_at %></p>
<%= link_to h(album.title), album %>
<p><%= album.created_at %></p>
<p><%= album.photo_count %></p>
</div>
</div>
</div>
link_to takes a block of code ( >= Rails 2.2) which it will use as the body of the tag.
So, you do
<%= link_to(#album) do %>
html-code-here
<% end %>
But I'm quite sure that to nest a div inside a a tag is not valid HTML.
EDIT: Added = character per Amin Ariana's comment below.
Also, this may be an issue for some:
Make sure to write <%= if you are doing a simple link with code in it instead of <%.
e.g.
<%= link_to 'some_controller_name/some_get_request' do %>
Hello World
<% end %>
For older Rails versions, you can use
<% content_tag(:a, :href => foo_path) do %>
<span>Foo</span>
<% end %>
You can use link_to with a block:
<% link_to(#album) do %>
<!-- insert html etc here -->
<% end %>
A bit of a lag on this reply I know -- but I was directed here today, and didn't find a good answer. The following should work:
<% link_to raw(html here), #album %>

How do I set a unique ID for checkboxes in a multi-record Rails form?

I've set up a Rails form roughly following the instructions in this Railscast.
Here's the code for the form:
<% form_tag complete_todos_path, :method => :put do %>
<ul>
<div id="incomplete_todos">
<% #incomplete_todos.each do |todo| %>
<%= render :partial => todo %>
<% end %>
</div>
</ul>
<%= submit_tag "Mark as completed" %>
<% end %>
And here's the code for the todo partial:
<div class="todo">
<li>
<%= check_box_tag "todo_ids[]", todo.id %>
<%=h todo.name %>
<%= link_to 'edit', edit_todo_path(todo) %>
<%= link_to 'delete', todo, :confirm => 'Are you sure?', :method => :delete %>
</li>
</div>
It's working great, but I'm looking to start implementing AJAX and I need each checkbox to have a unique id. Right now, the input tags generated look something like this:
<input id="todo_ids_" name="todo_ids[]" type="checkbox" value="7" />
Every check box has the same id ("todo_ids_"), which is a problem. I suspect the solution is embarrassingly simple, but I'm not seeing it. Any tips?
<%= check_box_tag "todo_ids[]", todo.id, false, :id => "todo_id_#{todo.id}" -%> or whatever you want the id to be.
I consider this a bug with check_box_tag caused by the seemingly hackish nature of manually giving it the name todo_ids[] and the method code calling sanitize_to_id(name). I just ran into this yesterday and I'm contemplating a patch.
I ended up using a solution similar to Ryan's, but as I wrote in the comment I had to make a further change. In the form:
<%= check_box_tag "todo_ids[#{todo.id}]", todo.id %>
In the action called by the form:
Todo.update_all(["completed_at = ?", Time.now], :id => params[:todo_ids].keys)
Note the "params[:todo_ids].keys" at the end, which was a workaround to deal with the odd way the parameters were formatted:
"todo_ids" => {"5"=>"5"}
Can you try this and let us know if it works:
check_box_tag "todo_ids[#{todo.id}]", todo.id %>
This is the expected behaviour of check_box_tag, as this comment on a rejected fix explains.
You can use collection_check_boxes like this (haml syntax, sorry):
# Accumulate todos in a params hash like { todos: { to_complete: [] } }
= collection_check_boxes(:todos, :to_complete, #incomplete_todos, :id, :name) do |todo_builder|
= todo_builder.label do
# This is the result of calling :name on the todo, as specified
# calling the helper
= todo_builder.text
= todo_builder.check_box
Of course you can use partials inside the block, just pass and use the builder inside.
Check more options in the API docs.

Resources