简体   繁体   English

红宝石路轨。 未定义的方法错误,创建嵌套的资源对象

[英]Ruby rails. Undefined method error, creating nested resource object

I am trying to render a form to upload a song. 我正在尝试呈现一个表格以上传歌曲。 I have a playlist model and a song model, song being a nested resource of playlist. 我有一个播放列表模型和一个歌曲模型,歌曲是播放列表的嵌套资源。

With simple form, when I try and render the new song partial, I get undefined method songs_path for #<#<Class:0x007fdc51980870> In routes, the new song path is new_playlist_song but it doesn't seem that simple form knows this. 以简单的形式,当我尝试部分渲染新歌曲时,我得到undefined method songs_path for #<#<Class:0x007fdc51980870>在路由中,新的歌曲路径是new_playlist_song但似乎简单的形式并不知道这一点。

Songs controller: 歌曲控制器:

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

Playlists controller: 播放列表控制器:

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

Song model: 歌曲型号:

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

Playlist model : 播放清单型号:

class Playlist < ActiveRecord::Base
  belongs_to :user
  has_many :songs
end

Routes: 路线:

resources :playlists do
    resources :songs
  end

User profile link to create new song: 用户个人资料链接以创建新歌曲:

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

I have tried a few variants for the simple_form form. 我已经为simple_form表单尝试了几种变体。 New song partial 1: 新曲部分1:

<%= simple_form_for ([@playlist, @song]) do |f| %>
<%= f.file_field :audio %>
 <%= f.button :submit %>
<% end %>

another try : 另一种尝试:

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

I just can't get the form to show and I can't figure out why not. 我只是无法显示该表单,而且我不知道为什么不这样做。 Any ideas would be appreciated, thanks. 任何想法将不胜感激,谢谢。

As pointed out in the comments songs_path is not referenced in your code, and shouldn't even exist as it's a sub-resource of playlist . 正如注释中指出的那样,您的代码中未引用songs_path ,甚至不应存在,因为它是playlist的子资源。
If you want to list all the available routes just run a rake routes and make sure the code references an existing path. 如果要列出所有可用的路由,只需运行rake routes ,并确保代码引用了现有路径。

I didn't define the playlist that the song belongs to. 我没有定义歌曲所属的播放列表。

Adding to the controller 添加到控制器

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

and

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

then to simple form 然后到简单的形式

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

It works. 有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM