背景 - 我有2個模型,上傳模型和用戶模型。最初,我在上傳模型(表)中擁有owner_id作爲用戶模型中用戶標識的外鍵。然而,我不能讓外鍵工作,所以我決定通過將owner_id重命名爲user_id來使用「rails」方式。即使在將列設置爲user_id之後,它也不會填充任何值。活動記錄沒有設置與has_many和belongs_to的外鍵
class User < ActiveRecord::Base
has_many :uploads
end
class Upload < ActiveRecord::Base
belongs_to :user
end
是否嘗試過明確設置的關鍵,但它仍然無法在上傳表
class User < ActiveRecord::Base
has_many :uploads ,:foreign_key => 'user_id'
end
填充USER_ID字段可能是簡單的東西,但我不能似乎找到它是什麼。有什麼建議麼 ?
**上傳控制器
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
**上傳表單
<%= 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 %>
==架構信息
表名:用戶
ID:整數不爲空,主鍵
電子郵件:字符串(255)默認( 「」),NOT NULL
encrypted_password:字符串(128)默認( 「」),NOT NULL
reset_password_token:字符串(255)
reset_password_sent_at:日期時間
remember_created_at:日期時間
sign_in_count:整數默認(0)
current_sign_in_at:日期時間
last_sign_in_at:日期時間
current_sign_in_ip:字符串(255)**
last_sign_in_ip:字符串(255)
created_at:日期時間
的updated_at:日期時間
admin:布爾值默認(FALSE)
==模式信息-----------------------------------------
表名:上傳
ID:整數
created_at:日期時間
的updated_at:日期時間
uploaded_file_name:字符串(255)
uploaded_content_type :字符串(255)
uploaded_file_size:整數
uploaded_updated_at:日期時間
USER_ID:整數
同時在這裏發佈用戶模型和上傳模型。 –
我認爲問題出在窗體或控制器上,請粘貼兩者。 – JCorcuera
用信息更新了我的問題。 – Skillachie