RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Hey Wayne,

The "states.csv" and "countries.csv" data files are available in the github hosted "Source Code" under the "Show Notes" header:

https://github.com/railscasts/088-dynamic-select-menus-revised

By using the "db/seeds.rb" file you can migrate the data into any rails app.

bundle exec rake db:seeds

I hope this helps for future use.

Avatar

I'm having trouble with the remove link in production. The fieldset remove works however it still triggers validation even though it is no longer visible on the screen. Am i missing a step?

Avatar

gracias vsolari, lo que realmente me ayudó.

Avatar

Thanks for this one more time amazing railscast !

Avatar

I ran into the same issue. Had to add the following strong params to the books_controller

ruby
def book_params
      params.require(:book).permit(:name, { :author_ids => [] })
end
Avatar

Here is a comparison of different ways to get started making an AngularJS app including the example from this railscast

http://www.dancancro.com/comparison-of-angularjs-application-starters/

Avatar

such impress. very thanks.

I think this is one of the very few resources out there that explains how rails is constructed. And it does so in the Ryan Bates's concise-yet-thorough style.

Having this primer will save so much time reading through the rails source code.

Avatar

works fantastic with Kaminari AND Rails 4 with turbolinks. even after so many years. Thanks Ryan!

Tip: If you're using Kaminari, just add class="next_page" to the next page link in the Kaminari view

<li class="next_page">
  <%= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, :rel => 'next', :remote => remote %>
</li>
Avatar

Wow. There is a lot of JS code in there.
Easily you can use just it:

$(document).ready(function() {
$('#example').dataTable();
});

Where #example is the ID of the table.
Remember to use dataTable as a class, and to read the documentantion:

https://datatables.net/

Avatar

How would you implement chosen or select2 if your has_many through join table is not using the default id as a primary key? Is the syntax author_ids something rails handles automagically in the standard join table?

Avatar

Hi Ryan...This tutorial is really AWESOME..I'm just a newbie in rails but I never had a hard time understanding your tutorials...I have a question, Can I automate the rake task that index the search suggestions by event or by time?..Thanks in advance..

Avatar

I didn't get it working with just Capistrano 3, because Capistrano doesn't want to use the sudo command a lot, and now recommends passwordless sudo. However, I made it work by provisioning with Sunzi. I made a how to video you can follow.

https://www.youtube.com/watch?v=S1iTmfnKS7Q

Avatar

let(:comment) { FactoryGirl.create(:comment, user: user, commentable: question) }

subject { comment }

it { should respond_to(:commentable) }

Avatar

Agree! It would be awesome! Been trying to setup Elasticsearch with the new Elasticsearch rails gem, but not finding enough/clear documentation on how to migrate from Tire. Railscasts are awesome!

Avatar

I've done the nested form by referencing to http://railscasts.com/episodes/196-nested-model-form-revised?

I can fill three models' data in one form now,

But How to saving the form data and make it available to re-fill the form ?

The relations of the models is 'One survey has many questions,and one question has many answers,'

My roughly thinking is, save all the survey_params in surveys_controller.rb

Maybe I should create a new model FormRecord to keep all the form records,

And when user comes to the form-page next time,

it'll selecting all the records he made,
and provide a checkbox and button to let him decide which form-data record he wants to fill the form.

But I have no idea what kind of the data structure I should have,

And How to fill the form via Javascript.

Is there any idea ? or reference ?

Avatar

I followed the tutorial of Nested Model Form (revised) http://railscasts.com/episodes/196-nested-model-form-revised?view=comments

Which is about how to create a survey with multiple models,

But I wonder how to make to survey cab be a real survey.

Let user can answer those questions and I can gather the user's feedback.

Avatar

Please, help srinu4122. I have same question)

Avatar

When I try to generate the access token in the irb console, I get "DNS lookup failed error". Does this have to do with some problem in my machine setup?

