undefined method `song_id' [rails 4] -


to break down app quickly: have songs, comments, , users model. user's can upload songs, , make comments.

in short, when submitting song below error.i've looked how ryan bates does , code identical. not sure why song_id isn't being associated. please advise :)

full app code can seen here: www.github.com/apane/leap

error msg:

nomethoderror in songs#show  showing /users/apane/downloads/leap/app/views/comments/_form.html.erb line #17 raised:  undefined method `song_id' #<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 %> 

comments#_form.html.erb

<%= form_for @comment |f| %>   <% if @comment.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@comment.errors.count, "error") %> prohibited comment being saved:</h2>        <ul>       <% @comment.errors.full_messages.each |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> 

songs_controller.rb

class songscontroller < applicationcontroller   before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy]   before_action :set_song, only: [:show, :edit, :update, :destroy]    # /songs   # /songs.json   def index     @songs = song.all   end    # /songs/1   # /songs/1.json   def show     @comment = @song   end    # /songs/new   def new     @song = song.new   end    # /songs/1/edit   def edit   end    # post /songs   # post /songs.json   def create     @song = song.new(song_params)      respond_to |format|       if @song.save         format.html { redirect_to @song, notice: 'song 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 |format|       if @song.update(song_params)         format.html { redirect_to @song, notice: 'song 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 |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 

comments_controller.rb

class commentscontroller < applicationcontroller   before_action :set_comment, only: [:show, :edit, :update, :destroy]      # /comments/new   def new   end    # /comments/1/edit   def edit   end    # post /comments   # post /comments.json   def create     @comment = comment.new(comment_params)      respond_to |format|       if @comment.save         format.html { redirect_to song_url(@comment.song_id), notice: 'comment 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 |format|       if @comment.update(comment_params)         format.html { redirect_to song_url(@comment.song_id), notice: 'comment 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 share common setup or constraints between actions.     def set_comment       @comment = comment.find(params[:id])     end      # never trust parameters scary internet, allow white list through.     def comment_params       params.require(:comment).permit(:song_id, :author_name, :site_url, :content, :user_id)     end end 

songs#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">       <% 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 @ %h:%m') %></em>           <%=simple_format comment.content %>           <p>               <%= link_to "edit", edit_comment_path(comment) %>             <% end %>               | <%= link_to "destroy", comment, :method => :delete, :confirm => "are sure?" %>           </p>         </div>       </div>     <% end %>      <h3>add comment:</h3>     <%= render :partial => 'comments/form' %> 

you forgot add association in comment model.

add in song.rb

belongs_to :song 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -