0
我正在嘗試使用rails(5.1.4)構建一個簡單的音樂上傳/流式web應用程序。我已經安裝了回形針寶石,添加回形針遷移到我的曲目模型與 - $ rails g paperclip track audio
,其中t.attachment :audio
添加到我的軌道表,然後運行所有必要的分貝:遷移。然後我驗證:audio
所以我track.rb模型現在看起來是這樣的:Rails:使用回形針上傳音頻文件返回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
然後在創建新的軌道,我添加的是,file_field標籤我_form.html.erb文件,所以現在看起來像這樣:
<%= 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看起來像這樣:
這似乎與相關的流派不同意(我以前添加到軌道模型的另一個遷移)
對不起,這個瘋狂囉嗦的問題,林很新的軌道,所以我不能更好地解釋它..任何幫助將大規模讚賞。
還..這裏是我的軌道控制器看起來像以供參考:
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
軌複製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 >
嗨@ronan,感謝您的建議,我很感激。我試過,不幸的是返回了同樣的錯誤。我認爲它在軌道控制器中的創建動作中缺少一些東西。但我還沒弄明白... – AntChamberlin
你可以做一個軌道C和鍵入'Genre.all'並複製粘貼結果嗎? @AntChamberlin –
我已經添加了終端碼和Genre.all的我的問題@Ronan LOUARN底部的截圖 – AntChamberlin