我想呈現一個表單上傳歌曲。 我有播放列表模型和歌曲模型,歌曲是播放列表的嵌套資源。Ruby導軌。未定義的方法錯誤,創建嵌套資源對象
用簡單的形式,當我嘗試呈現新曲的部分,我得到undefined method songs_path for #<#<Class:0x007fdc51980870>
在路線,新鬆路是new_playlist_song
,但它似乎並不認爲簡單的形式知道這一點。
歌曲控制器:
class SongsController < ApplicationController
def new
@song = Song.new
end
def create
@song = Song.new(song_params)
if @song.save
flash[:info] = "Song uploaded sensually"
redirect_to user_path(current_user)
else
render 'new'
end
end
def index
@song = Song.all
end
private
def song_params
params.require(:song).permit(:audio)
end
end
播放列表控制器:
class PlaylistsController < ApplicationController
before_action :find_playlist, only: [:edit, :update, :destroy]
def index
@playlists = Playlist.all
end
def new
if user_signed_in?
@playlist = current_user.playlists.new
else
redirect_to(root_url)
flash[:danger] = "You have to register to purchase gigs"
end
end
def create
@playlist = current_user.playlists.new
@playlist.user_id = current_user.id
if @playlist.save
redirect_to new_playlist_path
else
render 'new'
end
end
def destroy
@playlist = Playlist.find(params[:id])
if @playlist.present?
@playlist.destroy
redirect_to playlists_path
end
end
private
def find_playlist
@playlist = Playlist.find(params[:id])
end
end
宋模型:
class Song < ActiveRecord::Base
belongs_to :playlist
has_attached_file :audio
validates_attachment_presence :audio
validates_attachment_content_type :audio, :content_type => [ 'audio/mp3','audio/mpeg']
end
播放列表模型:
class Playlist < ActiveRecord::Base
belongs_to :user
has_many :songs
end
條
路線:
resources :playlists do
resources :songs
end
用戶配置文件鏈接創建新的歌曲:
<p> <%= @user.playlist.user_id %> </p>
<p> <%= @user.playlist.created_at %> </p>
<% if @user == current_user %>
<p> <%= link_to "Uploaad track", new_playlist_song_path(@user) %>
<% end %>
我已經試過了simple_form形式幾個變種。 新歌部分1:
<%= simple_form_for ([@playlist, @song]) do |f| %>
<%= f.file_field :audio %>
<%= f.button :submit %>
<% end %>
闖闖:
<%= simple_form_for ([@playlist, current_user.playlist.songs.build]) do |f| %>
我不能讓形式展現,我想不通爲什麼。任何想法將不勝感激,謝謝。
我在代碼中的任何地方都看不到「songs_path」 – emaillenin