Show page failing to pass seed data along (rails) - ruby-on-rails

I have a seed database that renders Admin user information and links to individual member profile pages. The database renders images and data fine in the main page, but comes up nil <p>, with id </p> *== $0* when I try to call the individual objects on the show page.
routes.rb:
get '/team', to: 'pages#team'
get '/team/:id', to: 'pages#show', as: 'agent'
pages_controller.rb:
def team
#admins = Admin.where(role: :staff)
end
def show
#admin = Admin.find(params[:id])
end
views/pages/team.html.erb:
<%= link_to agent_path(agent.id), class:"team-link" do %>
.....
<% end %> (all working, routes stable)
views/pages/show.html.erb:
<p><% #admin.name %>, with id <% #admin.id %></p>
#rendering == $0
Where's the data connection breaking down? I've been working at this for a day or so now and this is a fairly solid wall for me.

Ah, your ERB statements are a bit off. You need to use the <%= %> format for the output of your ruby code to be shown (note the =).
So <% #admin.name %> should be <%= #admin.name %>, etc.

Related

How to add a button to delete data without using params?

Getting back into programming but I'm having trouble with this basic thing. So I've scraped products from a web site then inserted them into a DB. Then I list those products on my web site. Now I'm trying to add a delete button next to each of those product that are listed on my web site. I've tried using the solutions found on stackoverflow but I can't seem to get any of them to work. I know this is a basic question, but I appreciate the help.
Controller
class IbottaController < ApplicationController
def save
require 'watir'
require 'phantomjs'
#browser = Watir::Browser.new:phantomjs
#browser.goto "https://ibotta.com/rebates"
#button = #browser.button(class: "see-more-label")
Ibotta.delete_all
# if x = 24 then I get 492 products
# if x = 23 then I get 472 products
x = 24
y = 0
while y < x
#button.click
y+=1
end
#products = #browser.divs(class: "offer-card")
#products.each do |a|
# if Ibotta.find_by title: a.imgs[0].alt
if a.divs[2].text.split("").include?('%')
else
value_placeholder = a.divs[3].text.split(" ")
value_placeholder.delete("cash")
value_placeholder.delete("back")
value_placeholder = value_placeholder.join(" ").split("")
value_placeholder.delete("$")
value_placeholder = value_placeholder.join("")
Ibotta.create(title: a.imgs[0].alt, values: value_placeholder, store: a.divs[5].text, link: a.links[0].href)
end
end
#products = Ibotta.all
end
def show
#products = Ibotta.all
end
def delete
Ibotta.delete_all
#products = Ibotta.all
end
def practice
end
end
View
<h1>Show Page for iBotta</h1>
<h3><%= #products.length %> products in the iBotta DB</h3>
<% #products.each do |x| %>
<p>Title: <a href=<%=x.link%>><%= x.title %></a> </p>
<p>Value: <%= x.values %> </p>
<p>Store: <%= x.store %> </p>
<% end %>
If you also have advice on what code I need to add, could you mention what file to add the code in? Thanks.
Routes
Rails.application.routes.draw do
resources :articles
get 'scraper/ibotta'
get 'scraper/checkout51'
get 'ibotta/save'
get 'ibotta/show'
get 'ibotta/delete'
get 'targetcoupon/save'
get 'targetcoupon/delete'
get 'targetcoupon/show'
get 'targetibottum/delete'
get 'targetibottum/show'
get 'targetibottum/save'
get 'savingstar/delete'
get 'savingstar/save'
get 'savingstar/show'
get 'ibottasavingstar/show'
get 'ibottasavingstar/save'
get 'ibottasavingstar/delete'
get 'targetcoupon/practice'
get 'targetibottasavingstar/show'
get 'targetibottasavingstar/save'
get 'targetibottasavingstar/delete'
get 'checkout51/save'
get 'checkout51/show'
get 'checkout51/delete'
get 'checkout51/practice'
get 'ibotta/practice'
get 'ibottacheckout51/save'
get 'ibottacheckout51/show'
get 'ibottacheckout51/delete'
get 'ibottacheckout51/practice'
get 'newcheckout51/save'
get 'newcheckout51/show'
get 'newcheckout51/delete'
get 'smiths/save'
get 'smiths/show'
get 'smiths/delete'
get 'smiths/practice'
Why don't You want to use params? I do not know If It is even possible...
With ID You could simply add something like <%= link_to 'delete', ibotta_path(x.id), method: :delete %> In Your view.
If You have resources routes the path helper should be avaliable for You.
Then in controller add:
def destroy
Ibotta.find(params[:id]).destroy
redirect_to your_redirect_path
end
EDIT: I see that You are not using resources routing - add delete 'ibotta/:id', to: 'ibotta#destroy' to Your routes.rb or just use resources routing
So Your view would look like:
<% #products.each do |x| %>
<p>Title: <a href=<%=x.link%>><%= x.title %></a> </p>
<p>Value: <%= x.values %> </p>
<p>Store: <%= x.store %> </p>
<p><%= link_to 'delete', ibotta_path(x.id), method: :delete %></p>
<% end %>
One note - I think You shouldn't use variable names like 'x' in each block, use 'product' instead, it is much more descriptive.

