How to add a button to delete data without using params? - ruby-on-rails

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.

Related

Show page failing to pass seed data along (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.

How to pass an id in URL to controller? Rails

I want to deal with some data rows with same key_id. I want them to get select by using an id in URL, so I will have /field_data/index/12 and 12 here means the key_id.
I can manually do it in the controller by hardcoding the key_id
def index
#key_id = 12
#field_data = FieldDatum.select('field_content').where("key_id = ?", #key_id)
end
and my URL is very general: /field_data
So how to make it be able to get the id in URL?
routes
get "/field_data/index/:key_id", to: "field_datas#index"
controller
def index
#field_data = FieldDatum.select('field_content').where(key_id: params[:key_id])
end
Try this:
Show.html.erb
<% user_all.each do |user| %>
<%= link_to user_id_pass_path(user.id), method: :get do %>
<span class="btn btn-warning">Pass Id</span>
<% end %>
<% end %>
routes.rb
get '/user_id_pass/:id' => 'users#user_id_pass', as: 'user_id_pass'
users_controller.rb
def user_id_pass
#user = User.find_by_id(params[:id])
end

Rails route to a resource

I'm building a Rails app that is a podcast directory. I have Podcasts and Episodes. Episode belongs to Podcast and Podcast has many Episodes. On the home page I want to show the last 5 episodes that have been created and link to them.
I have it working with this, though this is obviously not the way to do it:
<% #episodes.each do |episode| %>
<%# link_to episode do %>
<%= episode.title %>
<%# end %>
<% end %>
The link_to is commented out because that's part of my problem.
Here is the index controller:
def index
#podcasts = Podcast.where.not(thumbnail_file_name: nil).reverse.last(5)
#episodes = Episode.where.not(episode_thumbnail_file_name: nil).reverse.last(5)
end
Here is the routes file:
Rails.application.routes.draw do
devise_for :podcasts
resources :podcasts, only: [:index, :show] do
resources :episodes
end
authenticated :podcast do
root 'podcasts#dashboard', as: "authenticated_root"
end
root 'welcome#index'
end
Results of rake routes | grep episode:
podcast_episodes GET /podcasts/:podcast_id/episodes(.:format) episodes#index
POST /podcasts/:podcast_id/episodes(.:format) episodes#create
new_podcast_episode GET /podcasts/:podcast_id/episodes/new(.:format) episodes#new
edit_podcast_episode GET /podcasts/:podcast_id/episodes/:id/edit(.:format) episodes#edit
podcast_episode GET /podcasts/:podcast_id/episodes/:id(.:format) episodes#show
PATCH /podcasts/:podcast_id/episodes/:id(.:format) episodes#update
PUT /podcasts/:podcast_id/episodes/:id(.:format) episodes#update
DELETE /podcasts/:podcast_id/episodes/:id(.:format) episodes#destroy
How can I correctly create a text link of the title using a link_to that links directly to the episode? Thanks!
When you're using link_to with a block, the only thing you need to pass into the block is the text of the link, so you should be able to do this (assuming your routes are setup properly):
<% #episodes.each do |episode| %>
<%= link_to episode, class="tt-case-title c-h5" do %>
<%= episode.title %>
<% end %>
<% end %>
UPDATE
You really don't even need to use a block here. This should work fine for you and be a little more succinct.
<% #episodes.each do |episode| %>
<%= link_to episode.title, episode, class="tt-case-title c-h5" %>
<% end %>
UPDATE #2
Thanks for including your route info. Try this:
<% #episodes.each do |episode| %>
<%= link_to episode.title, podcast_episode_path(episode.podcast, episode), class="tt-case-title c-h5" %>
<% end %>

Rails 4, params not received by controller

I'm trying to setup a post editing page, with links to edit and delete posts. I'm having troubles because my edit controller somehow doesn't seem to receive the parameters I'm sending. I've printed them out on the page to make sure they're there, and it displays correctly, but when I click the editlink I get ActiveRecord::RecordNotFound Couldn't find Article with 'id'= .
here's the view with the link: (article.id display correctly on this one)
<div id= "articles-index">
<div class = "row">
<% #article.each do |article| %>
<div class="row">
<div class = "container">
<p><%= link_to article.title, article %> | <%= article.updated_at.strftime("%e. %b %Y, %H:%M") %> | <%= link_to 'edit', articles_update_path(article.id) %> | delete</p>
<h2><%= article.id %></h2>
</div>
</div>
<% end %>
</div>
</div>
articles controller:(the ... is where I edited more code out as it's irrelevant to this question)
class ArticlesController < ApplicationController
...
def update
#article = Article.find(params[:id])
end
def manage
#article = current_user.articles
#article = current_user.articles.order('id DESC')
end
...
def article_params
params.require(:article).permit(:id, :title, :body, :user_id)
end
end
routes:
get 'articles/edit'
get 'articles/destroy'
get 'articles/manage'
get 'articles/show'
get 'articles/index'
get 'articles/new'
get 'articles/update'
get 'articles/search'
get 'articles/create'
get 'sessions/destroy'
get 'sessions/new'
get 'users/new'
resources :users
resources :sessions
resources :articles
GitHub Link
Clicking on a link instigates a GET request. I'm guessing the update action on your controller will be expecting the params to be a POST request.
The edit action normally handles GET.
The update action receives the POST request.
I was trying to find the DHH 2007 Keynote at Railsconf that covers this really well. If you can find it, watch it.
See CRUD Verbs and Actions at Rails Guides
UPDATE: Just seen your routes file. If it's a GET request then just put the URL directly into the browser. e.g.
articles/update?id=123
See if params[:id] == 123 in the controller. It should be.
UPDATE 2
articles_update_path(id: article.id)
Will generate a params has with an :id key but fundamentally you shouldn't be calling update actions with a GET route.

Rails Link_to with Loop - RESTful routes

A question that I hope you can answer for a Q&A app. Still very new with Rails. It should be fairly simple but there is a small issue that I run into whenever I am trying to create a list of links to show up that point to a RESTful route from a loop.
Here's the code for the controller:
users_controller.rb
def list
#user = User.find(params[:id])
#users = User.find(:all, :select => :name)
end
def quiz
#user = User.find(params[:id])
#users = User.find(:all, :select => :name)
end
Here's the code for the view:
<% if current_user.admin? %>
<ul>
<%- #users.each do |link| %>
<li><%= link_to link.name, quiz_user_path(#user, #quiz) %></li>
<% end %>
</ul>
<% end %>
Here's the code for the route:
resources :users do
get 'quiz', :on => :member
end
What I want to do is generate individual links based on the name of the users and then link to the quiz page for that specific user. I'm pretty sure that something needs to be changed for the code in my view. Right now, all I'm getting is a link that all points to the current user which in this case is user 4. (http://localhost:3000/users/4/quiz)
Thanks for any quick tips to solve this.
Currently you are using the same #user instance variable for each link. Instead you need to use the variable set in your loop. The code below should work as expected:
<% if current_user.admin? %>
<ul>
<%- #users.each do |user| %>
<li><%= link_to user.name, quiz_user_path(user) %></li>
<% end %>
</ul>
<% end %>
I also renamed the variable from link to user, for clarity, since your iterating through users not links.
It's because in your current route map quiz_user_path needs only one variable. Try to remove #quiz.

Resources