0

快速分解我的应用程序:我有一个歌曲、评论和用户模型。用户可以上传歌曲,发表评论。

简而言之,在提交歌曲时,我收到以下错误。我研究了 Ryan Bates 的做法,我的代码是相同的。不知道为什么 song_id 没有被关联。请指教 :)

完整的应用程序代码可以在这里看到:www.github.com/apane/leap

错误信息:

NoMethodError in Songs#show

Showing /Users/apane/Downloads/leap/app/views/comments/_form.html.erb where line #17 raised:

undefined method `song_id' for #<Song:0x007f8e71f77d10>

<div class="row">
<div class="large-6 columns">
<div class="field">
<%= f.hidden_field :song_id %>
<p>
<%= f.label :author_name, 'Name' %><br />
<%= f.text_field :author_name %>

评论#_form.html.erb

<%= form_for @comment do |f| %>
  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

      <ul>
      <% @comment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

    <div class="row">
        <div class="large-6 columns">
  <div class="field">
     <%= f.hidden_field :song_id %>
      <p>
        <%= f.label :author_name, 'Name' %><br />
        <%= f.text_field :author_name %>
      </p>
      <p>
        <%= f.label :site_url, 'Website URL' %><br />
        <%= f.text_field :site_url %>
      </p>
      <p>
        <%= f.label :content, 'Comment' %><br />
        <%= f.text_area :content, :rows => '12', :cols => 35 %>
      </p>
      <p><%= f.submit "Submit" %></p>
    <% end %>
    </div></div></div>

歌曲控制器.rb

class SongsController < ApplicationController
  before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy]
  before_action :set_song, only: [:show, :edit, :update, :destroy]

  # GET /Songs
  # GET /Songs.json
  def index
    @songs = Song.all
  end

  # GET /Songs/1
  # GET /Songs/1.json
  def show
    @comment = @song
  end

  # GET /Songs/new
  def new
    @song = Song.new
  end

  # GET /Songs/1/edit
  def edit
  end

  # POST /Songs
  # POST /Songs.json
  def create
    @song = Song.new(song_params)

    respond_to do |format|
      if @song.save
        format.html { redirect_to @song, notice: 'Song was successfully created.' }
        format.json { render action: 'show', status: :created, location: @song }
      else
        format.html { render action: 'new' }
        format.json { render json: @song.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /Songs/1
  # PATCH/PUT /Songs/1.json
  def update
    respond_to do |format|
      if @song.update(song_params)
        format.html { redirect_to @song, notice: 'Song was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @song.errors, status: :unprocessable_entity }
      end
    end
  end

  # Song /Songs/1
  # Song /Songs/1.json
  def destroy
    @song.destroy
    respond_to do |format|
      format.html { redirect_to songs_url }
      format.json { head :no_content }
    end
  end

  private

    def set_song
       @song = Song.find(params[:id])
     end

     def song_params
       params.require(:song).permit(:title, :artist, :bio, :track, :user_id)
     end
  end

评论控制器.rb

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]



  # GET /comments/new
  def new
  end

  # GET /comments/1/edit
  def edit
  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = Comment.new(comment_params)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to song_url(@comment.song_id), notice: 'Comment was successfully created.'  }
        format.json { render action: 'show', status: :created, location: @comment}
      else
        format.html { render action: 'new' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end


  # PATCH/PUT /comments/1
  # PATCH/PUT /comments/1.json
  def update
    respond_to do |format|
      if @comment.update(comment_params)
        format.html { redirect_to song_url(@comment.song_id), notice: 'Comment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @comment.destroy
      redirect_to song_url(@comment.song_id)
    end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:song_id, :author_name, :site_url, :content, :user_id)
    end
end

歌曲#show.html.erb

<p id="notice"><%= notice %>


    <p>
    <strong>Title:</strong>
    <%= @song.title %>
    </p>

    <p>
    <strong>Bio:</strong>
    <%= @song.bio %>
    </p>

    <p>
    <strong>Audio:</strong>
    <%= audio_tag (@song.track.url), controls: "controls", alt: "Please use chrome, ie, or safari" %>
    </p>


    <br /><br />


    <%= link_to 'Edit', edit_song_path(@song), class: "button small secondary"%> 
    <%= link_to 'Back', songs_path,  class: "button small secondary" %>


    <% unless @song.comments.empty? %>
      <h2><%= pluralize(@song.comments.size, 'comment') %></h2>

      <div id="comments">
      <% for comment in @song.comments %>
        <div class="comment">
          <strong><%= link_to_unless comment.site_url.blank?, h(comment.author_name), h(comment.site_url) %></strong>
          <em>on <%= comment.created_at.strftime('%b %d, %Y at %H:%M') %></em>
          <%=simple_format comment.content %>
          <p>
              <%= link_to "Edit", edit_comment_path(comment) %>
            <% end %>
              | <%= link_to "Destroy", comment, :method => :delete, :confirm => "Are you sure?" %>
          </p>
        </div>
      </div>
    <% end %>

    <h3>Add your comment:</h3>
    <%= render :partial => 'comments/form' %>
4

2 回答 2

1

您忘记在您的评论模型中添加关联。

加入song.rb

belongs_to :song
于 2013-07-17T22:14:02.277 回答
0

你的声音模型应该有这样一条线。

在你的 sound.rb

class Song

  attr_accessible :id 

end

这将允许在您的视图和控制器中访问该变量。

如果与歌曲有任何关联,请在您的 comment.rb 中

class Comment

    belongs_to :songs
    accepts_nested_attributes_for :song, :reject_if => lambda { |a| a[:content].blank? } 
    attr_accessible :sound_id    

end
于 2014-08-12T06:42:37.347 回答