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!
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>
In my view, I am doing this:
<% case #post
when #post.has_children? %>
<% #post.children.each do |child| %>
<li><%= link_to child.title, post_path(child)%></li>
<% end %>
<% when #post.has_siblings? %>
<% #post.siblings.where.not(id: #post.id).each do |sibling| %>
<li><%= link_to sibling.title, post_path(sibling)%></li>
<% end %>
<% when !#post.parent.nil? %>
<li><%= link_to #post.parent.title, post_path(#post.parent) %></li>
<% else %>
</ul>
</p>
<p>
There are no related posts.
</p>
<% end %>
Basically what I want to do is I want to check #post for a variety of conditions. If it has_children?, if it has_siblings?, etc.
I don't want the statement to exit if any of the above is true or false.
Once the view loads, it should automatically check for all of these statements. If it finds any of the above true, it should execute the command right below the check.
The issue is when I do this, it always defaults to the else. i.e. the case statement doesn't work.
I know I could simply just do a bunch of disjointed if statements, but then the HTML around it gets a bit weird.
Is there a way I can do this with a CASE statement?
Edit 1
The reason the if statement doesn't work properly, is if I have 3 if statements back to back - none of which that interact with each other (that's the only way to cycle through all of the conditions properly), is that the else doesn't trigger properly.
E.g. if the first two conditions are true, but the third is not...it will print out "there are no related posts"...when that's not the case. It is the case that there are no parent posts.
Basically I just want to have a catch-all related posts, so I am simply iterating through all of the various options and checking to see if those relations exist. If they do, I am pulling them out and if they don't then they move on. If none exist, then I don't print "there are no related posts".
The fact that the view is already looking looking complex is a sign that it may be a good idea to refactor the logic out of the view and place it into the Post model where it belongs. Ideally the view(s) should end up looking like this:
<%# posts/show.html.erb %>
<% if #post.has_related_posts? %>
<%= render partial: 'children', collection: #post.children, as: :child %>
<%= render partial: 'siblings', collection: #post.other_siblings, as: :sibling %>
<%= render partial: 'parent', locals: {parent: #post.parent}%>
<% else %>
<p>There are no related posts</p>
<% end %>
The paritals:
<%# posts/_children.html.erb %>
<li><%= link_to child.title, post_path(child)%></li>
<%# posts/_sibling.html.erb %>
<li><%= link_to sibling.title, post_path(sibling)%></li>
<%# posts/_parent.html.erb %>
<% unless parent.nil? %>
<li><%= link_to parent.title, post_path(parent) %></li>
<% end %>
Then the Post model can organize the logic:
class Post < ActiveRecord::Base
def has_related_posts?
!children.to_a.empty? || !other_siblings.to_a.empty? || !parent.nil?
end
def children
self.children || [] # Rails does this automatically, but just for the example
end
def other_siblings
self.siblings.where.not(id: self.id)
end
#...
end
I know this doesn't directly answer your question, however IMHO I think it's a better solution.
You have two options here.
Use IF ELSIF
<% if #post.has_children? %>
<% #post.children.each do |child| %>
<li><%= link_to child.title, post_path(child)%></li>
<% end %>
<% elsif #post.has_siblings? %>
<% #post.siblings.where.not(id: #post.id).each do |sibling| %>
<li><%= link_to sibling.title, post_path(sibling)%></li>
<% end %>
<% elsif !#post.parent.nil? %>
<li><%= link_to #post.parent.title, post_path(#post.parent) %></li>
<% else %>
</ul>
</p>
<p>
There are no related posts.
</p>
<% end %>
Use only case as doz mentioned
<% case
when #post.has_children? %>
<% #post.children.each do |child| %>
<li><%= link_to child.title, post_path(child)%></li>
<% end %>
<% when #post.has_siblings? %>
<% #post.siblings.where.not(id: #post.id).each do |sibling| %>
<li><%= link_to sibling.title, post_path(sibling)%></li>
<% end %>
<% when !#post.parent.nil? %>
<li><%= link_to #post.parent.title, post_path(#post.parent) %></li>
<% else %>
</ul>
</p>
<p>
There are no related posts.
</p>
<% end %>
You can do it with a case statement. Just give case no parameter. Eg instead of case #post just use case
This is the equivalent of an if statement. And should work for what your trying to achieve
Check out ruby case statements for some examples
Having a Bit of trouble displaying unique results from my database. I have a database called "Activities". Each Activity has an associated Sport through sport_id. There may be many activities with the same sport_id.
I want to display a list of all sports linked to the activities database without displaying (for example "Football") twice.
FYI : Venues have many Facilities and Facilities have many Activities.
Controller:
#sports = Sport.all
#activities = Activity.paginate(page: params[:page])
#facilities = Facility.where(venue_id: #venue.id)
View:
<% #facilities.each do |f| %>
<% #activities.find(:all, :conditions => "facility_id == #{f.id} ").each do |a| %>
<li><%= Sport.find(a.sport_id).name %>, (<%= a.facility_id %>)</li>
<% end %>
<% end %>
This shows:
Football, (2)
Hockey, (2)
Hockey, (2)
Football, (5)
I would like to display just:
Football
Hockey
Any ideas?
A simple solution would be to reduce your array with ruby in the view using: uniq!
<% #facilities.each do |f| %>
<% #activities.find(:all, :conditions => "facility_id == #{f.id} ").uniq! { |a| a.sport_id }.each do |a| %>
<li><%= link_to Sport.find(a.sport_id).name, Sport.find(a.sport_id) %></li>
<% end %>
<% end %>
Another way may be to perform a single query on your DB since Sport what you want to narrow down
In controller:
#sports = Sport.joins(activities: [facility: :venue]).where(facilities: { venue_id: #venue.id }).distinct
In view:
<% #sports.each do |sport| %>
<li><%= link_to sport.name, sport %></li>
<% end %>
I am not sure about your DB schema so I went with what I thought you would have done, but it might needs some tweakings.
I hope I helped you.
try to use reject before each
<% #facilities.reject{your condition here}.each do |f| %>
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>
When performing a block like:
<% #user.favoured_user.each do |user| %>
<li><%= user.name %></li>
<% end %>
With the favoured_user method returning a limit of 5 users, how would I manipulate the block so that even when there are only 3 users available, I could still return 5 li elements?
I'm guessing a helper would come in to play, and maybe the 'first, second, third, etc.' array methods, but I can't think how to write it.
Any help?
You can try this,
<% 5.times do |i| %>
<li> <%= #user.favoured_user[i].try(:name) %> </li>
<% end %>
You can use in_groups_of
Like:
<% #user.favoured_user.in_groups_of(5).each do |favored_user| %>
<% favored_user.each do |user| %>
<li><%= user.try(:name) %></li>
<% end %>
<% end %>
The first 3 users will come through, and the last two entries will be nil