Rails - If / Else in view returning both branches

I've an 'if / else' running in my view, based on the current URL of the page, and currently the view is displaying what is should were the 'if' both true and false. It's a little tricky to explain, and I've no idea why this is happening - any explanations / solutions will be greatly appreciated!
Before the code, here's a little background:
I have recipes, each of which have one or more cuisines (via has-many-through relationships)
if the URL is, for example, /italian, I want it to display all recipes with the cuisine 'Italian'
otherwise, if the URL is invalid or doesn't have any recipes with matching cuisines, I want it to display a message stating this
(So far, so straightforward right?)
However, when the code runs, it's correctly printing the right recipes (i.e. French meals won't come up on the /italian url), BUT also printing the error message. Here's the code:
In the controller:
#url = request.path.split('/')[2] #returning 'italian', 'french', etc.
And the view:
<% Recipe.all.each do |recipe| %>
<% recipe.cuisines.each do |recipe_cuisine| %>
<% if recipe_cuisine.name.downcase == #url %>
<p><strong><%= recipe.name.humanize %></strong></p>
<ul>
<% recipe.ingredients.each do |recipe_ingredient| %>
<li><%= recipe_ingredient.name %></li>
<% end %>
</ul>
<p><%= recipe.method %></p>
<% else %>
<p>You've reached an invalid page, please return to <#%= link_to 'the homepage', root_url %></p>
<% end %>
<% end %>
<% end %>
To clarify, I've tested the 'recipe_cuisine.name.downcase == #url' line of code, and it's returning true when it should be, false when it shouldn't.
Does anyone know how to resolve this?
Thanks in advance, Steve.
Edit
Here are the routes that affect this:
Rails.application.routes.draw do
get 'recipes/:cuisine' => 'recipes#cuisine'
resources :recipes
end
You defined the following route:
get 'recipes/:cuisine' => 'recipes#cuisine'
This means when you hit /recipes, it uses the cuisine action of the recipes controller (thanks to 'recipes#cuisine').
You also defined an extra :cuisine after the recipes/, which means if you hit /recipes/italian, then you will have a GET param (named cuisine) available in your controller/view.
Here is how you can use it:
# recipes_controller.rb
def cuisine
#recipes = Recipe.all # (use `Recipe.scoped` if using Rails' version < 4)
if params[:cuisine].present?
#recipes = #recipes.includes(:cuisines).where(cuisines: { name: params[:cuisine] })
end
# other stuff
end
# cuisine.html.erb (view)
<% #recipes.each do |recipe| %>
<p><strong><%= recipe.name.humanize %></strong></p>
<ul>
<% recipe.ingredients.each do |recipe_ingredient| %>
<li><%= recipe_ingredient.name %></li>
<% end %>
</ul>
<p><%= recipe.method %></p>
<% end %>
But there is a flaw in this logic: What if I hit /recipes/frenchAndMexicanPlease ? The params[:cusine] will be equal to "frenchAndMexicanPlease", and your DB does not have any cuisine type named like this. In this case, it would display no recipe at all, since the query #recipes.includes(:cuisines).where(cuisines: { name: params[:cuisine] }) would not match any existing record.
I can obviously provide more explanations about the code and logic I used. Hope this helps!
How many cuisines are in the collection? If there are two, and one of them has a name that is equal to #url then you would see the first branch, while any that don't equal #url would show the second branch. You're evaluating that if statement for each cuisine.

