This is my current page view below:
<%= link_to "Back", root_path, :class => "pull-right time-padding" "btn btn-primary btn-sm"%>
<div class="entries">
<% #entries.each do |entry| %>
<h2><%= link_to entry.competition.name, entries_path %></h2>
<li><%= link_to entry.competitor.full_name %></li>
<br/>
<% end %>
</div>
I would like to display only unique entry.competition.name and then all the associated entry.competitor.fullname. Currently it loops over every entry of both and displays them.
I checked how to display individual listings? Ruby on Rails and how to display a link to individual microposts? (ruby on rails 3), but am not sure how those connect to the function I am seeking.
I am looking at link_to entry.competition.name.uniq, but I cannot grok how to implement something like this.
This will get one value for each competition name:
#entries.reduce({}){|m,e| m[e.competition.name] = e.competition; m}.values
You can do likewise for full.name
Related
I have seven different assignment objects. I'm currently iterating through each instance and then I iterating through the users of each assignment. I'm trying to designate the users to the assignment. I have a has_many through table that assigns the user to the assignment. But, my problem right now is that it keeps selecting the first assignment when I'm trying to select the fourth assignment. The way I'm iterating with the HTML is wrong but I can't seem to figure out what I'm doing? Here is my HTML.
HTML:
<div class="assignments">
<% #assignments.each do |assignment| %>
<ul>
<li><%= link_to assignment.name, account_assignment_path(assignment) %></li>
<%= link_to "designate Worker", "#designate", class: "button", data: { designate_worker: "" } %>
<div id="workers-modal", class="modal--worker hidden">
<% #account.account_workers.each do |worker| %>
<ul>
<li><%= link_to worker.name, designate_account_assignment_path(assignment, user_id: worker.id) %></li>
</ul>
<% end %>
</div>
</ul>
<% end %>
</div>
As you can see I'm iterating through all the assignment on the current account. Then, I'm iterating through all the users on the current account. Then, I'm trying to select the particular assignment by the id and the user by the id and create a relationship between the two, But from the UI I can't select any assignment but the first one. I think this has something to do with the way I'm iterating but I don't know for sure.
Controller:
def designate
designated_user = current_account.users.find_by(id: params[:user_id])
membership = designated_user.assignment_relationships.find_or_create_by(assignment_id: params[:id])
membership.update!(designated: true)
flash[:notice] = "Successfully designated the user"
redirect_to root_path
end
As you can see. The second line in the method looks for the assignment_id with the params[:id]. Right now it only comes in as :id => "1", when I'm trying to get :id => "2" or "4"
Let me know if you need to see any other code. Thanks!
Do you really want to start a new list for each assignment and each worker. If you inspect the view, do you see each of the assignments with different ids? Try amending the view so the unordered list tags are outside the each loop:
<div class="assignments">
<ul>
<% #assignments.each do |assignment| %>
<li><%= link_to assignment.name, account_assignment_path(assignment) %></li>
<%= link_to "designate Worker", "#designate", class: "button", data: { designate_worker: "" } %>
<div id="workers-modal", class="modal--worker hidden">
<ul>
<% #account.account_workers.each do |worker| %>
<li><%= link_to worker.name, designate_account_assignment_path(assignment, user_id: worker.id) %></li>
<% end %>
</ul>
</div>
<% end %>
</ul>
</div>
I'm using acts_as_taggable_on to tag posts and user profiles. Tag records are being saved correctly with taggable_type post or profile, and context tags and interests. Acts_as_taggable_on sends <%= render post.tags %> and <%= render profile.interest %> to views/acts_as_taggable_on/tags/_tag.html.erb, where the readme suggested a link_to like <%= link_to tag.name, tag_path(tag.name) %>.
What I'd like to do is change the path depending on either the taggable_type or the context--something like if tag.tagging.context == 'interest' link_to tag.name, profile_tag_path(tag.name) etc for post tags vs profile tags.
In the respective views/controllers, I can get the records correctly filtered by context, I'm just having trouble changing the link path conditionally. tag.tagging.context gives a no method error, tag.taggings.each do |tag| tag.context returns all contexts for a given tag for every time it's been used--ie, 4 prints of 'interest' and 10 prints of 'tags' if it's been used multiple times on both profiles and posts. What I'd like is just the context for the current instance.
I'd also be happy to hear if there's another way to do this (ie, link to users/tagged/tag to return just profiles tagged with 'tag' and /tagged/tag to return just posts tagged with 'tag', etc), it just seemed like it should be pretty straightforward.
In case it's useful to others, I ended up iterating over each tag like so:
<span class="global tags">
<% #post.global_list.each do |tag| %>
<span class="label label-default">
<%= link_to tag, post_tag_path(tag) %>
</span>
<% end %>
</span>
<span class="personal tags">
<% #post.personal_list.each do |tag| %>
<span class="label label-default">
<%= link_to tag, user_tag_path(#post.user.name, tag) %>
</span>
<% end %>
</span>
I have the current code in my traders.index.html file
<ul>
<% #traders.each do |trader| %>
<li><%= link_to trader.name, trader %></li>
<%end%>
</ul>
I want to add an extra parameter to be sent through, I tried
<li><%= link_to trader.name, trader, {:restricted => params[:s]} %></li>
But this doesn't send the parameter, whats the actual format of the link_to to get this done?
You can do:
<%= link_to trader.name, trader_path(trader, restricted: params[:s]) %>
I'm building a Rails application that allows the user to create a Book object based on search results from the Google Books api. I have a controller that handles searching the api, using the GoogleBooks gem and displays the results in a list. I'm struggling to figure out a way to pass book information from a single search result into my Create action in the Books controller.
My search controller takes params from my search form and creates a variable, #results, that I'm calling in a 'search' view. Here is my search controller:
class SearchController < ApplicationController
def search
#results = GoogleBooks.search(params[:search])
end
end
My view looks like this:
<h1>Search Results</h1>
<% #results.each do |result| %>
<ul>
<li><%= result %></li>
<li><%= result.title %></li>
<li><%= result.authors %></li>
<li><%= result.isbn %></li>
<li><%= result.description %></li>
</ul>
<% end %>
The problem that I'm having is that I'm not sure how to pass individual result data on to my Book controller's 'create' action to generate a new book in the database. I don't think I can pass 'result.title' or 'result.author' to the Book controller for example because they aren't instance variables and there is also no way to distinguish between each result.
My page source for search results looks like this, if that is any help.
<h1>Search Results</h1>
<ul>
<li>#<GoogleBooks::Item:0x007ff75d2bd138></li>
<li>Kansha: Celebrating Japan's Vegan and Vegetarian Traditions</li>
<li>Elizabeth Andoh</li>
<li>9781607743965</li>
<li>The celebration of Japan’s... </li>
</ul>
<ul>
<li>#<GoogleBooks::Item:0x007ff75d2bc148></li>
<li>Advanced Energy Saving and Its Applications in Industry</li>
<li>Kazuo. Matsuda, Yasuki. Kansha, Chihiro. Fushimi</li>
<li>9781447142072</li>
<li>The conventional approach for... </li>
</ul>
I would like to use a button to allow a user to 'select' the book and then pass on the book's information to the create action. I'm thinking I need to do something like this with button_to:
<%= button_to 'Create Book', book_path, :method => :post %>
But how do I get the a single book's data to the create action?
Depending on your overall layout, I suggest start by building an in-line form for each Book result. You can do this with hidden fields. As mentioned by gregates, it's hard to provide the complete answer, but this might get you going in the right direction.
In your SearchController:
def search
#results = GoogleBooks.search(params[:search])
#books = []
#results.each do |result|
#books << Book.new
end
end
In your View:
<% #results.each_with_index do |result, index| %>
<%= form_for(#books[index]) do |f| %>
<ul>
<li><%= result.title %></li>
<li><%= result.authors %></li>
<li><%= result.isbn %></li>
<li><%= result.description %></li>
</ul>
<%= f.hidden_field :title, value: result.title %>
<%= f.hidden_field :authors, value: result.authors %>
<%= f.hidden_field :isbn, value: result.isbm %>
<%= f.hidden_field :description, value: result.description %>
<%= button_to 'Create Book', controller: 'books', action: 'create' %>
<% end %>
<% end %>
Again, depending on your overall design, there's probably a more elegant approach but hopefully this will provide some inspiration.
Good luck!
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.