我正在尝试使用rails(5.1.4)构建一个简单的音乐上传/流媒体网络应用程序。我已经安装了paperclip gem,使用 - $ rails g paperclip track audio
向我的Tracks模型添加了回形针迁移,将t.attachment :audio
添加到我的track表中,然后运行所有必需的db:migrates。然后我验证了: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
然后在我的_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 %>
track / new仍然正确呈现表单视图并允许我输入数据字段,但是,当我点击“创建跟踪”时,我得到一个看起来像这样的NoMethod错误:
它似乎不同意相关的类型(我之前添加到轨道模型的另一个迁移)
对于这个疯狂冗长的问题感到抱歉,我对rails非常陌生,所以我无法更好地解释它......任何帮助都会受到大力赞赏。
此外..这是我的轨道控制器的参考资料:
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
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 >
答案 0 :(得分:0)
我认为你应该尝试在tracks / 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 %>
祝你好运。