401 unauthorized when trying to save Backbone model to rails db - ruby-on-rails-3
I am trying to match a song (in BBone world) through Rails to the DB. I continually am getting a POST http://localhost:3000/songs 401 (Unauthorized) when trying to save in the BBone View. The rails console error is below. It appears that the parameter sent to Rails is duplicated in fields (data removed is below the Rails error, so to see to nesting), but would this prevent the data from getting saved, rather would it produce this error, as opposed to something more along the lines of 'too many fields'.
As far as being unauthorized, that doesn't make sense, since the user ID is getting populated.
Any thoughts?
using gem 'rails-backbone'
Backbone Model:
//filename: song.js
define([
'underscore',
'backbone',
'backbone/collections/components'
], function(_, Backbone, componentCollection) {
var songModel = Backbone.Model.extend({
paramRoot: 'song',
defaults: {
title: '',
content: '',
user: '',
components: componentCollection
},
initialize: function(){
}
});
return songModel;
});
Backbone View:
// Filename: views/songs/new_view
define([
'jquery',
'underscore',
'backbone',
// Pull in the Collection module from above
'backbone/collections/songsCollection',
// 'backbone/views/songs/a_song_view',
'text!backbone/templates/songs/new.html',
'app/dispatch',
'app/state'
], function($, _, Backbone, SongsCollection, songsTemplate, dispatch, state){
return Backbone.View.extend({
el: $('#songs'),
initialize: function(options){
//......
},
save: function(e){
e.preventDefault();
e.stopPropagation();
var JSONSong = JSON.stringify(this.model.toJSON());
this.model.unset("errors");
this.model.set({
title: $('#title').val(),
content: JSONSong,
user: $('#user').val()
});
return this.collection.create(this.model.toJSON(), {
success: function(song) {
console.log('saved!');
this.model = song;
return window.location.hash = "/" + this.model.id;
},
error: function(song, jqXHR) {
return this.model.set({
errors: $.parseJSON(jqXHR.responseText)
});
}
});
},
render: function(){
$(this.el).html('');
var compiledTemplate = _.template ( songsTemplate, this.model.toJSON());
$(this.el).html(compiledTemplate);
return this;
},
});
});
Rails Schema:
ActiveRecord::Schema.define(:version => 20130222200403) do
create_table "songs", :force => true do |t|
t.string "title"
t.string "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
Rails Console error:
Started POST "/songs" for 127.0.0.1 at 2013-02-25 16:45:56 -0500
Processing by SongsController#create as JSON
Parameters: {"title"=>"1234", "content"=>"{\"title\":\"1234\",\"content\":\"{\\\"title\\\":\\\"\\\",\\\"content\\\":\\\"\\\",\\\"user\\\":\\\"\\\",\\\"components\\\":[{\\\"label\\\":\\\"Snare\\\",\\\"img\\\":\\\"snare.png\\\",\\\"mute\\\":false,\\\"sample\\\":\\\"808_sd.m4a\\\",\\\"measures\\\":[{\\\"label\\\":\\\"0/4\\\",\\\"beats\\\":[{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false}],\\\"numberOfBeats\\\":0,\\\"divisions\\\":8}],\\\"active\\\":true,\\\"signature\\\":4,\\\"representation\\\":\\\"fraction\\\"},{\\\"label\\\":\\\"Hi Hat\\\",\\\"img\\\":\\\"hihat.png\\\",\\\"mute\\\":true,\\\"sample\\\":\\\"808_chh.m4a\\\",\\\"measures\\\":[{\\\"label\\\":\\\"0/4\\\",\\\"beats\\\":[{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false}],\\\"numberOfBeats\\\":0,\\\"divisions\\\":8}],\\\"active\\\":true,\\\"signature\\\":4,\\\"representation\\\":\\\"fraction\\\"},{\\\"label\\\":\\\"Kick Drum\\\",\\\"img\\\":\\\"kick.png\\\",\\\"mute\\\":true,\\\"sample\\\":\\\"808_bd.m4a\\\",\\\"measures\\\":[{\\\"label\\\":\\\"0/4\\\",\\\"beats\\\":[{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false}],\\\"numberOfBeats\\\":0,\\\"divisions\\\":8}],\\\"active\\\":false,\\\"signature\\\":4,\\\"representation\\\":\\\"fraction\\\"},{\\\"label\\\":\\\"Synth\\\",\\\"img\\\":\\\"synth.png\\\",\\\"mute\\\":true,\\\"sample\\\":\\\"ambass.mp3\\\",\\\"measures\\\":[{\\\"label\\\":\\\"0/4\\\",\\\"beats\\\":[{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false}],\\\"numberOfBeats\\\":0,\\\"divisions\\\":8}],\\\"active\\\":true,\\\"signature\\\":4,\\\"representation\\\":\\\"fraction\\\"}]}\",\"user\":\"1\",\"components\":[{\"label\":\"Snare\",\"img\":\"snare.png\",\"mute\":false,\"sample\":\"808_sd.m4a\",\"measures\":[{\"label\":\"0/4\",\"beats\":[{\"selected\":false},{\"selected\":false},{\"selected\":false},{\"selected\":false}],\"numberOfBeats\":0,\"divisions\":8}],\"active\":true,\"signature\":4,\"representation\":\"fraction\"},{\"label\":\"Hi Hat\",\"img\":\"hihat.png\",\"mute\":true,\"sample\":\"808_chh.m4a\",\"measures\":[{\"label\":\"0/4\",\"beats\":[{\"selected\":false},{\"selected\":false},{\"selected\":false},{\"selected\":false}],\"numberOfBeats\":0,\"divisions\":8}],\"active\":true,\"signature\":4,\"representation\":\"fraction\"},{\"label\":\"Kick Drum\",\"img\":\"kick.png\",\"mute\":true,\"sample\":\"808_bd.m4a\",\"measures\":[{\"label\":\"0/4\",\"beats\":[{\"selected\":false},{\"selected\":false},{\"selected\":false},{\"selected\":false}],\"numberOfBeats\":0,\"divisions\":8}],\"active\":false,\"signature\":4,\"representation\":\"fraction\"},{\"label\":\"Synth\",\"img\":\"synth.png\",\"mute\":true,\"sample\":\"ambass.mp3\",\"measures\":[{\"label\":\"0/4\",\"beats\":[{\"selected\":false},{\"selected\":false},{\"selected\":false},{\"selected\":false}],\"numberOfBeats\":0,\"divisions\":8}],\"active\":true,\"signature\":4,\"representation\":\"fraction\"}]}", "user"=>"1", "components"=>[{"label"=>"Snare", "img"=>"snare.png", "mute"=>false, "sample"=>"808_sd.m4a", "measures"=>[{"label"=>"0/4", "beats"=>[{"selected"=>false}, {"selected"=>false}, {"selected"=>false}, {"selected"=>false}], "numberOfBeats"=>0, "divisions"=>8}], "active"=>true, "signature"=>4, "representation"=>"fraction"}, {"label"=>"Hi Hat", "img"=>"hihat.png", "mute"=>true, "sample"=>"808_chh.m4a", "measures"=>[{"label"=>"0/4", "beats"=>[{"selected"=>false}, {"selected"=>false}, {"selected"=>false}, {"selected"=>false}], "numberOfBeats"=>0, "divisions"=>8}], "active"=>true, "signature"=>4, "representation"=>"fraction"}, {"label"=>"Kick Drum", "img"=>"kick.png", "mute"=>true, "sample"=>"808_bd.m4a", "measures"=>[{"label"=>"0/4", "beats"=>[{"selected"=>false}, {"selected"=>false}, {"selected"=>false}, {"selected"=>false}], "numberOfBeats"=>0, "divisions"=>8}], "active"=>false, "signature"=>4, "representation"=>"fraction"}, {"label"=>"Synth", "img"=>"synth.png", "mute"=>true, "sample"=>"ambass.mp3", "measures"=>[{"label"=>"0/4", "beats"=>[{"selected"=>false}, {"selected"=>false}, {"selected"=>false}, {"selected"=>false}], "numberOfBeats"=>0, "divisions"=>8}], "active"=>true, "signature"=>4, "representation"=>"fraction"}], "song"=>{"title"=>"1234", "content"=>"{\"title\":\"1234\",\"content\":\"{\\\"title\\\":\\\"\\\",\\\"content\\\":\\\"\\\",\\\"user\\\":\\\"\\\",\\\"components\\\":[{\\\"label\\\":\\\"Snare\\\",\\\"img\\\":\\\"snare.png\\\",\\\"mute\\\":false,\\\"sample\\\":\\\"808_sd.m4a\\\",\\\"measures\\\":[{\\\"label\\\":\\\"0/4\\\",\\\"beats\\\":[{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false}],\\\"numberOfBeats\\\":0,\\\"divisions\\\":8}],\\\"active\\\":true,\\\"signature\\\":4,\\\"representation\\\":\\\"fraction\\\"},{\\\"label\\\":\\\"Hi Hat\\\",\\\"img\\\":\\\"hihat.png\\\",\\\"mute\\\":true,\\\"sample\\\":\\\"808_chh.m4a\\\",\\\"measures\\\":[{\\\"label\\\":\\\"0/4\\\",\\\"beats\\\":[{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false}],\\\"numberOfBeats\\\":0,\\\"divisions\\\":8}],\\\"active\\\":true,\\\"signature\\\":4,\\\"representation\\\":\\\"fraction\\\"},{\\\"label\\\":\\\"Kick Drum\\\",\\\"img\\\":\\\"kick.png\\\",\\\"mute\\\":true,\\\"sample\\\":\\\"808_bd.m4a\\\",\\\"measures\\\":[{\\\"label\\\":\\\"0/4\\\",\\\"beats\\\":[{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false}],\\\"numberOfBeats\\\":0,\\\"divisions\\\":8}],\\\"active\\\":false,\\\"signature\\\":4,\\\"representation\\\":\\\"fraction\\\"},{\\\"label\\\":\\\"Synth\\\",\\\"img\\\":\\\"synth.png\\\",\\\"mute\\\":true,\\\"sample\\\":\\\"ambass.mp3\\\",\\\"measures\\\":[{\\\"label\\\":\\\"0/4\\\",\\\"beats\\\":[{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false},{\\\"selected\\\":false}],\\\"numberOfBeats\\\":0,\\\"divisions\\\":8}],\\\"active\\\":true,\\\"signature\\\":4,\\\"representation\\\":\\\"fraction\\\"}]}\",\"user\":\"1\",\"components\":[{\"label\":\"Snare\",\"img\":\"snare.png\",\"mute\":false,\"sample\":\"808_sd.m4a\",\"measures\":[{\"label\":\"0/4\",\"beats\":[{\"selected\":false},{\"selected\":false},{\"selected\":false},{\"selected\":false}],\"numberOfBeats\":0,\"divisions\":8}],\"active\":true,\"signature\":4,\"representation\":\"fraction\"},{\"label\":\"Hi Hat\",\"img\":\"hihat.png\",\"mute\":true,\"sample\":\"808_chh.m4a\",\"measures\":[{\"label\":\"0/4\",\"beats\":[{\"selected\":false},{\"selected\":false},{\"selected\":false},{\"selected\":false}],\"numberOfBeats\":0,\"divisions\":8}],\"active\":true,\"signature\":4,\"representation\":\"fraction\"},{\"label\":\"Kick Drum\",\"img\":\"kick.png\",\"mute\":true,\"sample\":\"808_bd.m4a\",\"measures\":[{\"label\":\"0/4\",\"beats\":[{\"selected\":false},{\"selected\":false},{\"selected\":false},{\"selected\":false}],\"numberOfBeats\":0,\"divisions\":8}],\"active\":false,\"signature\":4,\"representation\":\"fraction\"},{\"label\":\"Synth\",\"img\":\"synth.png\",\"mute\":true,\"sample\":\"ambass.mp3\",\"measures\":[{\"label\":\"0/4\",\"beats\":[{\"selected\":false},{\"selected\":false},{\"selected\":false},{\"selected\":false}],\"numberOfBeats\":0,\"divisions\":8}],\"active\":true,\"signature\":4,\"representation\":\"fraction\"}]}"}}
WARNING: Can't verify CSRF token authenticity
Completed 401 Unauthorized in 1ms
Parameter with all the content removed:
Parameters: {"title"=>"1234", "content"=>"{\"title\":\"1234\",\"content\":\"{}", "user"=>"1", "components"=>[], "song"=>{"title"=>"1234", "content"=>"{}"}}
I attached the CSRF param and token to the model to send to the database.
//To pass the variable safely in from BBone to Rails 3.2, you have to include the csrf param and token
toBeSavedSong.set($("meta[name=csrf-param]").attr('content'), $("meta[name=csrf-token]").attr('content'));
The CSRF token should be included in the ajax request to have this successful.
Solutions for you:
Make sure your page has CSRF token in the "" section. If not, it could be because you miss a tag in your application.html.erb layout file. There should be "<%= csrf_meta_tags %>" in the layout
<!DOCTYPE html>
<html>
<head>
<title>Site name</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
Check your jQuery(or other library used by backbone). Make sure it is updated. I believe jQuery should include CSRF token in Ajax request
If both don't work for you, you can disable CSRF token check(which has CSRF security issue if you are doing a public website). Disable it by delete this line in apps/controller/application_controller.rb
protect_from_forgery
Related
Rails PG::Error Presentation and Handling of Unique Constraint
I am developing on Rails 4 with postgres and devise. I have the following User model: # user model class User < ActiveRecord::Base extend FriendlyId friendly_id :username, :use => :slugged rolify devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable validates :username, :presence => true, :uniqueness => {:case_sensitive => false}, :length => { :minimum => 3 }, :format => { :with => /\A[A-Z0-9a-z\w\b\ \-\_\'\!&##\.]+\z/i, :message => "may contain only alphanumeric characters and common special characters." } validates :email, :uniqueness => {:case_sensitive => false}, :presence => true, :format => { :with => Devise.email_regexp, :message => "isn't valid"} validates :password, length: { in: 6..128 }, on: :create validates :password, length: { in: 6..128 }, on: :update, allow_blank: true validates :slug, :presence => true end # in schema create_table "users", force: true do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0 t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" t.string "username" t.string "slug" end add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true add_index "users", ["slug"], name: "index_users_on_slug", unique: true add_index "users", ["username"], name: "index_users_on_username", unique: true The fields email and username are unique indices of the users table. My user registration view was created from devise. I create a test account, for instance, with username "test", email "test#example.com", password "123456". I sign out and try to sign up with the same information. I am expecting that the :uniqueness validations to trigger and be rendered on a list of errors alongside the signup form, but instead I get a full-page error with: ERROR: duplicate key value violates unique constraint "index_users_on_email" DETAIL: Key (email)=(test#example.com) already exists. How can I allow this error to bubble up to Rails and be shown as friendly single-line error like "Email is already registered with an account" alongside the sign up form instead of being caught with a big Rails error page?
I've identified the problem. The plugin friendly_id turned out not so friendly. It breaks uniqueness validation and results in 500 internal server errors. I have removed it from my application. Consider using something like ActsAsUrl in the Stringex library. I have removed extend FriendlyId friendly_id :username_copy, :use => :slugged and am now using acts_as_url :username, :sync_url => true, :url_attribute => :slug to generate friendly urls.
rails add_column with default value but the default value don't take effect
My rails version is 3.2.8 and use the default database. This is my migration code: class AddQuantityToLineItem < ActiveRecord::Migration def change add_column :line_items, :quantity, :integer,:default=>1 end end I find a explaination about :default option here and as it said,when i create a new LineItem ,it should have a default quantity=1,but here is what i get from rails console: lineb=LineItem.new #<LineItem id: nil, product_id: nil, cart_id: nil, created_at: nil, updated_at: nil, quantity: nil> And when i get LineItem from the database,the quantity field is nil too. And here is the db/schema.rb : ActiveRecord::Schema.define(:version => 20121008065102) do create_table "carts", :force => true do |t| t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end create_table "line_items", :force => true do |t| t.integer "product_id" t.integer "cart_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.integer "quantity" end create_table "products", :force => true do |t| t.string "title" t.text "description" t.string "image_url" t.decimal "price", :precision => 8, :scale => 2 t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end end
Your migration should work fine. Based on your schema though it looks like it hasn't actually taken effect, since t.integer "quantity" has no default value. The line in the schema for quantity should look like this: t.integer "quantity", :default => 1 Make sure you have actually run your migration (bundle exec rake db:migrate), and if that doesn't work then rollback (bundle exec rake db:rollback) and run the migration again (as #surase.prasad suggested).
Why is the user_id not automatically added upon creation of a comment?
I have a field that creates a comment (named pcomment). I am trying to get it to automatically add the user_id to the pcomment in the pcomment table like it adds the purchase_id automatically. I am not sure why the purchase_id is being recorded in the database but the user_id remains blank for each pcomment. Here is the form for the pcomment. <%= form_for([purchase, purchase.pcomments.build], :html => { :id => "blah_form" }) do |f| %> <div class="field"> <h4>What deal are you offering?</h4> <%= f.text_field :body %> </div> <% end %> It may be that I have to add some hidden_field, but I don't think so. I am using http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#cha-user_microposts as resource and in that the microposts dont have any hidden_field. Instead, the user_id is indexed and it automatically is created upon the creation of a micropost (based on who is signed in at the time). This part is working for me too, adding to my rational that indexing user_id on the pcomments table is enough to automatically generate it. Here is my schema.rb file so that you can see the current state of my database. ActiveRecord::Schema.define(:version => 20121011085147) do create_table "pcomments", :force => true do |t| t.string "body" t.integer "purchase_id" t.integer "user_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end add_index "pcomments", ["purchase_id"], :name => "index_pcomments_on_purchase_id" add_index "pcomments", ["user_id"], :name => "index_pcomments_on_user_id" create_table "purchases", :force => true do |t| t.string "content" t.integer "user_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end add_index "purchases", ["user_id", "created_at"], :name => "index_purchases_on_user_id_and_created_at" create_table "sales", :force => true do |t| t.string "content" t.integer "user_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end add_index "sales", ["user_id", "created_at"], :name => "index_sales_on_user_id_and_created_at" create_table "scomments", :force => true do |t| t.string "body" t.integer "sale_id" t.integer "user_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end add_index "scomments", ["sale_id"], :name => "index_scomments_on_sale_id" add_index "scomments", ["user_id"], :name => "index_scomments_on_user_id" create_table "users", :force => true do |t| t.string "name" t.string "email" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.string "password_digest" t.string "remember_token" t.boolean "admin", :default => false end add_index "users", ["email"], :name => "index_users_on_email", :unique => true add_index "users", ["remember_token"], :name => "index_users_on_remember_token" end and the reason I know its not working is that I check in the database and the pcomment is successfully created with all columns filled in including purchase_id but the user_id is still blank. also, the user has_many pcomments and has_many purchases. The purchase has_many pcomments and belongs_to user. The pcomment belongs_to user and belong_to purchase. also, here is the pcomments_controller.rb class PcommentsController < ApplicationController before_filter :signed_in_user def create #purchase = Purchase.find(params[:purchase_id]) #pcomment = #purchase.pcomments.build(params[:pcomment], :user_id => #purchase.user_id) #pcomment.purchase = #purchase if #pcomment.save flash[:success] = "Offer submited!" redirect_to :back else render 'shared/_pcomment_form' end end def new #pcomment=purchase.pcomments.new end end def new #pcomment=purchase.pcomments.new(:user_id => purchase.user_id) end end
purchase.pcomments.build builds empty Pcomment object just with purchase_id filled from purchase. To assign also user_id pass the hash with attribute: purchase.pcomments.build(:user_id => purchase.user_id)
Getting the type a polymorphically associated element in a controller (Rails)?
I have three models: Post (has_many votes as votable), Comment (has_many votes as votable) and Votes (belongs_to votable). This is votes_controller.rb: class VotesController < ApplicationController def vote_up #votable = WHAT_TO_PLACE_HERE?.find(params[:id]) if #votable.votes.exists?(:user_id => current_user.id) #notice = 'You already voted' else #vote = #post.votes.create(:user_id => current_user.id, :polarity => 1) #votable.reload end respond_to do |format| format.js end end Relevant schema: create_table "comments", :force => true do |t| t.text "content" t.integer "post_id" t.integer "user_id" t.datetime "created_at" t.datetime "updated_at" t.integer "total_votes", :default => 0 end add_index "comments", ["post_id", "user_id"], :name => "index_comments_on_micropost_id_and_user_id" create_table "posts", :force => true do |t| t.string "content" t.integer "user_id" t.datetime "created_at" t.datetime "updated_at" t.string "title" t.integer "comments_count", :default => 0, :null => false t.integer "total_votes", :default => 0 end create_table "votes", :force => true do |t| t.integer "votable_id" t.string "votable_type" t.integer "user_id" t.integer "polarity" t.integer "total" t.datetime "created_at" t.datetime "updated_at" end I need way to find the type of the votable element (Post or Comment) so I can assign it to #votable. Any suggestions to accomplish this? EDIT: views/posts/show.html.erb: <%= link_to "Vote Up", vote_up_path(#post), :remote => true, :class => "vote-up" %><br /> routes.rb: get 'votes/:id/vote_up' => 'votes#vote_up', as: 'vote_up'
Assuming the type is passed via params, you can do this: params[:votable_type].constantize.find(params[:id]) Please check the documentation for constantize. You can send the type by appending it as an argument to vote_up_path: <%= link_to "Vote Up", vote_up_path(#post, :votable_type => 'Post'), :remote => true, :class => "vote-up" %> Like this it will be added as a get parameter, which shouldn't be a problem. If you don't like this, you have to change your route and add :votable_type somewhere.
Having issues importing a CSV File using FasterCSV
I am new to Rails and I have figured out how to export results from my Database but I am having issues creating new records from a CSV File. With the code listed below I want to be able to import a CSV file and fill in the last two columns with session data from the user. For now I just inserted a static number to try to get this working. I currently receive "can't convert ActionDispatch::Http::UploadedFile into String" as the error message CSV DOCUMENT name,task,expected_results,require_id Test Name 1,Open a File,Open,t Test Name 2,Read a File,Read,t Test Name 3,Close a File,Close,f CONTROLLER def csv_import file = params[:file] FasterCSV.foreach(file,{:headers => true, :row_sep => :auto}) do |row| Script.create!(:name => row[0], :task => row[1], :expected_results => row[2], :require_id => row[3], :department_id => 1, :category_id => 1) end end VIEW <%=form_tag '/script_admin/csv_import', :multipart => true do%> <%= file_field_tag "file" %><br/> <%= submit_tag("Import") %> <% end %> DB MIGRATION class CreateScripts < ActiveRecord::Migration def self.up create_table :scripts do |t| t.integer :department_id, :null => false t.integer :category_id, :null => false t.string :name, :null => false t.string :task, :null => false t.string :expected_results, :null => false t.boolean :require_id, :null => false, :default => "t" t.timestamps end end def self.down drop_table :scripts end end Any help with this can be appreciated ~Kyle
Issue is resolved thanks to James Gray def csv_import file = params[:file] FCSV.new(file.tempfile, :headers => true).each do |row| Script.create!(:name => row[0], :task => row[1], :expected_results => row[2], :require_id => row[3], :department_id => 1, :category_id => 1) end end