Background - I have 2 models, a upload model and a user model. Originally I had owner_id in the upload model(table) as the foreign key for the id of the user in the user model. However I could not get the foreign key to work, so I decided to go the "rails " way by renaming owner_id to user_id. Even after setting the column to user_id, it does not populate with any values at all.
class User < ActiveRecord::Base
has_many :uploads
end
class Upload < ActiveRecord::Base
belongs_to :user
end
Have tried explicitly setting the key, but it still does not populate the user_id field in the uploads table
class User < ActiveRecord::Base
has_many :uploads ,:foreign_key => 'user_id'
end
Might be something simple but I cant seem to find what it is. any suggestions ?
** the upload controller
class UploadsController < ApplicationController
def index
@uploads = Upload.all
end
def new
@upload = Upload.new
end
def create
@upload = Upload.new(params[:upload])
if @upload.save
flash[:notice] = "your file has been uploaded"
redirect_to uploads_path
else
render :action => 'new'
end
end
def destroy
@upload = Upload.find(params[:id])
@upload.destroy
flash[:notice] = "Sucessfully deleted your file"
redirect_to uploads_path
end
def download
upload = Upload.find(params[:id])
#location = "#{Rails.root}"
# send_file (@upload)
#send_file('public/test_file.pdf', :filename => 'Test File', :type => 'application/pdf', :disposition => 'attachment', :streaming => 'true', :buffer_size => '4096')
send_file upload.uploaded.path,
:filename => upload.uploaded_file_name,
:type => upload.uploaded_content_type,
:disposition => 'attachment'
flash[:notice] = "Your file has been downloaded"
end
end
**The upload form
<%= form_for(@upload, :html => { :multipart => true }) do |form| %>
<form>
<fieldset>
<div class="clearfix">
<label for="fileInput">File input</label>
<div class="input">
<%= form.file_field :uploaded %>
</div>
<div class="actions">
<input type="submit" class="btn primary" <%= form.submit "Upload" %> <button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>
<% end %>
== Schema Information
Table name: users
id :integer not null, primary key
email :string(255) default(""), not null
encrypted_password :string(128) default(""), not null
reset_password_token :string(255)
reset_password_sent_at :datetime
remember_created_at :datetime
sign_in_count :integer default(0)
current_sign_in_at :datetime
last_sign_in_at :datetime
current_sign_in_ip :string(255)**
last_sign_in_ip :string(255)
created_at :datetime
updated_at :datetime
admin :boolean default(FALSE)
== Schema Information -----------------------------------------
Table name: uploads
id :integer
created_at :datetime
updated_at :datetime
uploaded_file_name :string(255)
uploaded_content_type :string(255)
uploaded_file_size :integer
uploaded_updated_at :datetime
user_id :integer
def create @upload = current_user.uploads.create(params[:upload]) if @upload.save flash[:notice] = "your file has been uploaded" redirect_to uploads_path else render :action => 'new' end end
Please check the controller and form for any additional errors