RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I was able to figure out my issue from 'vi /var/log/nginx/error.log'

Avatar

In Rails 4, this:

ruby
Product.update_all({discontinued: true}, {id: params[:product_ids]})

should be:

ruby
Product.where(id: params[:product_ids]).update_all(discontinued: true)
Avatar

Mike, see my question below...I am doing a similar thing and I definitely get a clear flash when the page is reloaded or re-rendered...

Avatar

Hi all. I encountered an issue where the CSS provided in this episode wasn't highlighting the selected search term.

I dug around and found out that for some reason JQueryUI changed the class of "active" search items from a.ui-state-active to a.ui-state-focus. If you make that change to Ryan's CSS, the example works again.

Avatar

starflyer59 -

I did get it to work with this javascript added to the end of mercury.js:

javascript
jQuery(window).on('mercury:ready', function() {
   var link = $('#edit_link');
   Mercury.saveUrl =link.attr('data-save-url');
   link.hide();
});
Avatar

Great episode, again :)

Oh by the way, is it my imagination or is there absolutely no flashing when you click "discontinue checked" in solution 1? I always get flashing when I do this stuff - unless I am using ajax-y logic. Anyone?

Avatar

Since he's editing multiple records, the operation is on the products collection, or at least a subset thereof.

It also means the url helper (discontinue_products_path) will have the plural form which seems appropriate. YMMV.

Avatar

What am I missing? I cannot have a hour and min field convert to a duration model field.

ruby
  attr_accessible :date, :hour_dur, :min_dur
  attr_accessor :hour_dur, :min_dur
  has_many :shifts
  has_many :seats
  belongs_to :user
  
  before_save :update_duration

  validates_presence_of :date
  validates_presence_of :duration

  def update_duration
    duration = hour_dur*60 + min_dur
  end
  
  def hour_dur
    duration / 60 if duration.present?
  end
  
  def min_dur
    duration % 60 if duration.present?
  end
end
Avatar

Agree or disagree.

Putting an edit form on an index page (as he does in the video) kind of makes Rails work like a javascript single page app. It seems to load and present the updates much faster, in addition to not having to redirect to a different page

Avatar

Why did he make it a "collection type route" in the first rewrite of the edits (at 1:52 of the video)?

ruby
resources :products do
  collection do
    put :discontinue
  end
end

i.e. did he have to do it that way? What would be alternative ways to do it? why is this way better? Thanks if you can help.

Avatar

Thanks - saved me some time this afternoon!

Avatar

I came to this cast by looking for benchmark tests with rspec, and I was wondering if a chapter on rspec testing and benchmarking apis would be in your plans, if not then I guess this is a suggestion...

Awesome screencasts as always, thank you!

Avatar

Are you using nginx with Trinidad? If so, you would set up the SSL on nginx and ensure that external traffic is not accepted on the port that nginx is forwarding traffic to Trinidad. On ubuntu, you would do this through ufw or iptables.

Traffic between nginx and trinidad would be loopback, so you shouldn't need an SSL unless you are forwarding to a separate server.

Avatar

Wow, great episode. Wanted to know more about that new feature for a while. Thanks!

Avatar

Did you ever get an answer to this? I have the same situation.
Thanks.

Avatar

Firstly am new to this so if anything is wrong, apologies and please correct it!

If you use application.js as is written in the example, no ajax will happen... (it would of worked when it was first written).

This is due to the 'live' event being replced with 'on' in the current version of jquery.

However live, used to stay bound to all future nodes. 'on' by default does not.

Thus if you just change 'live' to 'on' it will work once (for one click per page), and then fallback to non ajax.

