【发布时间】:2013-11-17 07:35:51
【问题描述】:
我已经生成了一个不同用户上传的文件列表。
2 Pen writing 83746__braffe2__pen-writing.wav audio/wav 1057836 2013-11-17 03:43:29 UTC
3 Pen writing 83746__braffe2__pen-writing.wav audio/wav 1057836 2013-11-17 03:46:26 UTC
4 Pen writing 83746__braffe2__pen-writing.wav audio/wav 1057836 2013-11-17 03:51:41 UTC
5 Pen writing 83746__braffe2__pen-writing.wav audio/wav 1057836 2013-11-17 03:55:48 UTC
这里是生成列表的代码sn-p:
<% @sounds.each do |sound| %>
<%= sound.id %>
<%= sound.sound_name %>
<%= sound.rating %>
<%= sound.user_id %>
<%= sound.sound_file_file_name %>
<%= sound.sound_file_content_type %>
<%= sound.sound_file_file_size %>
<%= sound.sound_file_updated_at %>
<%= button_to "Download", download_path(sound.id), :method => :get %>
<% end %>
但是,我想显示哪个用户上传了哪个文件。一个用户可以有很多声音,声音属于一个用户。我使用 postgresql 作为我的数据库。
class User < ActiveRecord::Base
has_many :sounds
end
class Sound < ActiveRecord::Base
belongs_to :user
end
来自我的架构:
create_table "sounds", force: true do |t|
t.string "description"
t.integer "rating"
t.string "location"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "sound_file_file_name"
t.string "sound_file_content_type"
t.integer "sound_file_file_size"
t.datetime "sound_file_updated_at"
t.string "sound_name"
end
add_index "sounds", ["user_id"], name: "index_sounds_on_user_id", using: :btree
据我所知,user_id 字段由于某种原因没有被填充。从我的控制器:
def new
@sound = Sound.new
@user = User.find(params[:user_id])
end
def create
@sound = Sound.create( sound_params )
redirect_to :action => "show", :id => @sound.id, :user_id => current_user
end
private
# Use strong_parameters for attribute whitelisting
# Be sure to update your create() and update() controller methods.
def sound_params
params.require(:sound).permit(
:sound_file,
:sound_name,
:description,
:location,
:user_id => current_user)
end
这是用户用来创建声音的形式:
<%= form_for [@user, @sound], :url => "/users/:user_id/sounds", :html => { :multipart => true } do |form| %>
<%= form.text_field :sound_name, placeholder: "Sound name" %>
<%= form.text_field :description, placeholder: "Description" %>
<%= form.text_field :location, placeholder: "Location" %>
<%= form.file_field :sound_file %>
<%= form.submit %>
<% end %>
不确定我做错了什么?还是我完全误解了索引在 Rails 中的工作原理?
更新 1
我在控制器中进行了以下更改:
def create
@sound = Sound.create( sound_params ).merge(:user_id => current_user.id)
redirect_to :action => "show"
end
def sound_params
params.require(:sound).permit(
:sound_file,
:sound_name,
:description,
:location,
:user_id)
end
我最终得到了这个错误消息:
undefined method `merge' for #<Sound:0x007fa6a4a61ec0>
看起来这是问题所在:
@sound = Sound.create( sound_params ).merge(:user_id => current_user.id)
我的猜测是 Rails 疯了,因为 Sound 对象不带有合并功能。我有点惊讶这是多么痛苦。我只想能够选择属于用户的声音。我想我不太明白这些关系是如何用 Rails 实现的。
我确实在我的一次迁移中注意到了这一点:
class CreateSounds < ActiveRecord::Migration
def change
create_table :sounds do |t|
t.string :sound_name
t.string :description
t.integer :rating
t.string :location
t.references :user, index: true
t.timestamps
end
end
但是,当我查看我的数据库时,我在列中看不到任何对用户的引用。
HearHere_development=# SELECT * from sounds;
id | description | rating | location | user_id | created_at | updated_at | sound_file_file_name | sound_file_content_type | sound_file_file_size | sound_file_updated_at | sound_name
【问题讨论】:
-
当前用户是否在 def create nil 中?
标签: ruby-on-rails ruby rails-activerecord rails-postgresql pg