Guide "Getting Started with Rails" contains obsolete infomation

In section “5.13 Deleting Articles”, below code is obsolete in latest version of Rails. The link cannot delete the record anymore. I googled the problem and found if change “link_to” to “button_to”, it works. There are some other ways to do the deletion. While, my question is that how can we get the “Guide” updated?

<h1>Listing Articles</h1>

<%= link_to 'New article', new_article_path %>

<table>

``<tr>

``<th>Title</th>

``<th>Text</th>

``<th colspan="3"></th>

``</tr>

``<% @articles.each do |article| %>

``<tr>

``<td><%= article.title %></td>

``<td><%= article.text %></td>

``<td><%= link_to 'Show', article_path(article) %></td>

``<td><%= link_to 'Edit', edit_article_path(article) %></td>

``**<td><%= link_to 'Destroy', article_path(article),**

``method: :delete,

``data: { confirm: 'Are you sure?' } %></td>

``</tr>

``<% end %>

</table>

I am using the latest version of Ruby and Rails, and this works find eventually in my environment. The issue is that the Rails is not working well in Windows and the jQuery was not loaded properly. You should check the message from the server (if you are using ‘rails servier’, just check the command prompt)

If there is any issue regarding the guide, you could check here.

**I have exactly the same issue.`**

I am using Rails version 4.2.5.1, with ruby version 2.3.0p0 (2015-12-25 revision 53290) on a CentOS 6.6 Linux box. The “Getting Started…” guide has a incorrect material, which does not work right. This is unprofessional.

The “Destroy” option will not execute the delete action. This problem is serious, and problematic for anyone learning and using this “Rails…” product.

I have reviewed a lot of material, and attempted several workarounds, but the “Destroy” with “link_to” does not work. It simply renders the weblog page, as in “Show”. If I replace “link_to” with “button_to”, then the delete method can be triggered, but THERE IS NO DIALOG BOX GENERATED. I have tried various methods to attempt to load/reload the jquery-rails gem, verified the application.js has correct references, and even tried to manually load and reference the .js files. NOTHING WORKS. There is SIGNIFICANT documentation on exactly this issue, in numerous posts on Stack Overflow and other sites, but I have been unable to resolve this.

Many other people trying to use the product have reported exactly the same issue.

It is deeply unfortunate that the excellent “Getting Started with Ruby on Rails” tutorial is flawed in this way. I have confirmed the “Gemfile” has the “gem jquery-rails”, and have run a “bundle install”, without any errors being flagged.

The Tutorial is wrong, and should be corrected. Or the borked code that is causing this should be fixed and flagged in the “Getting Started” Guide. At the very least, some method should be documented which allows this Rails software to post a dialog box, and accept a user action, for an action which removes records from an associated SQL database.

I can use “button_to”, instead of “link_to” in the “index.html.erb” view, which will then correctly trigger the right method, and delete the SQL data, but there is no confirmation dialog, which is of course unacceptable. Just to be clear, the …/app/assets/javascripts/application.js has the “//= require jquery” and the “//= require jquery_ujs” entries, as some authors have suggested as a fix.

Perhaps someone could provide a detailed tutorial on how to ensure that jQuery code is loaded correctly by the “Rails” environment. I will do it if I have time, but I am concerned that using this “Ruby on Rails” approach might not be a wise choice for a production system, as the environment seems unstable and inaccurately documented.

`

Hi Mark,

Thanks for reporting the issue. Is it possible for you to upload a sample application on Github which is having this issue? That will help us in getting to bottom of this issue.

Thanks.

Hi Mark,

Is your JavaScript console in your browser showing any errors?

Do you have the code for the app on GitHub too?

Hi Ryan; I did not see any Javascript errors in the browser, and I’ve confirmed same behaviour on Firefox 44 on Windows, Safari on iPad, and Firefox on Android tablet. What seems to be happening, is that the Javascript libraries are not being loaded in production mode, but the are being loaded in development mode. I start the “rails” server on the CentOS Linux box, with a two-line script: export SSL=true rails server -b 0.0.0.0 -p 3000 -e production

