【问题标题】:undefined method `model_name' with simple_form on Rails 5Rails 5 上带有 simple_form 的未定义方法“model_name”
【发布时间】:2016-11-18 17:43:25
【问题描述】:

当我想在 Rails 5 上编辑基本帖子时,simple_form 出现undefined method 'model_name' 错误。

show.html.haml:

= link_to "Edit", edit_post_path(@post)

edit.html.haml:

= simple_form_for @post do |f|
    = f.input :title
    = f.input :link
    = f.input :description

    = f.button :submit

posts_controller.rb:

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

def edit
end

def update
  if @post.update(post_params)
    redirect_to @post
  else
    render 'edit'
  end
end

有什么想法吗?

【问题讨论】:

  • 控制器中有edit 操作吗?
  • 是的,我只是编辑我的帖子
  • @post = Post.find(params[:id]) 添加到edit 操作
  • 谢谢它的工作!
  • 如果您发布了完整的错误消息,这将更加明显——大概是说 "...for nil:NilClass",暗示你的@post变量是nil

标签: ruby-on-rails ruby


【解决方案1】:
def edit
  @post = Post.find(params[:id]) # <=========
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    相关资源
    最近更新 更多