RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Testing subdomains in Rails with xip.io

http://www.chrisaitchison.com/2013/03/17/testing-subdomains-in-rails

Covers how to get Capybara to play nice without editing /etc/hosts

Avatar

If there was a railscast I'd wish it was covered with tests, then this is the one.

Avatar

I'm probably missing the obvious, but I'm curious why you would place the JSON inside a tag in the HTML. Why wouldn't a user who is interested in the JSON just append .json to the URL?

Thanks.

Avatar

It is samey but it demonstrates all the things you need to know about these frameworks.

I'd expect these screencasts to take a lot longer to produce if Ryan had to write new boilerplate for each one.

Avatar

I just took a look at the ASCIIcast and the show notes, it looks like someone has corrected this already.

Avatar

(using ubuntu 12.10)

ruby
vim ~/.bashrc # add rbenv to the top
vim: command not found

So I used

ruby
sudo apt-get install vim
Avatar

Ember Data expects the server to respond JSON with errors as the root key, then the attribute name with the errors nested inside of that. You can basically do the following in Rails and Ember will accept it.

ruby
      render json: {errors: @project.errors.messages}, status: :unprocessable_entity

This will set 'isError' on that model and will remain at isError (and refuse to commit) until the attributes that the server said had errors are changed.

Avatar

I have to agree with this. Ryan normally provides an amazing resource in Railscasts, but I find the raffler example too contrived to be useful. Especially for a pro cast.

Avatar

I'd like to pass a parameter to the datatable.rb... specifically, the @user = User.find(params[:id]). Side note, I'd also like to be able to use current_user in the new class. I haven't tried it, so I'm not sure if that will even work.

Thoughts?

Avatar

I have an application using devise for my authentication. How do I allow users to NOT have to input their current password every time they want to update their settings?

Avatar

Is Google charts an option for proprietary data? I have an app that is strictly for internal use.

Avatar