form_for save input values to session variables

I am trying to create a compare functionality for an index of schools. Currently I am using the following code which takes any checked school and adds it to the school_ids[] in the params.
In my gemfile:
gem 'will_paginate'
In my school's index.html.erb:
<%= form_tag compare_path, :method => 'get' do %>
<%= submit_tag "Compare" %>
<ul>
<% #schools.each do |school| %>
<li>
<%= check_box_tag'school_ids[]', school.id %>
<%= link_to school.name, school %><br>
<%= school.city %>, <%= school.state %>
</li>
<% end %>
</ul>
<% end %>
In my school controller I have:
def compare
#schools = School.find(params[:school_ids])
end
This works great as long as all of the check schools are on the same page. But since I'm using will_paginate to paginate the list of schools, if I change pages, the check boxes do not persist. I'm assuming I need to save to sessions somehow.
Do you mean you want to be able to add a check mark to a school A on page 1 of the index, go to page 2 of the index and add another check mark for school B, then submit the compare form and see schools A and B? If that's the case, then you're correct, you need to get the check boxes into the session. Attach a js click event, like
$('.checkbox_class').click(function(){
$.post('update_session_method', { school_id: $(this).val(), checked: $(this).is(:checked)]);
});
then add a controller method
def update_session_method
session[:school_ids] ||= []
if params[:checked]
session[:school_ids] << params[:school_id]
else
session[:school_ids].delete(params[:school_id])
end
end
then your compare method
def compare
#schools = School.find(params[:school_ids].merge(session[:school_ids] || []))
end

Rails AJAX Search form reloading page

