RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I am getting this when I do cap deploy:setup or bundle exec cap deploy:setup

I am not quite familiar with the scripts used on the railscast, mainly copy and paste/augment path (insert username/projectname) on the root path. I suspect that I configured the scripts incorrectly?

bundle exec cap deploy:setup
triggering load callbacks
* 2013-09-06 01:21:56 executing `deploy:setup'
* executing "mkdir -p /home/deployer2/apps/lintong /home/deployer2/apps/lintong/releases /home/deployer2/apps/lintong/shared /home/deployer2/apps/lintong/shared/system /home/deployer2/apps/lintong/shared/log /home/deployer2/apps/lintong/shared/pids"
servers: ["192.241.241.204"]
[192.241.241.204] executing command
** [out :: 192.241.241.204] bash: /home/deployer2/.rvm/bin/rvm-shell: No such file or directory
command finished in 9ms
failed: "rvm_path=$HOME/.rvm $HOME/.rvm/bin/rvm-shell 'default' -c 'mkdir -p /home/deployer2/apps/lintong /home/deployer2/apps/lintong/releases /home/deployer2/apps/lintong/shared /home/deployer2/apps/lintong/shared/system /home/deployer2/apps/lintong/shared/log /home/deployer2/apps/lintong/shared/pids'" on 192.241.241.204

Avatar

@psingh: starting at 6:30, the cast explains that if you add "filter_resource_access" to a controller declaration, the gem will automatically take care of that for you.

Avatar

I am pasting my code here, it does the following
- downloads the file, not only make it in file system
- libre complaint, also windows, and browser

Even though I dont like to write and delete , but with spreadsheet seems like the only solution.

ruby
puts "# write the file
    
book.write "Employee_History_#{ params[:id]}.xls"
    
# send the file
    
send_file "Employee_History_#{ params[:id]}.xls", :type => "application/vnd.ms-excel", :filename => "data.xls", :stream => false
    
# and then delete the file
    
File.delete("Employee_History_#{ params[:id]}.xls")"
Avatar

I am pasting my code here, it does the following
- downloads the file, not only make it in file system
- libre complaint, also windows, and browser

Even though I dont like to write and delete , but with spreadsheet seems like the only solution.

ruby
puts "# write the file
    
book.write "Employee_History_#{ params[:id]}.xls"
    
# send the file
    
send_file "Employee_History_#{ params[:id]}.xls", :type => "application/vnd.ms-excel", :filename => "data.xls", :stream => false
    
# and then delete the file
    
File.delete("Employee_History_#{ params[:id]}.xls")"
Avatar

ya thanks sagescs

Now csv working fine but when i import Excel(xls) i get below error
invalid byte sequence in UTF-8

Avatar

for anyone that comes across it... the format that it needs to be in is just a string of ids, separated by commas.

ruby
incoming = ['Will E Makit', 'Betty Wont', 'Adolf Oliver Bush']
author_tokens: Author.where("name IN (?)", incoming).map{|c| c.id}.join(',')
Avatar

If I want to create a Book manually, how do I pass the Authors manually? An array doesn't seem to work. I'd basically like to do something like:

ruby
Book.create!(title: '10 yards to the Shitter', author_tokens: ['Will E Makit', 'Betty Wont'])

I need to call the create action from another model... and I can't seem to pass the author_tokens properly.

Avatar

It's an issue between roo gem and rubyzip gem.
You can fix it by adding

gem 'rubyzip', '~> 0.9.9'

to your Gemfile.

Avatar

Rails 4 docs give slightly different syntax:

accepts_nested_attributes_for :avatar, reject_if: proc { |attributes| attributes['name'].blank? }
Avatar

I'm wondering about the best way to handle very large data imports that can take minutes to parse... The app I'm working on requires handling potentially huge imports consisting of tens/hundreds of thousands of rows.
I'm thinking it makes sense to store the CSV temporarily on upload, and do all the parsing via cron... and somehow notify the user that the import is processing.
I'm pretty new to rails, and I'm not sure the best way to execute.
Anyone have a suggestion?

Avatar

hi all when i import csv or xls

i get below error

      cannot load such file -- zip/zipfilesystem
Avatar

hi all when i import csv or xls

i get below error

      cannot load such file -- zip/zipfilesystem

This error occurred while loading the following files:

zip/zipfilesystem

Avatar

I have the same question. Did you ever figure out a way to do this?

Avatar

I am trying to use geocoder gem to geocode address into latitude and longitude. However when I create a new record, it only records the address string but doesn't contain the latitude and longitude of the given address.
I am behind a proxy, but I have my proxy variable set.

What could be the problem here ?

Avatar

I have this working on my local iMac. When I upload to Heroku, the app gets this log when starting up "/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/relation/finder_methods.rb:310:in `find_with_ids': Couldn't find Tenant without an ID (ActiveRecord::RecordNotFound)"

