2016-01-02 58 views
0

我想呈現一個表單上傳歌曲。 我有播放列表模型和歌曲模型,歌曲是播放列表的嵌套資源。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| %> 

我不能讓形式展現,我想不通爲什麼。任何想法將不勝感激,謝謝。

+0

我在代碼中的任何地方都看不到「songs_path」 – emaillenin

回答

0

正如您在代碼中未引用songs_path的評論所指出的那樣,您應該不會因爲它是playlist的子資源而存在。
如果要列出所有可用路由,只需運行rake routes並確保代碼引用現有路徑。

+1

我知道songs_path不存在,並且我沒有在任何地方指定songs_path。我不知道爲什麼簡單的表單試圖找到songs_path,因爲實際路徑是new_playlist_song_path。我怎樣才能簡單的形式指向這個,而不是song_path?我添加了':url => new_playlist_song_path',但這也不起作用。 –

+0

好的,看看https:// github。com/plataformatec/simple_form/wiki /嵌套模型它討論用簡單的表單處理嵌套資源 – bukk530

0

我沒有定義歌曲所屬的播放列表。

添加到控制器

def create 
    @song = current_user.playlist.songs.new song_params 
    @song.playlist_id = @playlist.id 
    if @song.save 

def find_playlist 
    @playlist = Playlist.find(params[:id]) 
    end 

然後以簡單的形式

<%= simple_form_for ([@playlist, @playlist.songs.build]) do |f| %> 

它的工作原理。