how can I replace the method find_by_name in rails4? where(:name = 'params[:name]') ?
i have error in session_controller: undefined method `authenticate' =/ anyone help?

Avatar

ActionView::MissingTemplate: Missing partial exception_notifier/current_user

Any clues?

Avatar

Prawn is working fine in development but in production, my app is not even opening. When I check production logs I get a looping message

production.log
...
19x.xxx.xxx.xxx: Connecting to database specified by database.yml
19x.xxx.xxx.xxx: Connecting to database specified by database.yml
19x.xxx.xxx.xxx: Connecting to database specified by database.yml
19x.xxx.xxx.xxx: Connecting to database specified by database.yml
...
Avatar

Exactly right. This why, if I "must" use AR in a migration, I prefer to redefine (much simplified) classes within the migration file... ensuring that the AR class definitions match the db schema.

Also, for large datasets, executing SQL rather than instantiating AR records can mean your migration runs in seconds rather than minutes/hours.

Avatar

Hi guys,

How to refactor link_to_function using UJS since it will be deprecated in Rails 4.

ruby
def link_to_add_fields(name, f, association)
  new_object = f.object.class.reflect_on_association(association).klass.new
  fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
    render(association.to_s.singularize + "_fields", :f => builder)
  end
  link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
end
Avatar

Hi @caiolufer, I am also facing a problem similar to this. Can you let me know how you solved this?

Avatar

Kobaltz,

Where are you putting the

    if status == 200
      $('#order_stripe_card_token').val(response.id)
      $('#new_order')[0].submit()

clause in your script below?

coffeescript --yum
      if response.error.message == "An unexpected error has occurred. We have been notified of the problem."
        $('input[type=submit]').attr('disabled', false)
      else
        $('#stripe_error').text(response.error.message)
        $('input[type=submit]').attr('disabled', false)

After you test for "An unexpected error..." or before?

Avatar

I am testing on a legacy machine running XP (so IE 8) and not seeing the error. Should probably try on a machine running IE 10.

I cannot track down what I did (probably upgraded the Stripe API on our account), but as I worked back through my Stripe test logs, about 2 weeks ago each execution of:

Stripe.createToken(card, order.handleStripeResponse)

in my coffee script started generating two tokens at the Stripe end, not one. Don't know if it is related because with FireFox (both XP and Mac) the "An unexpected error has occurred. We have been notified of the problem.", does not generate a token log entry (or 2) in the Stripe log, but :

js.coffee
handleStripeResponse: (status, response) ->
    if status == 200
      $('#order_stripe_card_token').val(response.id)
      $('#new_order')[0].submit()

gets to the .submit() line, presumably because of a status ==200.

With Chrome, the error is there, but the token is in the hidden token field on the form and dismissing the error results in a new order being properly created. With FireFox, there doesn't seem to be anything in the hidden token field and order creation fails (but not by redirecting back to the "submit order" page.

Avatar

Not sure if this is the most elegant, but this works if you want permission.rb working with Carrierwave.

in def initialize(user)
allow_nested_param :post, :photos_attributes, [:image]
added method in permission.rb
def allow_nested_param(resources, attribute, nested_attributes)
  @allowed_params ||= {}
  Array(resources).each do |resource|
    @allowed_params[resource.to_s] ||= []
    @allowed_params[resource.to_s] += [{ attribute.to_s => Array(nested_attributes).map(&:to_s)}]
  end
end

References:
strong parameter example for hashes with integer keys
strong_parameters#nested-parameters

Avatar

There's a ton of simple apps he could make. A blog, a forum, a store etc

Avatar

Exactly what I was looking for! Thanks.

Does anybody know how to use Gon in helper methods?

Avatar

I had not tried it with different browsers. I'm on IE 10.

Avatar

I think the order of the routes is important to make this work properly.

resources :products

get ':id', to: 'pages#show', as: :page
resources :pages, except: :show
root to: 'products#index'

Avatar

Are you seeing the error with all browsers?

I'm getting with Firefox (with subsequent failure to create to new entry--order in my case rather than subscription), Chrome (here the new entry is created...)

And not seeing the error with Safari or IE.

Avatar

I still read these. :)

Did you resolve your permissions issue?

Avatar

My 2 cents on Ember after watching this screencast:
- the code after refactoring to extend ArrayController looks actually pretty worse than it was before ( added magic, more lines of code, and bizarre stuff like #each controller ).
- No way in hell am i ever gonna use Ember.js. Weird setters and getters, weird routes that sets controller data, and really a lot of files to do something that looks much simpler in other frameworks. I would prefer going full JS with meteor or another Node framework or Angular.

Avatar

view_context is a method on ActionController::Metal (iirc) that builds a new 'view context object.' This is usually an instance of ActionView::Base, but is override-able. Every time you call it, it generates a new one, because caching is hard, basically.

Avatar

Hi there! I maintain both AMS and Draper, so maybe I can give you some insight. ;)

Draper is about general decoration, AMS is about serializing to JSON. I know several people that use both together: Draper for adding presentation-only concerns, and then AMS to actually do the conversion.

It's arguable that serialization and view-specific logic are two different concerns, and require two different objects.

Also, if you use Draper's #to_json, you miss out on the conventions that AMS provides.

Avatar

Rails is built on conventions. AMS is built on conventions. RABL and JBuilder are built on customizing every last detail of the response.

Avatar

I literally just merged respond_to support yesterday, so it wasn't there when he reviewed.

Also, respond_to isn't all that great, many of us don't actually use it.

Avatar

What do you suggest he builds? I think the point is to just show the basics of working with the technology. I'm happy with samey.

Avatar

I did it like this

confirm: t("sure")

but in fact my question is more for links like the one below :

ruby
<%= link_to (raw('<button class="btn" rel="tooltip" title="Edit"><i class="icol-pencil"></i></button>')), edit_user_path(user)%> 
Avatar

Yes, angularjs is great for my contacts app. But ember is better than angularjs for a whole project. Because ember has associations, rails like model, view, router, controller.

USE ANGULAR: If your project has a single model like contact.
USE EMBER: If your project has several models like railcasts.com app.

Avatar

It works like a charm :) Thank you very much !

Just a question, how can I use I18n in javascript confirmation, for example to delete an item

<%= link_to raw(''), user, confirm: 'Are you sure?', method: :delete %>

Thanks :)

Avatar

An alternative is to use the Representer pattern outside of the view context. There are some implementations such as https://github.com/apotonick/representable.

Avatar

In my last deployment I received this:

[Thu Mar 14 03:34:18 2013] [warn] default VirtualHost overlap on port 8080, the first has precedence
** [out :: staging.absolutecommerce.com]
** [out :: staging.absolutecommerce.com] [Thu Mar 14 03:34:18 2013] [warn] default VirtualHost overlap on port 7001, the first has precedence
** [out :: staging.absolutecommerce.com]
** [out :: staging.absolutecommerce.com] [Thu Mar 14 03:34:18 2013] [warn] default VirtualHost overlap on port 7000, the first has precedence
** [out :: staging.absolutecommerce.com]
** [out :: staging.absolutecommerce.com] [Thu Mar 14 03:34:18 2013] [warn] NameVirtualHost *:8443 has no VirtualHosts
** [out :: staging.absolutecommerce.com]
** out :: staging.absolutecommerce.comAddress already in use: make_sock: could not bind to address 0.0.0.0:7000
** [out :: staging.absolutecommerce.com]
** [out :: staging.absolutecommerce.com] no listening sockets available, shutting down
** [out :: staging.absolutecommerce.com]
** [out :: staging.absolutecommerce.com] Unable to open logs
** [out :: staging.absolutecommerce.com]
** [out :: staging.absolutecommerce.com] Action 'start' failed.
** [out :: staging.absolutecommerce.com]
** [out :: staging.absolutecommerce.com] The Apache error log may have more information.

[
** [out :: staging.absolutecommerce.com]
** [out :: staging.absolutecommerce.com] fail
** [out :: staging.absolutecommerce.com]
** [out :: staging.absolutecommerce.com] ]

Can anybody tell me how to solve the problem?

Avatar

Awesome episode! I was wondering though, how might you handle AND statements for searching on a has_many association? The OR (contains any) is simple enough, just an IN statement. I'm curious how this might handle the other situation.

Avatar

recompiling doesn't work for me. what else should I try?