RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I recently wrote an article about how to set up Angular in Rails, making Karma aware of the assets pipeline.

The magic is basically in a rake task:

ruby
sprockets = Rails.application.assets
sprockets.append_path Rails.root.join("spec/karma")
files = Rails.application.assets.find_asset("application_spec.js").to_a.map {|e| e.pathname.to_s }

with the karma configuration being:

javascript
files: [
  APPLICATION_SPEC,
  'app/assets/javascripts/angular/*/*.{coffee,js}',
  'spec/javascripts/**/*_spec.{coffee,js}'
]

More info: http://sebastien.saunier.me/blog/2014/02/04/angular--rails-with-no-fuss.html

Avatar

Thank you so much SimplizIT. That was very helpful. Please keep it up.

Avatar

In this approach, the searching is case-sensitive, how can i make that case-insensitive?

Also, i need to search the text in multiple fields using where clause and or operation in MongoDB. How can i do that?

Avatar

Thank you this was very helpful

Avatar

Make sure you add image or whatever you added in the migration to your params in whatever controller you added it to.

Avatar

hi steve, did you ever find a solution or determine the root of this problem? i'm having the same issue running rails 4 and thin on OS X 10.6.8 in development...

Avatar

Adding the stop callback to the paintings.js.coffee file worked for me.

jQuery ->
  $('#new_painting').fileupload
    dataType: "script"
    add: (e, data) ->
      file = data.files[0]
      data.context = $(tmpl("template-upload", file))
      $('#new_painting').append(data.context)
      data.submit()
    progress: (e, data) ->
      if data.context
        progress = parseInt(data.loaded / data.total * 100, 10)
        data.context.find('.bar').css('width', progress + '%')
    stop: (e, data) ->
      $('.upload').hide()
Avatar

same problem :-(
Did you find a solution yet?

Avatar

How can I deal with locales regarding form objects?

My labels and error messages don't use what's defined in the YAML like they do with normal active record models.

I would really like some input on this, since to work around it I had to bake my own solution and replace every validation with my own.

Avatar

Well, it took 10 months but I finally got around to creating a gem to do this :)

https://github.com/veloper/zeusd

run zeusd restart --block in your pre block and enjoy!

Avatar

for those who are experiencing the error, "ERROR: Rack::Utils::OkJson::Error: cannot encode Symbol", i suggest using the taps-taps gem instead of just "taps." that's the only changed i had to make to get taps to work (it appears taps is incompatible with rack 1.5, but i can't say for sure). thanks to the following resource for solving the problem.

https://shellycloud.com/blog/2013/10/easy-database-migration-using-taps

Avatar

Depending on how custom you need the JSON, RABL or JBuilder are still used. I prefer JBuilder as it's a little bit more flexible for my needs. I needed custom keys that didn't depend on the model name. If however, you just want a simple data structure with model-name-based keys, then you should use ActiveModel Serializers

Avatar

I just got the same error, on Capistrano 2.15. Looking back through my shell history, during the automatic ssh login I fat-fingered the command and so that wasn't working correctly either.

If this is the case for you, just redo that automatic login step and then retry cap deploy:setup once you have it working.

Avatar

When you guys move on to Rails 4, use

ruby
puts "friendly_id :foo, use: [:slugged, :finders]"
ruby
puts "friendly_id :foo, use: :slugged"
Avatar

For anyone wondering, this can now be accomplished by typing passenger start from the root of your application.

Avatar

Mike, did you get this to work with Capistrano 3.0 or did you use 2.15? If so, would you mind pasting the code? I'm also using Ubuntu 13.04 on a DigitalOcean VPS. I'm getting this error after cap deploy:setup. I tried downgrading to Capistrano 2.15.5 and continued to get the same error.

connection failed for: 123.456.789.000 (Net::SSH::AuthenticationFailed: Authentication failed for user ...)

Avatar
ruby
class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = {}

  def self.connection
    @@shared_connection[self.connection_config[:database]] ||= retrieve_connection
  end
end

This is a modified version I've used for projects that have multiple connections

Avatar
app/controllers/users_controller.rb
def index
  @users = User.all
end

then you'll probably need to create the file:

app/views/users/index.html.erb
<% @users.each do |u| %>
<%= u.email %>
<% end %>

No promises that that works, but hopefully it supplies some instant gratification.