We also need change how the line is written to include the nodes to stay bound to:

  • $(document).on("click","#game_classes th a, #game_classes .pagination a", function() {* would replace the line used in the original:

I also believe we now need the lines in application.js that load other documents (// = require somthing).

Thus my complete application.js looks like this:

ruby...
//= require jquery
//= require jquery_ujs
//= require_tree .
$(function() {
$(document).on("click","#game_classes th a, #game_classes .pagination a", function() {
$.getScript(this.href);
return false;
});
$("#game_classes_search input").keyup(function() {
$.get($("#game_classes_search").attr("action"), $("#game_classes_search").serialize(), null, "script");
return false;
});
});
...

Hopefully this helps someone!

ps: this applied on 25/02/2013, rails 3.2
pps: this can also be done with coffeescript , in the relevant file! (products.js.coffee in the example given)

Avatar

Also getting this error ... and it used to work, so I suppose an update broke my code. Anyone knows what can fix it?

Avatar

Hi

Can someone help me here. I am new with rails and trying to dynamically add fixed number of pre-populated nested forms using cocoon. Please be kind enough to read more details here.

http://stackoverflow.com/questions/15072932/dynamically-adding-fixed-number-of-prepopulated-nested-forms

Thanks a lot!!

Avatar

Actually, I found that the problem is that I have the edit form dynamically generated via ajax, so that the ".fileupload" can't pick up the form. "$('form').on 'fileupload', '#edit_painting', (event)->" doesn't do the trick.. How to bind custom functions to dynamically loaded elements?

Avatar

I have a question: I cannot get this to work for the 'update' action. 'new' works fine, but I use the same code for 'update' and I always get a redirect with a "Template is missing" error for there's no 'update.html.erb', only 'update.js.erb', which has the same code as 'create.js.erb'.

If I add in the "respond_to" block with "format.js", it won't act out what's on 'create.js.erb' but rather do a full reload and shows the updated picture.

How can we get the same ajax upload to work with the 'update' action?

Avatar

I had the same issue. Here's how to solve it:

The //= require rails.validations

must be done before the //= require_tree

http://www.ddarrensmith.com/blog/2012/05/17/ruby-on-rails-client-side-validation-with-validation-helpers-and-twitter-bootstrap/

Avatar

remember this in user model

, :omniauth_providers => [:twitter]

Avatar

Hi guys, some of you can help me?
I have made all the video tutorial Rails Authentication Form Scratch Revised full.
But when i try to login with a email and one passowrd seems that my if statement has not evaluated the user.authenticate(params[:password]) because its never began login, in fact it is always evaluated the else statement of my if statement

this is my source code: SessionsController

def new
end

def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password_digest])
session[:user_id] = user.id
redirect_to root_path, :notice => "Bienvenido a Alertab"
else
flash.now.alert = "Tu correo o clave es invalida, intenta de nuevo"
render "new"
end
end

def destroy
session[:user_id] = nil
redirect_to root_path, :notice => "Session cerrada"
end

And here are my view source code:

Inicia Sección
<%=form_tag login_path do%>

<%= label_tag "Correo" %>
<%= email_field_tag :email, params[:email] %>


<%= label_tag "contraseña" %>
<%= password_field_tag :password %>


<%= submit_tag "Inicia Sección"%>

<%end%>
<%= link_to raw("← Regresar"), root_path %>

And here are console response:

Started POST "/login" for 127.0.0.1 at 2013-02-24 09:50:26 -0500
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"3xavsOw949fhLAIj/ypGq4eNpsQ2x3TSEx7/JS/898s=", "email"=>"my@gmail.com", "password"=>"[FILTERED]", "commit"=>"Inicia Sección"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'my@gmail.com' LIMIT 1
Rendered sessions/new.html.erb within layouts/application (1.3ms)
Completed 200 OK in 13ms (Views: 11.7ms | ActiveRecord: 0.2ms)

Avatar

After watching this Railscast and researching a bunch of other articles online, I figured out a way to do it with all of the upsides and none of the downsides–you get a pretty URL with both ID and name, parameterized, fully functional in controllers/views, without having to add a slug field to the database or anywhere else. Pretty efficient, I think. This is it if anyone is interested:

https://gist.github.com/rceee/5021916

Avatar

Any reason somebody would recommend Chef over Puppet?

Avatar

put the below file 'import.rake' in lib/tasks
then 'rake import_hosts'

require 'csv'
task :import_hosts =>[:environment] do
file="db/tmp/hostssmall.csv"

additional_rows_to_skip = 1
CSV.foreach(file) do |row|
if additional_rows_to_skip > 0
additional_rows_to_skip-=1
else
p = Host.create!({
:ref => row[0],
:name => row[1],
:address => row[2],
}
)

end

end
end

Avatar

Hello
I have a dataTable with nested models. I use server side processing and all works fine (search on multiple columns, sorting).

Now I need to add multiple row selection and send selected objects ids to my controller.

But I can't implement the multiple row selection. I followed the example on "http://datatables.net/release-datatables/examples/server_side/select_rows.html". I changed my users.coffee.js file to users.js to not have to mess with CoffeeScript.

Now when the page loads I have an error "reference error : aSelected is not defined" in the "fnRowCallBack" function, I see the datas in the table and the 'processing indicator'...

Any help on this would be very appreciated
Cheers

By the way, the example provided in dataTables.net uses jQuery.live function which is obsolete since version 1.7, I changed it to '.on'

Avatar

I am unable to start sidekiq, it throws an error, "Timed out connecting to Redis on localhost:6379". My Redis server is running on port 6379. I am using mac os x 10.8.2.

Avatar

Argh!! Post something and then it becomes apparent...

scaffolds.css.scss is the last thing that generates content in the final served css...

Avatar

I've been using Bootstrap to build an e-commerce site, and have run into a font override issue that I cannot track down. Using Typekit fonts, I can easily override all the headings (h1-h6) by adding something like:

h1 {font-family: "proxima-nova",sans-serif;
font-style: normal;
font-weight: 300;}

at the bottom of the bootstrap_and_overrides.css.less file. This generates the proper css in the final .css file that is served from the production server. These overrides are near, but not at, the bottom of the served .css file even though they are at the bottom of the bootstrap_and_overrides.css.less file. While overriding the headings works, I cannot override the body font. Using:

body {font-family: "proxima-nova",sans-serif;
font-style: normal;
font-weight: 300;}

generates the proper css near the bottom of the served .css file. Unfortunately, something else is adding a further body font-family override after thebootstrap_and_overrides.css.less override. There are 3 body font definitions in the served css; default Helvetica, followed by the "proximal-nova" assignment, which is then subsequently overridden by a Ventana assignment.

Any ideas where the Ventana assignment is coming from when using the twitter-bootstrap-rails gem? I've got the active admin gem bundled, but it appears to be doing styling on only active admin specific elements.

Avatar

Why don't you use the include method to eager load the categories ? I think that would be a better fit than the caching you are showing. Great vid, though!

Avatar

I just wrote a blog post about what to watch out for with Rails 4.0 Turbolinks: Web Dev: Gotchas With Rails 4.0 Turbolinks

Avatar

I don't know if this is your case, but that happened to me and i didn't have nothing on port 80... it was solved by commenting the listen [::]:80 default_server line on /etc/nginx/sites-enabled/default... hope it helps

Avatar

Thanks Ryan for great post. This railscast give me the most valuable 9$ per month :)