I’ve looked at a lot of material on this issue, and confirmed I had all the obvious suggestions already defaulted. Eg. In Weblog/app/assets/javascripts file application.js, the lines “//= require jquery” and “//= require jquery_ujs” are present. And the file application.html.erb, in Weblog/app/views/layouts has the line: <%= javascript_include_tag ‘application’, ‘data-turbolinks-track’ => true %>, and so on. (Ie. I’ve been thru all the Stack Overflow reports of this specific issue…)

My original assumption - that the Ruby-on-Rails “Getting Started…” Guide was obsolete - is wrong. The “link_to” verb does work, but I have not been able to nail down exactly why the javascript libraries are not being loaded. If I run in development with “rails server -b 0.0.0.0 -p 3000 -e development”, then the “link_to” in the example works (it presents the dialog box, when “Destroy” is selected from the blog article list, and then successfully deletes the article).

In directory …/Weblog/config/environments there are three “.rb” files (Ruby source), development.rb, production.rb and test.rb.

As a kludge workaround, I have taken a copy of the production.rb and the development.rb files, and simply renamed the production.rb file to “development.rb”. This then results in the “Weblog” tutorial app running correctly, as documented. (The dialog box is presented to the user, when the “Destroy” option is clicked, and this behaviour is correct and consistent across all browsers tested - Firefox 44 on Windows, Safari on iPad, and Firefox on Android (5.0.2 Lollipop).

The differences between the two files in /Weblog/config/environments is as follows (I have hand-keyed this, just by looking at the screen on the Linux box, so there might be typos, and I have not typed the comments or commented out code)

development.rb (original file - which I just copied to production.rb, to get the app working…)

Rails.application.configure do config.cache_classes = false config.eager_load = false config.consider_all_requests_local = true config.action_controller.perform_caching = false config.action_mailer.raise_delivery_errors = false config.active_support.deprecation = :log config.active_record.migration_error = :page_load config.assets.debug = true config.assets.digest = true config.assets.raise_runtime_errors = true config.action_view.raise_on_missing_translations = true end

production.rb (original file - which makes the “link_to” verb NOT WORK correctly, probably because jquery/jquery_ujs are not loaded…)

Rails.application.configure do config.cache_classes = true config.eager_load = true config.consider_all_requests_local = false config.action_controller.perform_caching = true config.serve_static_files = ENV[‘RAILS_SERVE_STATIC_FILES’].present? config.assets.js_compressor = :uglifier config.assets.compile = false config.assets.digest = true config.force_SSL = true config.log_level = debug config.i18n.fallbacks = true config.active_support.deprecation = :notify config.log_formatter = ::Logger::Formatter.new config.active_record.dump_schema_after_migration = false end

I have not go thru line by line, making changes to the production.rb file, since probably someone will just recognize the issue (I hope.)… Perhaps the “uglifier” is mangling the javascript it sends, and so the browser is just flushing it?

Any suggestions appreciated. I will track this down myself, if I have to, and post the results of my reseach here, and to Stack Overflow as well. Thanx for any info.

Oh, one note on another work-around: The “link_to” verb (in the example) can be replaced with a “button_to” verb, which will create a button on the displayed webpage, instead of a highlited link. This will work (without javascript libraries), but it WILL NOT present a dialog box. It will just destroy the article, with no confirmation prompt (not good, obviously). This is a popular workaround to this problem, as indicated by the postings to Stack Overflow. Since most browsers support javascript, the “link_to” is obviously the preferred method for any delete/destroy action.

Hello Prathamesh;

Please see my response to Ryan Bigg;

I will try to post details to Github, once I nail down exactly what is happening. I have a work-around, which involves just copying the “development.rb” file overtop of the “production.rb” file. These files are in …/Weblog/config/environments, in the sample application that is generated by following the “Getting Started …” Guide.

As I mentioned in my response to Ryan, I think the problem is related to the Javascript libraries “jquery” and “jquery_ujs” either not getting loaded, or not being used correctly, by this version of Rails. The “Getting Started with Ruby on Rails” tutorial guide looks to be correct, and my suggestion that it is obsolete was wrong.

Several folks recommend just replacing the “link_to” verb with “button_to”, but that does not present the dialog box. It just destroys the article entry in the SQL database, without any confirmational prompt, which is of course, not acceptable practice.

Here are my two “development.rb” and “production.rb” file contents. The “development.rb” file works ok, and the “production.rb” file does not work (ie. when one clicks “Destroy” link on the webpage, the page simply displays, no action occurs.)

Rails.application.configure do config.cache_classes = false config.eager_load = false config.consider_all_requests_local = true config.action_controller.perform_caching = false config.action_mailer.raise_delivery_errors = false config.active_support.deprecation = :log config.active_record.migration_error = :page_load config.assets.debug = true config.assets.digest = true config.assets.raise_runtime_errors = true config.action_view.raise_on_missing_translations = true end

production.rb (original file - which makes the “link_to” verb NOT WORK correctly,

probably because jquery/jquery_ujs are not loaded…)

Rails.application.configure do config.cache_classes = true config.eager_load = true config.consider_all_requests_local = false config.action_controller.perform_caching = true config.serve_static_files = ENV[‘RAILS_SERVE_STATIC_FILES’].present? config.assets.js_compressor = :uglifier config.assets.compile = false config.assets.digest = true config.force_SSL = true config.log_level = debug config.i18n.fallbacks = true config.active_support.deprecation = :notify config.log_formatter = ::Logger::Formatter.new config.active_record.dump_schema_after_migration = false end

I will do a bit more research to nail down what is happening. I have already posted a bug issue on the “Getting Started…” Guide “link_to” example not working to Github. Once I determine exactly what is happening (ie. why development.rb works, but production.rb does not), I will post the details to Github, so the error location is more clear. Thanx for your suggestion.

Just a note: What happened when I built the “Gettings Started with Rails” sample app, was that the “Deleting Articles” method did not work also, as I indicate below. The issue involved a parameter in the “production.rb” file, where a parameter is coded by default as false. This parameter is: “config.assets.compile = false”. If you have not “precompiled” your assets, this value set to “false” prevents the javascript from being sent from the WEBrick rails server to your browser. You get no javascript generated. (Note: I am using Ruby version 2.3.0p0,rev. 53290, i686-linux, and rails 4.2.5.1, with sqlite3 1.3.11).

The solution for my experimental website, was to toggle this value to “true”. This means that in “…/Weblog/config/environments” directory, you can simply edit the “production.rb” file, and set the “config.assets.compile = true”, and your javascript will now be sent from the WEBrick rails server, to your browser, as you would expect.

You can then start the WEBrick rails server in production mode, and the example website should work, assuming your target browser has javascript enabled. (For Firefox, check with “about:config”, search for javascript, confirm it is set to “enabled”.). On the Rails server side, if running on a Linux box, (in my example), I start the default rails server from a terminal window, in the Weblog directory, with: “export SSL=true” and then “rails server -b 0.0.0.0 -p 3000 -e production”.

With the “production.rb” parameter “config.assets.compile = true” your website should work. (This means the “Destroy” option should bring up the dialog box, and then if confirmed, invoke the delete method, and remove the selected article from your SQLite database. ) It also means that images that you put in “…/Weblog/app/assets/images” directory, will actually render, if you use the rails “image_tag” verb. (I think Rails calls these verbs “helpers”). For example, you can render a .jpg or .png file, by just dropping the file into the images directory, and then putting "<%= image_tag ‘ImageFileName.jpg’ %> in your “index.html.erb” file, which lives in the “…/Weblog/app/views/welcome” directory. But without the “config.assets.compile = true” line in your “production.rb” file, you just see the actual image-filename rendered in your browser, instead of the seeing the image.

Basically, the “Getting Started with Rails” tutorial is correct. But one has to confirm that the many Rails parameters are set correctly, to ensure the javascript code is sent from the WEBrick rails server (- or server “Thin” or “Unicorn” or “Apache” / whatever web-server you are using) - to the browser. For large production systems, you will want to pre-compile your “assets” - ie. javascript, images, etc. See Rails Guide:

http://guides.rubyonrails.org/asset_pipeline.html#in-production

Hope this is useful. There are a fair bit of obsolete suggestions on others sites about how to fix this problem, which appears to have been successfully addressed in current versions of Rails. :slight_smile:

The getting started guide does not ever require you to run your app in production. Knowing that you were running it in production would’ve been useful information to know during your initial post.