Rails 3 link_to delete destory method calls show method? - ruby-on-rails-3

Hey I have seen several posts on this, but I am still having issues with calling :method => 'delete' and getting directed to the show method of my controller. The destroy method is working as expect, in that it deletes the comment, but after the request is done, it throws a 404 on GET. Here is the code:
<%= link_to 'delete', "/events/#{#event.id}/comments/#{comment.id}.js",
:confirm => 'Are you sure?',
:method => :delete,
:remote => true %>
Here is the controller method:
def destroy
#comment = #event.comments.find(params[:id])
#comment.destroy
redirect_to do |format|
format.html # redirect_to #event, :notice => "comment deleted" }
format.js { render 'destroy.js.erb' }
end
end
I've hear that this could be due to not using button_to, but I have tried using button_to as opposed to link_to but this does the same thing.
I've also heard that this could be do to some problems with the way you are using jquery in your set up, but I feel like I doubt that, but here is how I call in jquery just in case (application.html.erb):
<%= javascript_include_tag 'jquery-1.5.2.min.js', 'rails', 'application' %>
<%= javascript_include_tag 'jquery-ui-1.8.17.custom.min.js' %>
When i watch rails server output I see that it's says:
Redirected to http://0.0.0.0:3000/events/1/comments/35
Completed 302 Found in 132ms
ACTION NAME application
Started GET "/events/1/comments/35" for 127.0.0.1 at Wed Feb 08 16:31:43 -0800 2012
AbstractController::ActionNotFound (The action 'show' could not be found for CommentsController):
Thanks for the help!

Rails 3.1 and greater cleans a lot of this up for us...rails.js is gone. Jquery_ujs is part of the gem, but the file should NOT be in your javascript directory:
specifically, my jquery_ujs file was old and still "living" in my assets/javascript directory. It still had .live methods which are deprecated. Delete the file!!
Keep the gem 'jquery-rails', '~> 2.1' # REF: https://github.com/rails/jquery-ujs
Keep the //= require jquery and //= require jquery_ujs and //= require_tree .
In your application.js file
Make sure you update the gem and bundle install to get the latest gem.
If a javascript error is thrown as was the "sneaky" case for me, the rest of link_to js will not work. Now the delete and the confirmation dialog are working fine. An old jquery_ujs was the culprit.

I would make sure you have the proper version of rails.js in your app. rails.js is what now takes care of making sure delete links actually use the delete method, so you may need to update your rails.js file. What version of rails are you running?

It might be an issue with your config/routes.rb file. If an earlier entry in the routes.rb file matches events/:id/comments/:id and points to comments#show, it could cause this error.

format.html might cause it.
Change this line with
format.html { render :nothing => true }

Related

Rails: How to disable turbolinks in Rails 5?

It's a constant headache when dealing with websockets, and it kills my performance in addition to adding bugs. Since ActionCable is the whole reason I upgraded I'd very much like to get rid of it completely.
The following was copied from here. It's for Rails 4, but I believe the steps are the same.
Remove the gem 'turbolinks' line from Gemfile.
Remove the //= require turbolinks from app/assets/javascripts/application.js.
Remove the two "data-turbolinks-track" => true hash key/value pairs from app/views/layouts/application.html.erb.
Edit: As of at least Rails 5 the last step should refer to "data-turbolinks-track" => "reload" as opposed to "data-turbolinks-track" => true. Thanks to #boddhisattva
Edit: As of at least Rails 4.2 you can generate a project without turbolinks to begin with. Just use something like this:
rails new my_app --skip-turbolinks
Removing //= require turbolinks from app/assets/javascripts/application.js seems to have done the trick.
I also removed both turbolinks references in app/views/layouts/application.html.erb
If you are using Webpacker (Rails 5-6)
Delete this line from Gemfile and run bundle:
gem 'turbolinks', '~> 5'
Run yarn remove turbolinks
Delete this line from application pack file app/javascript/packs/application.js:
require("turbolinks").start()
Remove any data-turbolinks data attributes from your html.
Change:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
to
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_pack_tag 'application' %>
you can also do it when you create your rails application by using;
rails new app name --skip-turbolinks
Completely removing the turbolinks tags from application.html.erb might break CSS and JS. add this lines instead of the turbolinks if no CSS or JS is loaded:
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>

paperclip with amazon s3 working in development but not working in production (amazon ec2)?

I'm pretty new to rails and seem to be having an issue with the paperclip gem. I installed the gem and it works well in development (localhost:3000) but when I'm running it on the amazon server, for some reason it does not want to attach files, and the app breaks (error 500 page).
i have deployed my code to amazon server, i configured bucket details also, but still it is having problem with production, although it works fine with localhost.
Here is the process i ran... I pushed my file to amazon. This did not seem to help.
Here is the code that I have for paperclip:
user.rb model:
has_attached_file :avatar,
:styles => {
:thumb => "50x50",
:medium => "400x400",
:square => "70x70"
},
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => "mybucket"
my User form:
<%= form_for(#user, url: "/sessions/#{current_user.id}", :html =>{:method => :put, :class => "form-horizontal", :multipart => true}) do |f| %>
<%= f.file_field :avatar %>
<% end %>
breaks down in production. Any pointers would be greatly appreciated... I just cant seem to figure this out and it's pretty frustrating. Thank you so much for your time and any help! please give a pointer where i am making mistake.
Had same issue with image_magic that was breaking our paperclip functionality in production, but not in development (weird, I know). Yet even after removing imagemagick from our gemfile and Gemfile.lock locally (running bundle install and all that stuff) and then deploying back to production on heroku, the error persisted in production! (weird, I know).
What ended up doing the trick was running:
$ heroku repo:purge_cache -a myAppName
(Taken from: https://github.com/heroku/heroku-repo#purge_cache)
When you deploy your app, Heroku caches some things like your assets and installed gems in order to speed up deployment. Although this is a great feature, it can have side-effects sometimes, and in this case, it seems that something about the imagemagick gem got "stuck" in production's cache, which is why purging solved the issue for us (since after purging, your app will rebuild itself from scratch on your next deployment)

rails 3 rails couldn't find file 'twitter/bootstrap' pt2

This is my second post regarding this issue as my webrick server spits out this message. I've had this before and twiddled with the application.css file to get it to work. My rails is 3.1.3 on ruby 1.9.3. The response from rails is couldn't find file 'twitter/bootstrap'
"all" %>
Researching on Google I've followed seyhunak's responses on this issue. Here is my line in the Gemfile, on its own line and not in a group.
gem 'twitter-bootstrap-rails', :git => 'http://github.com/seyhunak/twitter-bootstrap-rails.git'
Here is what's in my application.css.scss:
*= require_self
*= require bootstrap_and_overrides
*= require_tree .
Here's what's in my application.js
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require require_tree .
I have done the following commands:
bundle update
rails g bootstrap:install
rails g bootstrap:layout application fixed
touch bootstrap_and_overrides.css.less
rails s
After restarting the server, I still get that error. I'm unable to get past this.
I think I've exhausted all Stack and Google can suggest. What am I overlooking? thanx, sam
After beating myself up over this, I got some Rails devs to look at this problem. My understanding is that Twitter/Bootstrap needs files to be in the vendor directory. The gem would then need to be removed, commented out, so they would not conflict. My app now shows pages that look like the project's homepage. I'm unsure if I've lost the ability to use less to change things. This is my inexperience showing. Thanks for looking.
Moving gem "twitter-bootstrap-rails" out of group :assets
And doing a bundle install worked for me.
Here is some more information
https://github.com/seyhunak/twitter-bootstrap-rails/issues/91
I have similar problem,
I use application.html.haml instead of application.html.erb
using this link:
HTML to Haml
Code is as:
%html
%head
%title Task
= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true
= javascript_include_tag "application", "data-turbolinks-track" => true
= csrf_meta_tags
%body
= yield
You Must install Haml gem first:
install haml gem rails

"assets" getting prefixed on a link_to

I have a link_to in a partial that generates a different URL when the page is accessed as the root then if it is accessed using controller/index. When accessed as the root, the URL the link_to generates has "assets" prefixed to it, which doesn't work.
Can anyone explain why this happens?
Rails 3.2.1
Ruby 1.9.3
With some help from the Rails IRC channel, I found the problem. I was not using the helper paths in the link. In case it will help someone else, here is what I did to fix it;
not working when at the root;
<%= link_to 'Gifts detail', :controller => 'gifts', :action => 'index'%>
working anytime;
<%= link_to 'Gifts detail', gifts_path%>

Devise not working after update, Tests via capybara complete without failures

I'm quite new to RoR and programmed a couple of things before discovering Test-Driven-Development. So I tried to write some tests with guard-rspec and capybara. All was fine, but I got some deprecation warnings for devise (had version 1.1.rc02), so I updated it (now having latest version 1.4.5).
These were the warnings:
Running all specs
DEPRECATION WARNING: Using form_for(:name, #resource) is deprecated. Please use form_for(#resource, :as => :name) instead. (called from _app_views_devise_sessions_new_html_erb___942904761_2291541620_0 at /Users/xonic/Documents/work/ror.ror.at/app/views/devise/sessions/new.html.erb:3)
FDEPRECATION WARNING: Using form_for(:name, #resource) is deprecated. Please use form_for(#resource, :as => :name) instead. (called from _app_views_devise_sessions_new_html_erb___942904761_2291541620_0 at /Users/xonic/Documents/work/ror.ror.at/app/views/devise/sessions/new.html.erb:3)
.DEPRECATION WARNING: Using form_for(:name, #resource) is deprecated. Please use form_for(#resource, :as => :name) instead. (called from _app_views_devise_registrations_new_html_erb__1735759005_2291086140_0 at /Users/xonic/Documents/work/ror.ror.at/app/views/devise/registrations/new.html.erb:3)
So I changed
<%= form_for(resource_name, resource, :url => registration_path(resource_name)) do |f| %>
to
<%= form_for(resource, :url => registration_path(resource_name)) do |f| %>
and got rid of the warnings. So far so good, I continued with my tests and did not try to open the application in the browser myself.
I'm not sure what happened then but all of a sudden, when trying to open the application in the browser, i get the following error message:
NoMethodError in Slides#index
Showing /Users/xonic/Documents/work/ror.ror.at/app/views/layouts/application.html.erb where line #27 raised:
private method `split' called for #<Class:0x10c023278>
Extracted source (around line #27):
24:
25: <div class="gopro">
26:
27: <% if user_signed_in? %>
28: Signed in as <%= link_to current_user.username, user_path(current_user) %>. Not you? <%#, user_slides_path(current_user.username) %>
29: <%= link_to "Sign out", destroy_user_session_path %>
30: <% else %>
So obviously, Devise is in trouble... no idea why. Funny thing is, when calling save_and_open_page while testing the app, a browser window opens with the application displaying correctly and no errors at all.
Guard::RSpec is running, with RSpec 2!
Running all specs
...
Finished in 0.95467 seconds
3 examples, 0 failures
UPDATE:
I've seen this very similar question Devise: NoMethod Error & user_signed_in and have correctly set up the user model, so that's not the problem.
This is quite confusing to me, hopefully someone out there understands what's happening crossingFingers
Thanks,
xon1c
UPDATE
I fixed it, but stackoverflow won't let me answer my own question in the next 6hrs because of lack of reputation sigh. Will post the solution later.
Ok, I fixed it (close to perfect). Here's what i did:
I reverted git to a working status and began from scratch. This time I did not use the line
gem 'devise', :git => 'git://github.com/plataformatec/devise.git'
in my Gemfile, instead I only put
gem 'devise'
and then installed the latest devise gem through
gem install devise
rails generate devise:install
The command installed version 1.4.5, not 1.4.7 as I previously had. Then I installed all the other gems, namely capybara, guard-rspec, factory_girl_rails and launchy. Everything works fine now except these two warnings I'm left with:
Running: spec/requests/sign_ups_spec.rb
DEPRECATION WARNING: :name_prefix was deprecated in the new router syntax. Use :as instead. (called from /Users/xonic/Documents/work/ror.ror.at/config/routes.rb:7)
DEPRECATION WARNING: :name_prefix was deprecated in the new router syntax. Use :as instead. (called from /Users/xonic/Documents/work/ror.ror.at/config/routes.rb:7)
.
Finished in 0.16255 seconds
1 example, 0 failures
Well, as long as everything works, I'm ok with the warnings but if anybody knows how to get rid of them, please let me know.
Thanks.