New to AJAX and search. I feel like I'm an inch away on this one, but I'm not sure what to fix. Also, my controller looks really hacky to me right now.
At any rate, I'm trying to create a search that allows users to search through blog posts on my page using AJAX. Here are the (relevant parts of the) parts:
posts_controller.rb
def show
#posts = Post.all.reverse
#post = Post.find(params[:id])
#link_num = 10
respond_to do |format|
format.html # show.html.erb
format.json { redirect_to #post }
end
end
def search
#link_num = 10
#posts = Post.all.reverse
#post = Post.find(params[:id])
#The including function returns the search results
#search = Post.first.including(params[:term])
render 'show'
end
What strikes me as "hacky" here is that I repeat all the variable assignments (there are others I didn't show cause they're not relevant). Shouldn't an AJAX call ideally not have to redefine/reload all these variables? Also, I have to pass :id to my search action through a hidden field. This feels weird/wrong to me.
show.html.erb
<h1 class="squeeze">Recent Posts</h1>
<%= form_tag("/search", method: "get", class: "search") do %>
<%= text_field_tag(:term, '', placeholder: "Search posts:") %>
<%= hidden_field_tag(:id, #post.id) %>
<%= submit_tag("Search", class: "btn search_button", remote: true) %>
<% end %>
<% if !#search%>
<ul>
<% #posts.first(#link_num).each do |p| %>
<li>(<%= p.created_at.strftime("%b %d, %Y") %>)</span></li>
<% end %>
<% if #posts.length > #link_num %>
<div class="link_disclaimer">
<h4>---</h4>
<h5><%= "Only showing #{#link_num} most recent posts." %></h5>
<h5>Search to narrow results.</h5>
</div>
<% end %>
</ul>
<% elsif #search.empty? %>
<h3>Term not found!</h3>
<% else %>
<ul>
<% #search.first(#link_num).each do |p| %>
<li>(<%= p.created_at.strftime("%b %d, %Y") %>)</span></li>
<% end %>
<% if #search.length > #link_num %>
<div class="link_disclaimer">
<h4>---</h4>
<h5><%= "Only showing first #{#link_num} relevant hits." %></h5>
<h5>Narrow search for more specific results.</h5>
</div>
<% end %>
</ul>
<% end %>
routes.rb
match '/search', to: 'posts#search'
Currently, the search itself works fine, with three major problems:
The aforementioned messiness of my Controller.
The fact that the whole page reloads. Isn't that the opposite of what AJAX is supposed to do? How can I get it to reload just the list of links?
The URL. It's super messy (i.e "/search?utf8=✓&term=limits&id=11&commit=Search"). I'd ideally have it remain untouched by the search, but if that's not possible, maybe just something like "/search?=". How's that done?
Based on the comment here is basic logic to make the function work(Sorry for no real code as that is too time consuming)
In controller you make a method say "search". The method need an argument which is the phrase to search. It gets the result from db and respond to JSON only with the result.
In template you have a search form.
In JS, you listen the event of user clicking the submit button, catch the characters they write, and handle it to ajax function.
Write Ajax code, preferred using jQuery's ajax(). Basically use ajax() to send the typed characters to controller method in #1 as argument. Then ajax() will get the response(the search result) from server.
Update the result in DOM. You can either add a new div to show search result, or replace current main content div to show result.

How do I setup the views for nested resources that are displayed as tabs within their parent resource in Rails?

I have a resource Company that has many Projects and People. The index action for the companies displays each company in a table. The show action shows some additional information about it, and then what I'd like to show is another table, with tabs "Projects" and "People".
When I click on the "People" tab, I should go to URL "companies/:id/people", and likewise for the "Projects" tab. I'm not worried about AJAX or pre-loading all of this information into the #company variable during the show action. A simple nested resource is fine.
Now when I'm at "companies/:id/people", it will use PeopleController#index, but I want to show that view (which is JUST it's table, I suppose?) nested within the company's show view. So that when I switch between "companies/:id/people" and "companies/:id/projects", the only thing changing is the table, not the company information around the outside.
Is this sort of thing easily do-able? If Rails isn't build to handle this sort of thing easily, I don't mind using something else. I just don't have much experience with the view layer, so I don't know much about it since I primarily work with JSON.
Basic Example:
ProjectsController && PeopleController:
layout :current_layout
def current_layout
if #company && #company.persisted? && request.path_parameters[:action] == "index" # i prefer helper 'current_action'
"company"
else
"application"
end
end
Helper:
def parent_layout(layout)
#view_flow.set(:layout, self.output_buffer)
self.output_buffer = render(:file => "layouts/#{layout}")
end
Company layout:
#views/layouts/company.html.erb
<h1><%= #company %></h1>
<ul class="tabs">
<li>Info</li>
<li>Info</li>
<li>Info</li>
</ul>
<%= yield %>
<%= parent_layout(:application) %>
People template:
# views/people/index.html.erb
<% if current_layout == "company" %> # just table
<%= render "people_table" %>
<% else %>
<h1>People controller</h3>
<%= render #people %>
<% end %>
Projects template:
# views/projects/index.html.erb
<% if current_layout == "company" %> # just table
<%= render "projects_table" %>
<% else %>
<h1>Projects controller</h3>
<%= render #projects %>
<% end %>
I suggest you take a look at the rails guides, specifically routing from the outside in.
However rails can handle this, your route would be setup in the following way:
resources :company do
resource :projects
resource :people
end
I assume you already have all your CRUD actions setup, then this would work. However do note, it will change your named routes.
i.e
if you were calling
projects_path
in your views, they will now become:
company_projects_path
You can see a full list of routes with the command
rake routes

Resources