Do I need to do anything special to run this on Heroku?

Avatar

I would have liked to see validations using AJAX. If the user tried to submit a blank task, I'd want the field highlighted and an error message to be displayed. I'll figure it out but it would have been nice to see the best practices related to this.

Avatar

Just in case anyone misreads this as I did, "I've found the after_touch method a good place to clear the cache as well" as in I found I need to use both after_commit and after_touch.

Great find on the after_touch callback!

after_commit doesn't get called if you are just touching the record, ie have nested caches that get invalidated through touching.

Avatar

This might be a very stupid question but unless I'm mistaken this approach requires pre-loading all the values for the dynamic select onto the client... I have too many for this and instead want to hit the server once for the relevant dynamic dropdown data once the primary selection has been made via AJAX.

Is this feasible?

Avatar

p.3 in this case should look like (here tags, etc. have polymorphic relations) :

class OrganizationsController < ApplicationController
def index
@organizations = Organization.all
respond_to do |format|
format.json { render :json => @organizations.to_json( :include => [:user, :tags, :emails, :phones] ) }
end
end
end

That's way angular will bind all of your nested attributes

Avatar

I am using:

ruby
CONFIG = YAML.load(File.read(File.join(File.dirname(__FILE__), 'application.yml')))[Rails.env]

It is one line not two and it does not use slashes for OS compatibility.

Avatar

Hello, Jason

To have access to your models in Angular you have to do:
1. Declare your model in rails .rb file (hope you've done with this)
2. Declare model as resource in route.rb (resources :your_model )
3. Pack it to JSON in Rails Controller
4. Create angular factory with next code
angular.module('GarageCRM.resources').factory 'Your_model', ($resource) ->
$resource 'your_model'

That's all. After configuring angular routes (it's pretty simple) you'll have access to any nested model or attribute.
This should help coderberry.me

Avatar

Changing the pasted in navigation code to this worked for me:

html
<nav class="top-bar">
  <ul class="title-area">
    <li class="name">
      <h1><%= link_to "Awesome Store", products_path %></h1>
    </li>
    <li class="toggle-topbar menu-icon"><a href="#"><span>menu</span></a></li>
  </ul>
  <section class="top-bar-section">
    <ul class="right">
      <li class="divider"></li>
      <li><%= link_to "Browse Products", products_path %></li>
      <li class="divider"></li>
      <li><%= link_to "Price List" %></li>
      <li class="divider"></li>
      <li><%= link_to "Contact Us" %></li>
      <li class="divider"></li>
      <li><%= link_to "Cart" %></li>
    </ul>
  </section>
</nav>
Avatar

I'm seeing the same thing. The right navigation elements disappear when the page is narrow.

Avatar

I had already read the CodeClimate blog entry about fat models. It is a great reference for OO refactoring, but lacks of examples on how to organize the code the Rails way. This RailsCast just covered all things that weren't clear for me. Thanks, Ryan!

Rafael Barbolo from Loja Virtual Kauplus

Avatar

+1 for Teaspoon - just a great tool for testing JavaScript - kudos to @Jeremy.

Avatar

Everything works up until I switch the source to anything non-local (source: ['foo', 'food', 'four'] works fine).

This is the error I'm seeing in the console while typing for non-local data:

Uncaught TypeError: Property 'source' of object [object Object] is not a function

Any suggestions? My code is almost identical to the screencast.

Avatar

I see render @incompletetasks, but I didn't see the _incomplete_task.html.erb ? I'm using Rails 3.2.13 and give me missing template error==

Avatar

Hey Ryan, have you had a chance to reasess your opinion of ActionController::Live since first releasing this cast?

