Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 没有方法错误,但我定义了它_Ruby On Rails_Ruby_Activerecord_Web_Rubygems - Fatal编程技术网

Ruby on rails 没有方法错误,但我定义了它

Ruby on rails 没有方法错误,但我定义了它,ruby-on-rails,ruby,activerecord,web,rubygems,Ruby On Rails,Ruby,Activerecord,Web,Rubygems,我打电话的时候发现了一个命名错误,我不知道如何修复它 提取的源(第4行附近): 在创建的帖子 类别: ||“别这么做,伙计!”,:method=>:delete%> 显示c:/Sites/blog/app/views/posts/show.html.erb,其中第5行出现: nil:NilClass的未定义方法“name” 这是我的posts_controller.rb class PostsController < ApplicationController def

我打电话的时候发现了一个命名错误,我不知道如何修复它

提取的源(第4行附近):




创建的帖子 类别:

||“别这么做,伙计!”,:method=>:delete%>
显示c:/Sites/blog/app/views/posts/show.html.erb,其中第5行出现:

nil:NilClass的未定义方法“name”


这是我的posts_controller.rb

  class PostsController < ApplicationController
     def index
 @post = Post.all


  end

  def new
 @post = Post.new
 @category = Category.all
  end


  def create
  @post = Post.new(post_params)
   if @post.save
   redirect_to posts_path, :notice => 'Your post has been posted!'
   else
   render 'new'
   end
  end


  def post_params
      params.require(:post).permit(:title, :body, :category_id, :admin_user_id, :admin_user, :name)
  end


  def edit
  @post = Post.find(params[:id])
  end

  def update
  @post = Post.find(params[:id])
  if @post.update_attributes(post_params)
     redirect_to post_path, :notice => 'Your post has been updated.'
     else
     render 'new'
    end  
  end

  def show
  @post = Post.find(params[:id])
  @user = AdminUser.all
  end

  def destroy
  @post = Post.find(params[:id])
      @post.destroy
      redirect_to posts_path, :notice => 'Your post has been deleted.'
   end




   end
class PostsController“您的帖子已发布!”
其他的
呈现“新”
结束
结束
def post_参数
参数require(:post).permit(:title,:body,:category_id,:admin_user_id,:admin_user,:name)
结束
定义编辑
@post=post.find(参数[:id])
结束
def更新
@post=post.find(参数[:id])
如果@post.update_属性(post_参数)
将_重定向到post_路径:notice=>“您的帖子已更新。”
其他的
呈现“新”
结束
结束
def秀
@post=post.find(参数[:id])
@user=AdminUser.all
结束
def销毁
@post=post.find(参数[:id])
@事后销毁
重定向到帖子路径:notice=>“您的帖子已被删除。”
结束
结束
这是我的帖子

class Post < ActiveRecord::Base

  belongs_to :category

 belongs_to :admin_user


end
class Post
我的管理员用户.rd

class AdminUser < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, 
         :recoverable, :rememberable, :trackable, :validatable

   has_many :posts


end
class AdminUser
您正在查看的
@post
没有关联的
管理员用户
,因此
@post.admin\u用户
为零


要么将其置于条件中,以便仅在用户在场时打印出名称,要么使用类似于
try
(例如
@post.admin\u user.try(:name)
)的方法。

由于@post.admin\u user为零,因此它会给您带来错误。您是否创建了与post关联的管理员用户
class AdminUser < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, 
         :recoverable, :rememberable, :trackable, :validatable

   has_many :posts


end