2017-09-15 1 views
0

私はレール(5.1.4)で簡単な音楽アップロード/ストリーミングウェブアプリを構築しようとしています。私はpaperclip gemをインストールしました。また、というトラックモデルにpaperclipの移行を追加しました。トラックテーブルにt.attachment :audioが追加され、必要なすべてのdb:migrateが実行されました。私はfile_fieldタグを追加した新しいトラックを作成するための私の_form.html.erbファイルに次にRails:Paperclipでオーディオファイルをアップロードすると、NoMethodErrorが返されます

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 

ので、今では次のようになります:

私はそのように私のtrack.rbモデルは次のようになります :audioを検証しました
<%= 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 %> 

トラック/新が正常にフォームビューをレンダリングし、入力するデータフィールドを、私を可能にする、私がクリックしたときただし、「トラックを作成します。」私はこのようになりますNoMethodエラーを取得:

enter image description here

それはので、私はそれをよりよく説明することができませんでしレールにかなり新しいこのめちゃくちゃ長いったらしい質問、イムのため

申し訳ありませんが(私は以前にトラックモデルに追加され、別の移行)に関連するジャンルで不同意しているようです..どんな助けも大いに評価されるだろう。レールの

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 

コピーは、私はあなたがしようとすべきだと思う

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 > 

enter image description here

答えて

0

をGenre.all C:ここにも

..私のトラックコントローラは、参照用に次のようになりますトラック/ new.html.erbにそのような部分を描画します。

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

そして、そのようなあなたの部分更新:

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

幸運を。

+0

こんにちは、私の提案に感謝します。私は試してみましたが、残念ながら同じエラーが返されました。私はそのトラックコントローラーの作成アクションで何かが欠けていると思う。しかし、私はまだそれを把握していません... – AntChamberlin

+0

レールCを入力し、 'Genre.all'と入力してコピーしてください。 @AntChamberlin –

+0

私の質問の末尾に端末コードとGenre.allのスクリーンショットを追加しました@Ronan Louarn – AntChamberlin

関連する問題