Ruby on rails 未定义的方法'song#u id';[第4条]
快速分解我的应用程序:我有一个歌曲、评论和用户模型。用户可以上传歌曲,并发表评论 简言之,当我提交一首歌曲时,我会遇到以下错误。我已经了解了Ryan Bates的做法,我的代码是相同的。不确定为什么没有关联歌曲id。请告知:) 完整的应用程序代码可在此处查看:www.github.com/apane/leap 错误消息:Ruby on rails 未定义的方法'song#u id';[第4条],ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,快速分解我的应用程序:我有一个歌曲、评论和用户模型。用户可以上传歌曲,并发表评论 简言之,当我提交一首歌曲时,我会遇到以下错误。我已经了解了Ryan Bates的做法,我的代码是相同的。不确定为什么没有关联歌曲id。请告知:) 完整的应用程序代码可在此处查看:www.github.com/apane/leap 错误消息: NoMethodError in Songs#show Showing /Users/apane/Downloads/leap/app/views/comments/_for
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 %>
NoMethodError在歌曲中#show
显示/Users/apane/Downloads/leap/app/views/comments/_form.html.erb,其中第17行出现:
未定义的方法“song_id”#
comments#u 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>
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
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
禁止保存此评论:
'12',:cols=>35%>
歌曲\u控制器.rb
<%= 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>
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
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
classsongscontroller
评论\u controller.rb
<%= 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>
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
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
class CommentsController
歌曲#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' %>
标题:
Bio:
音频:
在…上
|:删除,:确认=>“确定吗?”%>
添加您的评论:
'注释/表格'%>
您忘记在注释模型中添加关联
外接程序song.rb
belongs_to :song
你的声音模型应该有这样一条线 在你的声音里 这将允许在视图和控制器中访问该变量 在您的comment.rb中,如果与歌曲有任何关联
除了设置关联本身之外,注释表还应该包含song_id列。如果您检查他的github帐户,他确实有。他只是忘了在模型上写下关联。如果列存在,那么
song\u id
方法也将存在。缺少的关联可能是一个错误,可能会导致song
方法丢失,但不应导致此错误。我已将关联添加到注释模型中,但仍会收到相同的错误。我现在没有时间完全调试此错误,但请注意,它表示Song
对象没有Song\u id
方法,这听起来很合理:aSong