Avatar

Please explain how is that the best practice

Avatar

Thanks for the update lamont. I agree, things have changed and it's hard to find good documentation. I'm interested in learning how to use berkshelf and chef-solo together but can't find a good tutorial. Can you recommend one?

Avatar

For eyample, given your app runs on example.com:80, you could serve faye via proxying from a different vHost (faye.example.com:80 -> localhost:9292).

Avatar

I have a document that has spanish characters, accents and ñ. Is there way ensure that these are exported correctly? Or better put, how can I change the encoding to 'Latin-1'?

Avatar
def create
  @album = Album.find(params[:album_id])
  params[:photo][:user_id] = current_user.id
  @photo = @album.photos.build(params[:photo])
  @photo.save
end

how do I capture params[:photo][:name]?

Avatar

for the user_id, you can do params[:photo][:user_id] = current_user.id or something like that. I'm still trying to figure out how to capture the photo name...

Avatar

any further notes on this? I added 'content-type' => 'file' as a placeholder, and added the data.form.find('#content-type').val(file.type) to the coffee file above the data.submit()... nada. Images stop uploading.

but I, too, am getting the application/octet-stream warning...

Avatar

Did you figure out how to do this?

Avatar

Thanks so much. This saved me a lot of time.

Avatar

I am only able to import the first column of the csv file Any suggestions?

Avatar

Not sure if you figured it out, but just ran into the same problem.

in the strong_params method, you must make tags an array

ruby
def article_params
  params.require(:article).permit(:name, :content, :published_at, tags: [])
end
Avatar

What if you set config.action_dispatch.tld_length = 2 for herokuapp hosted sites, such as staging.rb? For production, I'd assume you won't use herokuapp.com so tld_length = 1 still works?

Avatar

Yes, you can create a method in Country model that sorts the states by country_name and states_name (execute a query sql), and later call it in your form.

Example: if your new method is called "states_by_country_name_and_state_name"

in the form

/app/views/people/new.html.erb
<%= f.grouped_collection_select :state_id, Country.order(:name), :states_by_country_name_and_state_name, :name, :id, :name, include_blank: true %>
Avatar

FYI - JasmineRice repo 2013/1/17 states:

Brad Phelan is no longer actively developing this project but is accepting reasonable pull requests, so continue on with contributing.

This project is looking for a home :)

Avatar

I'm new to ruby and need some guidance with a task. I have to import data from an excel spreadsheet. That currently works fine. Now I need to add new columns to the spreadsheet and those new columns belong to seperate tables in the database. So, first I need to look-up the foreign key of the value from excel and add it to that respective table. In total when the spreadsheet is imported I am dealing with three tables. How do I go about doing that? Thank you in advance for any guidance, help or examples.

Avatar

I came here to share the same fix as given by others. I ended up renaming my authorize method to "check_authorization" and renamed my allow, allow? etc... methods to authorize, authorize? etc...

Avatar

Ok, so I downloaded the source code, and when a survey is updated, the params are this:

{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"yO16xiTDvjH1i4mvgUSK3XiDxiyzHM87PSGZAc57Cu4=",
"survey"=>{"name"=>"Rails Survey",
"questions_attributes"=>{"0"=>{"content"=>"kjhlkjh",
"_destroy"=>"0",
"answers_attributes"=>{"0"=>{"content"=>"kjlk",
"_destroy"=>"false",
"id"=>"2"}},
"id"=>"2"}}},
"commit"=>"Update Survey",
"id"=>"1"}

But I created a rails app from scratch following line by line in the railscast and I get an arror "Cannot convert Symbol into Integer" and my params are slightly different when updating a survey:

{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"I4IByp+jkutbWSHRM8z5zvLoKQ1AxfoyNhdMxJ02ORg=",
"survey"=>{"name"=>"Test 1",
"questions_attributes"=>{"content"=>""}},
"commit"=>"Update Survey",
"id"=>"1"}

So I have a feeling it has to do with the "0" that is before the "content" of "questions_attributes" in the source code params but not in my params. What am I doing wrong and how can I fix it?

Avatar

yeah I would love to find that out too. So far I have been able to test with a rake task by itself but would love to just test everything locally before I deploy