0

I'm building a rails app based off of the railsguide

 http://guides.rubyonrails.org/getting_started.html

The syntax it calls for in the erb is...

<td><%= link_to 'Destroy', article_path(article),
          method: :delete,
          data: { confirm: 'Are you sure?' } %></td>

For some reason this reads as

https://localhost:3000/articles/[#]

where # is the given record to delete. In other words, I want to delete a record, and it interprets my code as showing said record.

What could I be doing wrong?

more info

This is what is generated dynamically

<td>
  <a rel="nofollow" data-method="delete" href="/articles/2">Destroy</a>
</td>

application.html.erb has the following...

 <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
 <%= csrf_meta_tags %>

controller definition

def destroy
@article= Article.find(params[:id])
@article.destroy

redirect_to articles_path
end

Error associated with using 'application' in application.html.erb

ExecJS::ProgramError in Welcome#index

Showing E:/scabase/app/views/layouts/application.html.erb where line #6     raised:

TypeError: Object doesn't support this property or method
(in C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/turbolinks-  2.5.3/lib/assets/javascripts/turbolinks.js.coffee)
TangibleDream
  • 601
  • 8
  • 29
  • Your code looks fine. It should be sending a ```DELETE``` HTTP request to ```https://localhost:3000/articles/:id```. Are you getting an error when you click the link? We'll need a bit more information to help debug. – Alex S. Mar 28 '15 at 05:26
  • Thanks for the additional info -- I'm still a bit confused on what actually happens when you click the link? Are you taken back to the page you were previously on or are you given an error? You can also check the logs in the terminal where ```rails server``` is running and see if you can find where the request was routed to the destroy method of the ArticlesController. – Alex S. Mar 28 '15 at 05:47

3 Answers3

2

Most probably, Rails default javascripts are not loaded - specifically jquery_ujs. This javascript allows Rails to interpret DELETE calls, which are not totally browser compliant.

The javascript you load is named 'default'. Rails standard is 'application'. Check if you have a 'default.js' inside assets/javascripts and what it contains. If you want Rails out of the box behaviour, just move to:

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
 <%= csrf_meta_tags %>

This should work as long as you did not change the application.js file.

Martin
  • 7,634
  • 1
  • 20
  • 23
  • When I initially build the rails, I have application as the tag in application.html.erb. This draws the error added in the main question. Another stackoverflow recommends the switch to default, to fix the issue – – TangibleDream Mar 31 '15 at 14:07
1

This is completely normal. In the REST world a request uses a combination of a verb (GET, DELETE etc) and a URI that identifies the resource. If you are doing different things to the same resource (i.e. the same article) then only the verb changes. The show page for an object uses GET, the update action uses PATCH (PUT prior to rails 4.1) and destroying a record uses DELETE

In practice links always result in GET requests and even forms only allow GET or POST, so rails emulates the remaining methods - clicking on a link with a data-method=destroy attributes creates a form that posts to the URL with a hidden _method input with value DELETE. This is done via some javascript that is including in newly generated rails apps.

You've got

<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>

and you've indicated that you're getting an error loading default.js - I'd guess that you're following an old set of instructions because in a modern app (since rails 3.1) this should be

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

Your application.js file should in addition contain (among other things)

//= require jquery
//= require jquery_ujs

(You can use prototype or other js libraries too)

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • When I initially build the rails, I have application as the tag in application.html.erb. This draws the error added in the main question. Another stackoverflow reccomends the switch to default, to fix the issue – TangibleDream Mar 31 '15 at 13:34
  • @TangibleDream What error? It would help answerers if you describe this (and other attempts you've made) – Frederick Cheung Mar 31 '15 at 13:57
  • Added, tried to add 5 min ago, Stackoverflow had a maintainance banner up. – TangibleDream Mar 31 '15 at 14:00
  • 1
    That's a coffee script related error - if you are referring to http://stackoverflow.com/questions/28312460/object-doesnt-support-this-property-or-method-rails-windows-64bit then there is a much better answer further down the page (I had a similar error and downgrading coffeescript-source worked for me) – Frederick Cheung Mar 31 '15 at 14:07
  • That did it! Thanks Frederick, the bounty is yours. Or will be when they let me give it to you in 22 hours. Thank you. – TangibleDream Mar 31 '15 at 14:20
0

The code you show will do two things:

1) it will create a javascript pop-up that asks "are you sure?". That has to run on the client, not on the server. So Rails generates a "javascript" link that looks like # in your browser.

2) if you click yes, it will send a HTTP DELETE to your server. Unfortunately, browsers don't do this easily. So it generates more javascript code to do that (AJAX).

So what you are seeing not Ruby/Rails, but the Javascript code that Rails generated for you.

BraveNewCurrency
  • 12,654
  • 2
  • 42
  • 50
  • So it says 'Here we're using link_to in a different way. We pass the named route as the second argument, and then the options as another argument. The :method and :'data-confirm' options are used as HTML5 attributes so that when the link is clicked, Rails will first show a confirm dialog to the user, and then submit the link with method delete. This is done via the JavaScript file jquery_ujs which is automatically included into your application's layout (app/views/layouts/application.html.erb) when you generated the application. Without this file, the confirmation dialog box wouldn't appear.' – TangibleDream Mar 28 '15 at 05:42
  • This would not use AJAX. There's not a remote: true attribute set on the link_to method. As it stands now, it will merely send an HTTP ```DELETE``` request to the server. It will then follow the instructions in the ArticlesController#destroy method for redirects after the article has been deleted. – Alex S. Mar 28 '15 at 05:43
  • Oops, right, I am probably mis-remembering about the AJAX bit. – BraveNewCurrency Mar 28 '15 at 05:45
  • I'm seeing a 404 file not found for default.js in the browser console – TangibleDream Mar 28 '15 at 05:58