Avatar

Angular and Backbone differ in a few ways. Backbone is more of a low-level library, where much of the implementation is up to the developer. Angular is a much more opinionated (like Rails) framework, and does a lot more for you out of the box. The big difference though is the data-binding.

Standard BB applications usually involve stamping out templates, and reacting to changes on the model is very much a manual process. Of course there are plugins to simplify this like Derick Bailey's backbone.modelbinding.

With angular, the emphasis is on enhancing HTML through directives - which allow you to bind your data (javascript objects and such) to the DOM. This is an immensely powerful concept, because you don't have to deal with listening for events or do DOM traversal. In turn, you can move much of your view logic into controllers which take care of gluing together the interactions. They are somewhat analogous to view models, which should be familiar to anyone who's worked with Knockout.js.

Avatar

I keep getting this error when trying to run the /etc/init.d/unicorn start script:

/usr/share/nginx/www/rails_apps/symbolfy/bin/unicorn:13:in `require': no such file to load -- rubygems (LoadError)
from /usr/share/nginx/www/rails_apps/symbolfy/bin/unicorn:13

But I don't have any trouble running:

bundle exec unicorn -c config/unicorn.rb -D

Can anyone help point me in the right direction?

Avatar

Remington,

have you found a solution?

I am just creating an admin user in create_schema method of tenant. Since admin table is there in every schema, you end up with schema (or tenant) specif admin(s). This is not too bad if you want to distribute the admin work to a team of support persons and distribute the load based on accounts/regions.

However, if you prefer to have one admin table for all schemas, then leave it in public and drop from tenant schema when you create a new tenant.