简体   繁体   English

Rails:使用回形针上传音频文件会返回NoMethodError

[英]Rails: Uploading Audio files with Paperclip returns NoMethodError

I'm trying to build a simple music upload/streaming web app with rails (5.1.4). 我正在尝试使用Rails(5.1.4)构建一个简单的音乐上传/流媒体Web应用程序。 I have installed the paperclip gem, added paperclip migration to my Tracks model with - $ rails g paperclip track audio , which adds t.attachment :audio to my tracks table, then ran all the necessary db:migrates. 我已经安装了回形针gem,并使用- $ rails g paperclip track audio将回形针迁移添加到了我的Tracks模型中,这在我的跟踪表中添加了t.attachment :audio ,然后运行了所有必要的db:migrates。 I then validated :audio so my track.rb model now looks like this: 然后,我验证了:audio因此我的track.rb模型现在看起来像这样:

class Track < ApplicationRecord
  belongs_to :user
  belongs_to :genre

  has_attached_file :audio
  validates :audio, presence: true
  validates_attachment_content_type :audio, :content_type => [ 
  'audio/mpeg', 'audio/mp3' ]
end

Then in my _form.html.erb file for create new tracks I added the file_field tag, so now looks like so: 然后在我的_form.html.erb文件中以创建新轨道,我添加了file_field标签,所以现在看起来像这样:

<%= simple_form_for @track do |f| %>

<%= f.input :title, label: "Track Name:" %>
<%= f.input :price %>
<%= f.file_field :audio %>
<%= select_tag(:genre_id, options_for_select(@genres), :prompt => "Select a Genre", class: "genre_select") %>
<%= f.input :description %>
<%= f.button :submit %>
<% end %>

The tracks/new still renders the form view correctly and allows me to input the data fields, however, when I click 'Create Track' I get a NoMethod Error that looks like this: 轨道/新轨道仍可正确呈现表单视图,并允许我输入数据字段,但是,当我单击“创建轨道”时,出现如下所示的NoMethod错误:

在此处输入图片说明

It seems to be disagreeing with the associated genres (another migration that I previously added to the tracks model) 似乎与相关流派不同(我之前在tracks模型中添加的另一种迁移)

Sorry for this insanely long-winded question, Im pretty new to rails so I couldn't better explain it.. any help would be massively appreciated. 对这个疯狂而漫长的问题很抱歉,我对Rails很陌生,所以我无法更好地解释它。任何帮助将不胜感激。

Also.. here is what my tracks controller looks like for reference: 另外..这是我的跟踪控制器的外观,仅供参考:

    class TracksController < ApplicationController
  before_action :find_track, only: [:show, :edit, :update, :destroy]

  def index
    if params[:genre].blank?
      @tracks = Track.all.order("created_at DESC")
    else
      @genre_id = Genre.find_by(name: params[:genre]).id
      @tracks = Track.where(:genre_id => @genre_id).order("created_at DESC")
    end
  end

  def new
    #associates the new path to current user
    @track = current_user.tracks.build

    #gets all existing genres from table with all existing params, similar to a loop thru
    @genres = Genre.all.map{ |g| [g.name, g.id]}
  end

  def show
    # first finds track by id (code is in private find_track method)
  end

  def create
    #initialises the create for the current user
    @track = current_user.tracks.build(track_params)

    # associates and passes in the selected genre param (id) into tracks table
    @track.genre_id = params[:genre_id]

    if @track.save
      redirect_to root_path
    else
      render 'new'
    end

  end # --- end CREATE

  def edit
    #gets all existing genres from table with all existing params, similar to a loop thru
    @genres = Genre.all.map{ |g| [g.name, g.id]}
  end

  def update
    if @track.update(track_params)
      redirect_to track_path(@track)
    else
      render 'edit'
    end
  end

  def destroy
    @track.destroy
    redirect_to root_path
  end

private

  def track_params
    params.require(:track).permit(:title, :description, :artist, :genre_id, :price, :audio)
  end

  def find_track
    @track = Track.find(params[:id])
  end

end

Copy of rails c Genre.all Rails c Genre.all的副本

2.4.0 :002 > Genre.all
  Genre Load (1.3ms)  SELECT  "genres".* FROM "genres" LIMIT ?  [["LIMIT", 11]]
 => #<ActiveRecord::Relation [#<Genre id: 1, name: "Acoustic", created_at: "2017-09-11 12:53:37", updated_at: "2017-09-11 12:53:37">, #<Genre id: 2, name: "Electronic", created_at: "2017-09-11 12:54:02", updated_at: "2017-09-11 12:54:02">, #<Genre id: 3, name: "Rock", created_at: "2017-09-11 12:54:07", updated_at: "2017-09-11 12:54:07">, #<Genre id: 4, name: "Reggae", created_at: "2017-09-11 12:54:13", updated_at: "2017-09-11 12:54:13">, #<Genre id: 5, name: "Classical", created_at: "2017-09-11 12:54:19", updated_at: "2017-09-11 12:54:19">, #<Genre id: 6, name: "Piano", created_at: "2017-09-11 12:54:26", updated_at: "2017-09-11 12:54:26">, #<Genre id: 7, name: "Pop", created_at: "2017-09-11 12:54:29", updated_at: "2017-09-11 12:54:29">, #<Genre id: 8, name: "Theme", created_at: "2017-09-11 12:54:32", updated_at: "2017-09-11 12:54:32">, #<Genre id: 9, name: "Animation", created_at: "2017-09-11 12:54:36", updated_at: "2017-09-11 12:54:36">, #<Genre id: 10, name: "Country", created_at: "2017-09-11 12:54:55", updated_at: "2017-09-11 12:54:55">, ...]> 
2.4.0 :003 > 

在此处输入图片说明

I think you should try to render your partial like that in tracks/new.html.erb: 我认为您应该尝试在tracks / new.html.erb中渲染部分内容:

<%= render 'tracks/form.html.erb', genres: @genres, track: @track %>

And update your partial like that: 然后像这样更新您的部分内容:

<%= simple_form_for track do |f| %>
  <%= f.input :title, label: "Track Name:" %>
  <%= f.input :price %>
  <%= f.file_field :audio %>
  <%= select_tag(:genre_id, options_for_select(genres), :prompt => "Select a Genre", class: "genre_select") %>
  <%= f.input :description %>
  <%= f.button :submit %>
<% end %>

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM