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>
Related
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
Well, i have a problem, and i was wondering if it could be solved with rails only.
I have setup a view (home.html.erb) vith 3 partials, like this:
<%provide :title, 'Reader'%>
<div class = "row">
<div class = "span4">
<div class = "row">
<%= render 'layouts/add_subscription'%>
</div>
<div class = "row">
<%= render 'layouts/subscription_list'%>
</div>
</div>
<div class = "span8">
<div class = "row">
<%= render 'layouts/view' %>
</div>
</div>
</div>
where subscription_list shows up a list of links pointing to the list action after a redirection, each of them with the id of the subscription:
<ul>
<% current_user.subscriptions.each do |s| %>
<li><%= link_to s.url, "/list?s_id=#{s.id}" %></li>
<% end %>
</ul>
So, each of these links points to the list action in the controller, which tries to fetch the feed list of the subscription just clicked, and update the home view with the list of titles for the selected subscription:
def list
s_id = params[:s_id]
feed = ""
if !s_id.blank?
s = Subscription.find_by(id: s_id)
feed = Feedzirra::Feed.fetch_and_parse(s.url)
#render partial: "layouts/view", :locals => {:f => feed}
end
The problem is that I'm stuck at this point. I've tried to do a redirect_to home_path with feed as a parameter, and even a render (the line before the end of the list method) to see what happened, but nothing updates 'just' the layouts/view partial:
<ul>
<% if defined? feed and !feed.blank? %>
<% f.entries.each do |entry|%>
<li><%= entry.title %></li>
<% end %>
<% end %>
</ul>
So, I was wondering if it's possible to update the partial and see the result after a page reload using only rails methods, or if it can/must be done using javascript, and a clue to how to do this. Thanks in advance.
The goal you want to achieve is to show feed entries in the home.html.erb after clicking a link.
You can do it by pointing your links to the home action instead of list so that rails will automatically render your home.html.erb view and
you have to assign the instance variable #feed so it will be visible in your view.
You can do it like this (refactored a bit):
controller
def home
s_id = params[:s_id]
if s_id.present?
s = Subscription.find_by(id: s_id)
#feed = Feedzirra::Feed.fetch_and_parse(s.url)
end
end
layout/view
<ul>
<% if #feed.present? %>
<% #feed.entries.each do |entry|%>
<li><%= entry.title %></li>
<% end %>
<% end %>
</ul>
I'm not sure what is the path to your action, I assume here that home is the root ("/")
layouts/subscription_list
<ul>
<% current_user.subscriptions.each do |s| %>
<li><%= link_to s.url, "/?s_id=#{s.id}" %></li>
<% end %>
</ul>
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!
I am trying to achieve an unordered list of "Categories" in which if you click on a particular Category, all the photos(jags) that belong to that Category show on the screen. My view that includes the categories is:
<div id = "Categories">
<h2>Categories</h2>
<ul><% #cat.each do |c| %>
<li><%=link_to c.name, c,:controller => "category", :action => "show" %>
</li>
<% end %>
</ul>
my Category controller is:
def show
#jags = Jag.where("category_id = params[:id]")
if #jags.empty?
flash[:notice] = "No jags in this Category"
end
end
and lastly my show view is:
<%= render 'nav' %>
<div><% #jags.each do |j| %>
<%= image_tag j.image_url(:thumb)%>
<% end %>
</div>
The problem i am having is that I dont know how do i pass on my "particular category"(c) in the first view to the Category controller.
I tried making c an instance variable(#c) which apparently i cant do[formal argument cannot be an instance variable
'); #cat.each do |#c| ;#output_buffer.safe_concat('].
If I run this code I get an SQLlite error[SQLite3::SQLException: near "[:id]": syntax error: SELECT COUNT(*) FROM "jags" WHERE (category_id = params[:id])].
If you use RESTful controllers this should be enough:
<div id = "Categories">
<h2>Categories</h2>
<ul>
<% #cat.each do |c| %>
<li><%=link_to c.name, c %></li>
<% end %>
</ul>
Seeing as you're getting the ID for the Category in the controller, then you can just do
#category = Category.find params[:id]
in your controller. Also, clean up your link_to helper as per below.
I imagine this has a rather simple answer
<% for user in #users %>
<li>
<%= link_to user.username, user %>
</li>
<% end %>
Taking my simple example above, how would I apply a class to my <li> to the first three instances returned?
Secondly, how could I just have the the second two items have a different class from the first one? as in 1..2
Either you could count manually (which is kinda ugly):
<% i = 0 %>
<% for user in #users %>
<li class=<%= (i < 3 ? "foo" : "bar") %>>
<%= link_to user.username, user %>
</li>
<% i = i.next %>
<% end %>
or use each_with_index
<% #users.each_with_index do |user, i| %>
<li class=<%= (i < 3 ? "foo" : "bar") %>>
<%= link_to user.username, user %>
</li>
<% end %>
Once you get to more complex things than i < 3 (like your 1..2 issue) you should think about a helper_method (in helpers) like class_by_position(pos) so that you can write
<li class=<%= class_by_position(i) %>>
The :first-child pseudoselector might be a better way to go, but you'll need to have a counter variable that keeps track of the iterations to do it your way.
Your question is a little vague. I can't tell if you want to stop processing the array after the first x, or not.
If you're just looking to stop after the first x items, or just looking for the 2nd and 3rd items the solution is to use a slice.
Example: just the first 3:
#user[0,3].each do |user|
... # only executed for user = #user[0],user = #user[1] and user = #user[3]
end
Example: just the second and third:
#user[1,2].each do |user|
... #only only executed for user = #user[1] and user = #user[3]
end
And here's a more specific answer to your question using these new concepts and the content_tag to programatically decide the class of the list item. If you're going to be doing this often, makes a great candidate for a function.
<% first = true %>
<% #user[0,2].each do |user| %>
<% content_tag :li,
:class => first ? "class-for-first-item" : "class-for-2nd-&-3rd" do %>
<%= link_to user.username, user %>
<% end %>
<% first = false %>
<% end %>
<!-- Now to do the rest of them:-->
<% #user[3,-1].each do |user| %>
<% content_tag :li, :class => "class-for-rest" do %>
<%= link_to user.username, user %>
<% end %>
<% end %>