binding.pry ignored by rails 3 - ruby-on-rails-3

I'm trying to debug my little app failing to create a Price because of an AssociationTypeMismatch. So I dig out pry, install, watch railscast and Josh's screencast and dig into the wiki.
I "cd" into the prices controller but that fails. I've put <% binding.pry %> into the new.html before the submit code but Rails says that the 'pry' method is unknown. pry is in my updated Gemfile and I restarted the server.
I put binding.pry into the create method of my prices_controller.rb and Rails happily breezes through it to throw the error I've already seen, not the pause, etc I was expecting.
Looking at the Price model, I'm not experienced enough to know what I should be looking for. What am I overlooking? thanx, sam

I have the exact same problem, my solution was to add pry-remote to my Gemfile, and call binding.remote_pry instead of binding.pry, open the terminal at your current project and execute pry-remote. happy debugging :)

It appears that in addition to the pry gem, I need the pry-rails gem as well. Now the binding.pry is recognized.

Instead of installing pry-rails you could just require 'pry' before the binding :-)

Related

Rails UserRegistrationsController not Inheriting from Devise::RegistrationsController

I'm pretty new to Rails, but followed the excellent suggestion/walkthrough here. I've gotten the creation of my two user types (Clients and Developers) to work successfully with Devise. However, I also want to allow both user types to edit some of their attributes after they are logged in. These attributes are also specific to their user types (client specific fields or developer specific fields).
To do this, I thought that I needed to create a custom update in user_registrations_controller which inherits from Devise::RegistrationController as would make sense. During my customization however, I realize within update I am unable to call other default methods from Devise::RegistrationController. As a sanity check, I overrode update with an exact copy of the update method as defined in the original Devise::RegistrationController and still had this issue.
More specifically, the error I receive is: undefined local variable or method 'account_update_params' for #<UserRegistrationsController:0x007fe4f0342d58>, despite account_update_params being a method in the inherited controller.
Is there a better way to edit the fields specific to my user types (client/developer) in Devise without having to customize update? Also, what is going on with this error here, as I believe I am inheriting from Devise::RegistrationController?
Any thoughts? Help and suggestions much appreciated! =)
Versions: Rails 3.2.12, ruby 1.9.3p249
After revisiting this, I found this related question. Though this post and top answer is specific to Rails 4, the solution can be found in the Gemfile.
Replace your devise gem include with
gem 'devise', :git => 'git://github.com/plataformatec/devise.git'
Simply doing gem 'devise' does not get this updated version, unfortunately. Hope this helps others!

premailer rails not generating text part automatically

I want to use premailer-rails to generate text part of email automatically.
gem 'hpricot'
gem "premailer-rails"
I'm not getting the text part when I look at the actual email.
It seems as if the gem is not enabled. Any idea why?
If the gem is not working, it may be because you are missing an initializer
Add this code in config/initializers/premailer_rails.rb
Premailer::Rails.config.merge!(preserve_styles: true, remove_ids: true)
Also
If you're using this gem outside of Rails, you'll need to call Premailer::Rails.register_interceptors manually in order for it to work. This is done ideally in some kind of initializer, depending on the framework you're using.
You can find this information on the gem's Github page https://github.com/fphilipe/premailer-rails.
On another note, they also recomend to use nokogiri instead of hpricot, seeing as the latter is no longer mantained.

Rails Amazon S3 Paperclip/s3-swf-upload-plugin Associating attached files

