Delete keeps going to the Show page on rails application - ruby-on-rails

I'm trying to create a blog in rails, and I'm having a little trouble. I keep getting the page for my show method each time I try to use my destroy method to test deleting an article from my blog. This is the destroy method:
def destroy
#article = Article.find(params[:id])
#article.destroy
flash[:notice] = "Article has been deleted."
redirect_to articles_path
end
And this is the index file for the home page:
<h1>Listing all articles</h1>
<p>
<%= link_to "Create new artilce", new_article_path %>
</p>
<table>
<tr>
<th>Titile</th>
<th>Text</th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>
</tr>
<% end %>
</table>
If someone could please tell me where the faulty code is, I would greatly appreciate it.

Almost certainly a javascript / jquery issue.
Rails sets the "method" with a form, using JQuery to define it explicitly:
This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site). Supported verbs are :post, :delete, :patch, and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET
Thus, if you have Javascript / JQuery disabled, your link_to will simply be GET, which sends the request to the show action.
--
You need to make sure you have the following:
#app/views/layouts/application.html.erb
<%= javascript_include_tag :application %>
#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs

Related

Ruby on Rails- Destroy record using link

I want to delete a record that is stored in a table using a link right next to the table data. The error I come up with is:
No route matches [GET] "/carlogs/destroy"
My destroy method:
def destroy
#carlog= CarLog.find(params[:id])
#carlog.destroy()
redirect_to show_path(#carlog)
end
Part of the view code containing the Delete link:
<% #carlogs.each do |car| %>
<tr>
<td><%= car.id %></td>
<td><%= car.plate_number %></td>
<td><%= car.brand %></td>
<td><%= car.slot_number %></td>
<td><%= car.is_taken %></td>
<td><%= car.created_at %></td>
<td><%= link_to "Delete", show_path(car), method: :delete, data:
{confirm: "Are you sure?"} %>
</tr>
<% end %>
Make sure that you have delete REST method as:
DELETE /carlogs/:id(.:format) carlogs#destroy
And in your application.js you must have this line:
//= require jquery_ujs
Use destroy link for destroy record then redirect table like
method
def destroy
#carlog= CarLog.find(params[:id])
#carlog.destroy()
redirect_to show_path(#carlog) #-> Table pathe
end
routes.rb
delete 'destroy' => 'carlogs#destroy'
View
<%= link_to "Delete", destroy_path, method: :delete, data:
{confirm: "Are you sure?"} %>
I think will help you
Why did you write show_path(car)?
Maybe you mean car_path(car) ?
<%= link_to "Delete", car_path(car), method: :delete, data:
{confirm: "Are you sure?"}%>
Also you should check your route [GET] "/carlogs/destroy.
I think this don't present in rake router

How to delete association with parent controller update action? Rails

I'm using Rails 4
I need to delete association Box with link_to method. The problem is that I have to do it from parrent controller and method: patch. my corrent code is doing nothing because I don't know how to use data: for link_to.
#views/modifications/show.html.erb
<% #modification.boxes.each do |box| %>
<tr>
<td><%= box.name %></td>
<td><%= link_to "delete", #modification, remote: true, method: :patch %></td>
</tr>
<% end %>
By the way, this is used with Ajax so page doesn't need to be reloaded.
I have to do it
You don't have to do anything in any way - if Microsoft can release Windows for all the PC's in the world, I'm sure you can get this working.
You have several issues, the most important of which being... how do you identify the box object to delete?
The whole point of nested resources (which is what you need) is to give you the ability to identify a "parent" object and a child object.
Your current setup prevents you from identifying the box you wish to remove. Ideally, you should use the following code to get it sorted:
#config/routes.rb
resources :modifications do
resources :boxes, only: :destroy
end
#app/views/modifications/show.html.erb
<% #modification.boxes.each do |box| %>
<tr>
<td><%= box.name %></td>
<td><%= link_to "delete", [#modification, box], remote: true, method: :delete %></td>
</tr>
<% end %>
#app/controllers/boxes_controller.rb
class BoxesController < ApplicationController
def destroy
#modification = Modification.find params[:modification_id]
#box = #modification.boxes.find params[:id]
#box.destroy
end
end
Try to using link_to with delete method on box destroy path, like this:
<%= link_to 'Delete', box_path(box), method: :delete, remote: true %>

Calling and passing objects to a custom method in the controller

I have, what I have to imagine is a very simple question about calling custom controller methods and passing data to them.
I've added a custom method dprocess to the controller for the purposes of acting on post objects that I have listed in my index, to be called with some sort of button. The method dprocess does not have any view but simply processes the data in post and saves the result to a file in /tmp/. The problem I'm having is that I can't figure out how to pass the data to this custom method.
So Here's my index.html.erb
<% #post.each do |post| %>
<tr>
<td class="Name"><%= link_to post.name, post %></td>
<td class="created_at"><%= link_to "Edit Post", edit_post_path(post) %></td>
<td class="generate"><%= button_to "Process Post", ........ %></td>
</tr>
<% end %>
Note the "........" in button_to; the missing contents are what I'm struggling to figure out.
My question is: How can I pass post to this custom method in the controller?
Many thanks for ANY guidance; this problem has been killing me.
You can use the special attribute remote on link_to, button_to methods to indicate that you want to perform an AJAX operation.
Assuming your routes look like:
resources :posts do
member do
# note: the post here means a HTTP POST request is necessary, if you want
# a HTTP GET request, change it to get
post 'dprocess'
end
end
So the url will look like /posts/:id/dprocess
Then you can perform an AJAX request when clicking the button like this, please
make sure you change the method attribute to either :post or :get depending on how you have configured your route.
<% #post.each do |post| %>
<tr>
<td class="Name"><%= link_to post.name, post %></td>
<td class="created_at"><%= link_to "Edit Post", edit_post_path(post) %></td>
<td class="generate"><%= button_to "Process Post", dprocess_post_path(post), :remote => true, :method => :post %></td>
</tr>
<% end %>
This will call your controller action dprocess using a POST request:
def dprocess
# find object
#post = Post.find(params[:id])
# ... perform processing
# render nothing
render :nothing => true
end
Having a look at the documentation is usually a good guidance:
button_to documentation, see "Options" sections
Rails Guide on AJAX helpers

Unable to find "Destroy" link

I am trying to write test scenario for delete,but i don't understand why it is not getting destroy link.
Here my test scenario:
Scenario: User can delete kids
Given I am on the kids page
When I Destroy kid
Then I should see "Kid deleted successfully"
Then one kid should not exist
<h1>Listing kids</h1>
<tr>
<th>Kid name</th>
<th colspan=3>Action</th>
</tr>
<% #kids.each do |kid| %>
<tr>
<td><%= kid.kid_name %></td>
<td><%= link_to 'Show', kid %></td>
<td><%= link_to 'Edit', edit_kid_path(kid) %></td>
<td><%= link_to 'Destroy', kid, method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%= link_to 'New Kid', new_kid_path %>
My step defination for link:
When /^I Destroy kid$/ do |link|
click_link(link)
end
Please somebody suggest step definition for delete link, please correct me if their is some error in my scenario.
Thank you.
I have found the answer for destroy link. It won't support webrat so i have removed webrat and used capybara and my issue get solved.
Here is the step:
Scenario: Delete Kid
Given I am on the kids page
And there is a kid with kid_name "john"
When I destroy that kid
Then I am on the kids page
step_defination:
When /^I destroy that (.*)$/ do |element_type|
element = element_type.classify.constantize.last
path = "#{element_type}_path"
case page.driver
when Capybara::RackTest::Driver
page.driver.submit :delete, send(path, element), {}
else
visit send(path, element, { method: :delete })
end
end </code>

Returning data from database to another page in rails

How can I return data from the database on another page?
I can file this under: views / posts / index.htm.erb
<h1>Listing posts</h1>
<table>
<tr>
<th>Titulo</th>
<th>Conteudo</th>
<th>Categoria</th>
<th>Criado em</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.titulo %></td>
<td><%= post.conteudo %></td>
<td><%= post.category.name %></td>
<td><%= post.created_at.strftime("%d/%m/%Y") %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Delete', post, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Post', new_post_path %>
And I wanted to display these values ​​on another page: views / home / blog.html.erb
How do I do this? Could you explain all the steps so that I can display these values ​​in my other page.
Thanks Kocur4d
but how do I get only some information? eg: I would like that to appear in the title of the post page like this way (in blog.html.erb):
<div class="post-title">
<h2 class="title"> <a href="link_to_post"> **<% = post.titulo%>** </ a> </ h2>
</ div>
Step 1. Create controller
In your app root directory run:
rails g controller home blog
Modify controllers/homes_controller.rb :
class HomesController < ApplicationController
def blog
#posts = Post.all
end
end
Your controllers/posts_controller.rb should be already set up. Minimum what you need for your question is to have index method defined you might have other methods as well:
class PostsController < ApplicationController
def index
#posts = Post.all
end
end
Step 2. Extract Partial
change views/posts/index.htm.erb :
<h1>Listing posts</h1>
<%= render partial: 'shared/posts', object: #posts %>
<%= link_to 'New Post', new_post_path %>
create/modify views/home/blog.html.erb :
<h1>Listing posts</h1>
<%= render partial: 'shared/posts', object: #posts %>
<%= link_to 'New Post', new_post_path %>
create views/shared/_posts.html.erb :
<table>
<tr>
<th>Titulo</th>
<th>Conteudo</th>
<th>Categoria</th>
<th>Criado em</th>
<th></th>
<th></th>
<th></th>
</tr>
<% posts.each do |post| %>
<tr>
<td><%= post.titulo %></td>
<td><%= post.conteudo %></td>
<td><%= post.category.name %></td>
<td><%= post.created_at.strftime("%d/%m/%Y") %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Delete', post, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
Step 3. Set up routes.
You should have something like this in your routes.rb file:
resources :posts or match 'posts/index' => 'posts#index'
add this to config/routes.rb:
match 'home/blog' => 'home#blog'
so it might look like this(there is few variants):
config/routes.rb:
YourAppName::Application.routes.draw do
root to: 'posts#index'
resources :posts
match 'home/blog' => 'home#blog'
end
Now when you start rails server(assuming standard configuration) and visit:
127.0.0.1:3000/posts/index and 127.0.0.1:3000/home/blog
you should see same content.
This should work copy-and-paste but I could make some typos and other small mistakes(hope not, if ill find any ill try to edit them asap). In general look at it as you need 3 steps to forward http request down your rails application stack.
Map url to controllers using routes.
Create controllers and inside prepare data for views.
Display data in Views.
Look around in Rails Guides, Rails for Zombies and Rails Tutorial for more info.
---------Upadate to your second question-----------
I don't really understand what would you like to achieve?? At the moment both index.html.erb and blog.html.erb showing the same data, that was what you ware asking for?
post representing one post and is available in sharde/_posts.html.erb. You can't reference it from index.html.erb or blog.html.erb.
#posts represents all the posts and its available in index.html.erb or blog.html.erb.
render partial: 'shared/posts', object: #posts -- this line say "Hey man! Paste here content of shared/posts file, and btw I have here a local variable #posts so if you need to use that date in shared/posts file Ill name it posts from in side there"
To make them look different modify both files and part that will be identical for both of them is in a sharde/_posts.html.erb.
Try for example remove this line:
<td><%= post.category.name %></td>
from shared file to see what is going to happen.
Add some html tags and thinker with it.
Rails has may helper methods available' to find out about them check the links I give you and google, google, google.
Try to add some links with link_to helper
In your home controller, for the blog method, set #posts as you need...
Maybe
#posts = Post.all

Resources