我正在尝试在我的应用中实现博客应用。但是我在保存帖子后收到以下错误。
NoMethodError in Homes#blog
Showing C:/Site/library_management1/app/views/homes/blog.html.erb where line #19 raised:
undefined method `name' for #<Array:0x21896a0>
实际上我的博客实施故事是这样的。当登录用户将在所需位置添加他的名字和评论并点击提交按钮时,所有博客都将保存并显示。所有登录用户所有的评论都应该可见到这个app.Suppose一个用户想要回复/编辑然后他/她可以这样做。
我的代码如下。
<% if current_user %>
<div class="totaldiv">
<div class="navdiv"><span>STUDENT INFORMATION</span><span>Logged in as <%= current_user.email %></span></div>
<div class="wrapper">
<div id="leftsidebtn">
<ul>
<li><a href="/homes/issuebooks">Book issue</a></li>
<li><a href="/homes/availablebooks">Books Available</a></li>
<li><a href="/homes/magazines?user_id=<%= current_user.id %>">Magazines Purchase</a></li>
<li><a href="/sessions/removeuser">Log Out</a></li>
</ul>
</div>
</div>
<div class="restdiv" id="ex3" >
<center>
<%= form_for :blogs,:url => {:action => 'savecomments',:id => current_user.id } do |f| %>
<p>
<label for="name">Name:</label>
<%= f.text_field :name,placeholder:"Enter your name" %>
</p>
<p>
<label for="Comment">Comment:</label>
<%= f.text_area :body,:class => "blog-navigation",placeholder:"Type your comment here" %>
</p>
<p>
<%= f.submit "Add Comments",:class =>"btn btn-success" %>
</p>
<% end %>
<% if params[:id] %>
<h1>Comments</h1>
<% @blogs.each do |blog| %>
<div class="blog-site">
<div class="name-site">
<%= blog.name %>
</div>
<div class="message-site">
<%= blog.body %>
</div>
<div class="reply-site">
<%= link_to 'Replay',homes_blog_path %>
</div>
</div>
<% end %>
<% end %>
</center>
</div>
</div>
<% end %>
class HomesController < ApplicationController
before_filter :authenticate_admin!,only: [:admin]
def index
end
def admin
end
def managebooks
@books=Book.new
if params[:id]
@books=Book.find(params[:id])
@book=Book.all
end
end
def savebooks
@books=Book.new(params[:books])
if @books.save
flash[:notice]="Data has submitted successfully"
flash[:color]="valid"
redirect_to :action => 'managebooks',:id => @books.id
else
flash[:notice]="Data couldnot submitted successfully"
flash[:color]="invalid"
render 'managebooks'
end
end
def remove
@books=Book.find(params[:id])
@books.destroy
end
def books
end
def showbooks
@books=Book.all
end
def searchbooks
@books=Book.all
end
def member
@users=User.new
end
def registration
@users=User.new
end
def savedata
@users=User.new(params[:users])
if @users.save
flash[:notice]="Data has submitted successfully"
flash[:color]="valid"
redirect_to :action => 'member'
else
flash[:notice]="Data could not submitted successfully"
flash[:color]="invalid"
render 'registration'
end
end
def issuebooks
@issues=Issue.new
end
def savedissuebooks
@issues=Issue.new(params[:issues])
if @issues.save
flash[:notice]="information has saved successfully"
flash[:color]="valid"
redirect_to :action => 'member'
else
flash[:notice]="Data couldnot saved"
flash[:color]="invalid"
render 'issuebooks'
end
end
def availablebooks
@books=Book.all
end
def userissues
@issues=Issue.all
end
def magazine
@magazines=Magazine.new
end
def savemagazines
@users=User.find(params[:id])
@magazines=Magazine.new(params[:magazines])
@magazines.user_id=@users.id
if @magazines.save
flash[:notice]="Data submitted successfully"
flash[:color]="valid"
redirect_to :action => "member"
else
flash[:notice]="Data could not saved"
flash[:color]="invalid"
render 'magazines'
end
end
def magazineissue
@magazines=Magazine.all
@users=User.find @magazines.first.user_id
end
def blog
if params[:id]
@blogs=Blog.find(params[:id])
@blogs=Blog.all
else
@blogs=Blog.new
end
end
def savecomments
@users=User.find(params[:id])
@blogs=Blog.new(params[:blogs])
@blogs.user_id=@users.id
if @blogs.save
flash[:notice]="Comment has been posted successfully"
flash[:color]="valid"
redirect_to :action => "blog",:id => params[:id]
else
flash[:notice]="Comment could not saved"
flash[:color]="invalid"
render 'blog'
end
end
end
class Blog < ActiveRecord::Base
attr_accessible :body, :name
validates :name, :presence => true, :length => {:in => 3..10}
belongs_to :user
end
请检查我的所有代码,让我知道我在哪里犯了错误,并根据我上面解释的故事,帮助我正确地制作这个博客应用程序。
答案 0 :(得分:0)
这不是发布随机代码并让其他人修复它的论坛。请务必阅读本指南,了解如何提出一个好问题:https://stackoverflow.com/help/how-to-ask
作为下一步,请务必阅读并理解您的错误消息,因为它们是您自己解决问题的最佳方法。
接下来我想向您发一篇博客文章,解释如何自行调试rails应用程序:http://nofail.de/2013/10/debugging-rails-applications-in-development/
了解rails错误消息试图告诉您的内容非常重要:
数组的未定义方法
name
:0x21896a0
所以它在某个数组上调用.name
。
显示C:/Site/library_management1/app/views/homes/blog.html.erb第19行:
它在第19行的blog.html.erb
中执行此操作,我想这是此部分:
<%= form_for :blogs,:url => {:action => 'savecomments',:id => current_user.id } do |f| %>
<p>
<label for="name">Name:</label>
<%= f.text_field :name,placeholder:"Enter your name" %>
</p>
因此,此表单尝试在您定义为.name
的嵌套对象上调用:blogs
,以获取:name
的{{1}}属性。那么表单试图使用的对象是什么?
Homes#blog中的NoMethodError
查看f.text_field
的控制器代码,以查看可能的表单对象的来源:
Homes#blog
所以唯一可能是数组的是def blog
if params[:id]
@blogs=Blog.find(params[:id])
@blogs=Blog.all
else
@blogs=Blog.new
end
end
。这段代码对我来说看起来不太合适。你为什么只用@blogs = Blog.all
代替Blog.find
?