简体   繁体   中英

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). 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. I then validated :audio so my track.rb model now looks like this:

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:

<%= 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:

在此处输入图片说明

It seems to be disagreeing with the associated genres (another migration that I previously added to the tracks model)

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.

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

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:

<%= 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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