Rails 3.0.5
gem 's3_swf_upload', :git => 'git://github.com/nathancolgate/s3-swf-upload-plugin'
gem 'aws-s3'
gem 'awk-sdk'
gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
This is a "is it possible and how hard is it" question. I had a requirement to take in video submission to a website and store them on Amazon S3. Originally I was going to do this via paperclip. However, some one made me aware that we may eat up all our dinos since the file would first be uploaded to our app on heroku first.
Instead I went with the GEM s3-swf-upload-plugin. This is supposed to by pass our server and upload using javascript and flash. (If I am wrong on this please let me know but so far it seems to work as expected.) However, as far as I can tell I can't rename the file with out a fair bit of javascript and I don't think I can collect the URL of the file to link in my object.
What I would like to do: After the file is uploaded and before I save my object I would like to go out to S3 find the file, use paperclip to move/rename it (Using it's useful :path => ":class/:id/:filename") and then assigning the typical paperclip attributes so that I can link to the video in my object.
I have search around a bit today (I admit I have not search as long as I usually do before I come here but my time to get this done is limited) and I all can find is how to set it up normally. The s3-swf-upload-plugin has something which I think may be doing exactly what I need but I don't understand it enough to fit it into my code. https://gist.github.com/575842
If the Gist above is what I need does any one mind explaining what the important part do so I might understand and modify it appropriately? If it is not what I need is there something out there that would help? Thank you all for any help you can provide.

Rails 3 CSRF_meta_tag not functioning

I am in the process of upgrading my app from Rails 2.3 to Rails 3. I read that I need to insert
<%= csrf_meta_tag %>
in my layouts, which I did. Unfortunately, when I used ajax requests, I kept being logged out. After a little research, I found an older way was to add
$j(document).ajaxSend(function(e, xhr, options) {
var token =$j("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", token);
});
in my application.js file. It now works properly (It seems I actually don't need the csrf_meta_tag).
Is it normal I had to add these 4 lines of code in my application.js instead of just the csrf_meta_tag, or am I just missing something?
Firstly, you do need csrf_meta_tag. If you don't use it in your layout the csrf-token meta will not be generated then your application.js fix stops working.
The jquery-ujs gem (jquery-rails) should take care of this for you. It's possible the version you use have some bug. (I do remember there's a related bug in 3.0.x, but couldn't recall the exact issue). Try updating jquery-ujs and re-generate jquery (rails generate jquery:install), remove rails.js to see if the issue be fixed.
Bonus note: as of rails 3.1, csrf_meta_tag has been renamed to csrf_meta_tags, but the former one could still be used.
Please see jquery-ujs on github.

Question about Google Analytics Tracking (w/ Rails)

I've got google analytics setup on a rails project, and I've got "A single domain (default)" selected for the tracking options.
I've copied and pasted the js code into the layout for the application.
Now, if I use this locally, does analytics track the local use as well?
The reason I'm asking this is we've been running tests on our dev computers using rspec, and there seems to be a spike in the analytics. All these spikes also seem to show up as unique visitors.
I'd appreciate any insight on this.
Thanks!
Also, using a Google Analytics gem will automatically set some of these features for you. Here is a great way to do it:
Google Analytics and Rails in 5 EASY Steps:
If you are in Rails 3, I just found a great solution for doing Google Analytics in Rails apps.
(1) In your Gemfile:
group :production do
gem 'rack-google_analytics', :require => "rack/google_analytics"
end
(2) Bundle Install
(3) In your config/application.rb (put this in the class definition section - careful not to drop it in a module. I put mine right under "class Application"):
if Rails.env == "production"
config.middleware.use("Rack::GoogleAnalytics", :web_property_id => "UA-0000000-1")
end
(4) Initiate your Google Analytics account
(5) Copy and paste that funky web_property_id from Google's supplied code into the code from (3), replacing 'UA-000000-1'
That's it!
I originally found this solution here:
David Bock Article
I tried the gems but they didn't work; wouldn't spit out any code, etc. Seemed dumb for something so simple. So I ended up just doing this, in application.html.erb:
<% if Rails.env.production? %>
(GA JS Code Snippet)
<% end %>
Yes, it does track local visits as well.
You should probably use ruby conditional statement to exclude it for the local conneciton.
For example, at the bottom of the layout file
<% if !request.local? %>
Your source codes for Google Analytics
<% end %>
This way, Google Analytics will not be printed if connection is made from local.
In the google analytics admin, you can filter out visitors based on various attributes (e.g. ip address) This would also be a good idea to do.
Another option that I've done is add another analytics tracking account that you use for the dev/test environment so that you can test whether and how analytics are working.
This tutorial shows you how to set it up. I don't know why there's a gem for this.
Just put the code in a partial and render it in your application layout if rails.env.production?
http://aihuiong.com/post/452550136/google-analytics-and-rails-in-3-steps-and-less-than-5