【问题标题】:Use index to label results in rails 4使用索引在 rails 4 中标记结果
【发布时间】: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


【解决方案1】:

所以您尝试将 user_id(它是一个整数)与一个用户(它是一个对象)合并。你需要确保你有匹配的类型。一种重写方法:

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

您还可以在表单中添加一个隐藏字段,用于 :user_id, :value => current_user.id

【讨论】:

  • 我收到一条错误消息:#<0x007fa6a4a61ec0>
【解决方案2】:

我设法使用隐藏字段传递了用户 ID:

<%= 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.hidden_field :user_id, :value => @user.id %>
  <%= form.file_field :sound_file %>
  <%= form.submit %>

在控制器中:

  def create
    @sound = Sound.create( sound_params )
    redirect_to :action => "show", :id => @sound.id, :user_id => current_user
  end


 def sound_params
    params.require(:sound).permit(
      :sound_file,
      :sound_name,
      :description,
      :location,
      :user_id)
 end

【讨论】:

    【解决方案3】:

    找到另一种方式。由于我在模型中设置了初始关系,因此我可以这样做。

      def create
        @sound = current_user.sounds.create( sound_params )
        redirect_to :action => "show", :id => @sound.id, :user_id => current_user
      end
    
      private
    
     def sound_params
        params.require(:sound).permit(
         :sound_file,
         :sound_name,
         :description,
         :location
        )
     end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      相关资源
      最近更新 更多