ruby
irb(main):021:0> access = client.auth_code.get_token("6246e2d200c46b338780e72c4db85f5225c097e907792c31f7acf9b07421fe7f", redirect_uri: callback)
OAuth2::Error: <!-- IE friendly error message walkround.        
     if error message from server is less than   
     512 bytes IE v5+ will use its own error     
     message instead of the one returned by      
     server.                                 --> 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><style type="text/css">html,body{height:100%;padding:0;margin:0;}.oc{display:table;width:100%;height:100%;}.ic{display:table-cell;vertical-align:middle;height:100%;}div.msg{display:block;border:1px solid #30c;padding:0;width:500px;font-family:helvetica,sans-serif;margin:10px auto;}h1{font-weight:bold;color:#fff;font-size:14px;margin:0;padding:2px;text-align:center;background: #30c;}p{font-size:12px;margin:15px auto;width:75%;font-family:helvetica,sans-serif;text-align:left;}</style><title>504 DNS look up failed</title></head><body><div class="oc"><div class="ic"><div class="msg"><h1>504 DNS look up failed</h1><p>The webserver for http://localhost reported that an error occurred while trying to access the website.  Please click <u><a href="javascript:history.back()">here</a></u> to return to the previous page.</p></div></div></div></body></html>

        from /home/kempa/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/oauth2-0.9.3/lib/oauth2/client.rb:110:in `request'
        from /home/kempa/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/oauth2-0.9.3/lib/oauth2/client.rb:135:in `get_token'
        from /home/kempa/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/oauth2-0.9.3/lib/oauth2/strategy/auth_code.rb:29:in `get_token'
        from (irb):21
        from /home/kempa/.rbenv/versions/2.0.0-p451/bin/irb:12:in `<main>'
irb(main):022:0> 
Avatar

P.S.

My code is a little different:

First, I got carrierwave working as per 253-carrierwave-file-uploads

then..

photos.js.coffee
jQuery ->
  $('#new_photo').fileupload
    dataType: "script"
    add: (e, data) ->
      types = /(\.|\/)(gif|jpe?g|png)$/i
      file = data.files[0]
      if types.test(file.type) || types.test(file.name)
        data.context = $(tmpl("template-upload", file).trim()) if $('#template-upload').length > 0
        $('#new_photo').append(data.context)
        data.submit()
      else
        alert("#{file.name} is not a gif, jpeg, or png image file")
    progress: (e, data) ->
      if data.context
        progress = parseInt(data.loaded / data.total * 100, 10)
        data.context.find('.progress-bar').css('width', progress + '%')
_form_uploader.html.erb
<%= simple_form_for [@gallery, @photo], :html => { :class => 'form-vertical' } do |f| %>
        <h3>Upload Photos Here</h3>
        <p>You can select multiple files.</p>
        <%= file_field_tag(:image, multiple: true, name: "photo[image]") %>                
<% end %>

<script id="template-upload" type="text/x-tmpl">
<div class="upload">
  {%=o.name%}
  <div class="progress progress-striped"><div class="progress-bar" style="width: 0%"></div></div>
</div>
</script>

<div id="uploads">
        <%= render 'uploads' %>
</div>
_uploads.html.erb
<ul id="photos" class="inline"><%= render @photos %></ul>
_photo.html.erb
<% if photo.id.present? %><li class="no-margin-padding pull-left"><a href="<%= photo.image_url() %>"><%= image_tag(photo.image_url(:icon)) if photo.image? %></a></li><% end %>
photos_controller.rb
  def create
    #on Gallery using to_param with 'auth_tokens' for securing user interface
    @gallery = Gallery.where("auth_token = ?", params[:id].to_s).first
    @photo = @gallery.photos.create(photo_params)
    respond_to do |format|
      format.html { redirect_to :back, notice: "Thank you for uploading" }
      format.js
      format.json { render json: [@photo.to_jq_upload].to_json, status: :created, location: @photo }
    end
  end
application.css
 *= require jquery.fileupload-ui
application.js
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery-fileupload/basic
//= require jquery-fileupload/vendor/tmpl
//= require jquery.fileupload-process
//= require photos
Gemfile
gem 'jquery-fileupload-rails'
gem "jquery-ui-rails"
Avatar

@quindici can you please indicate what the error was, i am getting the same error. Thanks

Avatar

Hey All,

Bootstrap 3 modifications:

paintings.js.coffee
from:

data.context.find('.bar').css('width', progress + '%')

to:

data.context.find('.progress-bar').css('width', progress + '%')

And then

index.html.erb
from:

<div class="progress"><div class="bar" style="width: 0%"></div></div>

to:

<div class="progress progress-striped"><div class="progress-bar" style="width: 0%"></div></div>

I hope this helps. I f'd around with this for hours install all new css, js files before it dawned on me to update the bootstrap css.

To Your Success!!! Mark

Avatar

I had this issue. I think it had to do with a file I didn't have permissions to. Also do a search for "using rvm with god" if you are doing that.

Avatar

Thank you so much! The issue was pg using different query syntax. good to know!

I'm still having an issue where the existing tags don't show up when I go to the edit page.

Avatar

Would you also need a comma after 'facebook'? If not, why?

Avatar

You can use:

ruby
  def self.search(search)
    if search
      #case insensitive
      where('email ILIKE ?', "%#{search}%")
    else
      scoped
    end
  end
Avatar

Thank you so much, I've been looking for doing this with has_many :through for so long. I'm trying to work this with autocomplete as well. It's surprising how so few results there are for "has many through autocomplete" in a search.

Avatar

So far we've been starting Delayed Job by running rake jobs:work but in production we should use the delayed_job script provided in the script folder.

The reason why is explained here https://github.com/collectiveidea/delayed_job/issues/659.

Avatar

Very useful, thanks. I also borrowed some code from this answer on Stack Overflow to make the updates lazier.

Avatar

If you are going to use :histroy with friendly_id 5 you should consider the new change in slugs' regenrations. As version 5 the slugs are not updated on save unless you set them to nill first.

Updating a product
  def update
    @product.slug = nil
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end
Avatar

How can use the gem with ransack ?

At trying to build a search_form_for with the owner_name_cont, I keep getting the error:

uninitialized constant PublicActivity::ORM::ActiveRecord::Activity::Owner

Avatar

Thanks that worked for me too. (Rails 4.1.x)

Avatar

Ryan, this screencast is very clear! I need to know if work with custom daemons: https://github.com/mirasrael/daemons-rails is the "best" solution for synchronize a db in an app that read/write external hardware memory (Programmable Logic Controller). It is about a long running task (maybe years) and it is desirable not to break the execution at all along - ..... Or can I consider other solution that fit in this types of problems?
Thank You...

Avatar

if you have a lot of records you're going to use paginations, you're most likely going to have more records than you want to show on one page, therefore client side is useless.

Avatar

Hi, I know, that this is a bit old question. Anyway for everyone who chalanged this problem, you can use .includes(:trackable), like with any other association.

Avatar

Trying to implement this in a Rails 3 app. Anyone know how to translate the following route for Rails 3:

map.current_cart 'cart', :controller => 'carts', :action => 'show', :id => 'current'

Thanks!

Avatar

Where you able to fix this problem? If so, can you let me know what the bug is?