You'd be better off looking through some tutorials though if you don't understand the errors you're getting.

http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
^ is overkill for newbies, but I haven't found a more gentle learning source

Avatar

Hello. Newbie here.

I cant get the http://localhost:3000/users view to show properly. I get served a:

Unknown action
The action 'index' could not be found for UsersController

Any hints to as what I am doing wrong?

greatfull for any input.

Avatar

If you're having a problem where the theme's prompt isn't displaying correctly, just check you haven't brought in an errant PS1 prompt from your .bash_profile (or elsewhere)

Avatar
ruby
def sorted_messages
  the_messages = messages.arrange(order: :created_at)
  sorted_messages = the_messages.sort_by{ |key, value| key[:updated_at] }.reverse
  Hash[sorted_messages]
end
Avatar

figured out why calling html() with an element object works at all:
http://jamesallardice.com/passing-an-element-or-object-to-the-jquery-html-method/

...so the html() code WAS working right, but it's really calling .append(). Probably clearer to just use append().

(there's a big advantage to appending the element, rather than just the html from it by doing basically .html(...el.html()) : the @el object can be changed in the future in the js code, and we don't need to bother #container again.)

also, this was keeping me from getting that reset callback:
http://stackoverflow.com/questions/15603107/backbone-collection-fetch-doesnt-fire-reset

Avatar

I don't want to use any gem for authorization
so can you please help me out to use this particular authorization technique when we have multiple roles for the user.

I want to use it on rails 4

Avatar

Absolutely amazing screencast. I keep discovering new things I didn't know every time I watch it.

Avatar

You missed the point of the percentages, since you are rolling out a feature you want to set an initial set of users that are allowed to use that feature, then as you see it is working well you would increase the percentage. This way the original users who were included in the feature would not suddenly see it go away, and would be included as you increased the percentage.

Avatar

Make sure that if you are using devise with a locale field like so:

User Locale
      I18n.locale = current_user.locale 

It important to check if current_user is nil before setting the locale. If you're using devise, placing this code in your application controller without checking will cause not-logged in user's pages to crash.

Avatar

I've just spend about an hour to find that out - finally, thanks to your hint, I did^^

Avatar

Thanks for tutorial. I did infinite scrolling for my Art Asian site. However, when I go to a product page, then hit back button, it not keep same content and position. Is there any plugin to do that same as Pinterest or Polyvore

Avatar

I just thought it's worth noting that the API has changed and rightly so, since the implementation of asynchronous calls by bang method clashed in projects that have used bang methods heavily .

https://github.com/celluloid/celluloid/commit/946eeb81

ruby
r.launch! ; r.launch! ; r.launch!
# becomes
r.async (:launch) ; r.async (:launch); r.async (:launch)
Avatar

I am humbled by how simple the implementation ended up to add some very complex functionality, simply invoked I think that this is the best episode you have on this site. I await your return, eagerly- don't Why out on us!

Avatar

Has anyone figured out a way to do this with accepts_nested_attributes_for

Avatar

Is this current best practice or is https://github.com/rails-api/active_model_serializers the way to go these days?

Avatar

I had the same issue. I found that I needed to do the # ssh setup steps before I attempted cap deploy:setup. In the video, the ssh is done after and appears to be convenience only but in the show notes its before.

Avatar

Para los que estan trabajando con la version 3.2.13 de Rails se debe modificar lo siguiente en el archivo "application.js"

Dice:
function remove_fields(link) {
$(link).previous("input[type=hidden]").value = "1";
$(link).up(".fields").hide();
}

Debe decir:

function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}

Avatar

I'd also like to see an update, as I think the block/yield behaviour changed a lot since the video?

-- http://railscasts.com/episodes/208-erb-blocks-in-rails-3?view=asciicast

Avatar

I did try that but it didn't work and I found it difficult to figure out what was going wrong.. killed the droplet and started from scratch with an empty ubuntu image.

Avatar

Hey guys.
The server is not available (or you do not have permissions to access it)
this error always happens when I run any command related to god.
god status
god start
can someone help me?
Thanks

Avatar

I think there should be a revision that uses Capistrano 3 instead, as all the commands and config files have been updated

Avatar

Great question. I was just about to ask the same

Avatar

pls how do i use this to create a profile for my users....email me @ dehinde007@gmail.com