[英]undefined method `model_name' with simple_form on Rails 5
[英]Undefined method 'model_name' when attempting to use Simple Form in Rails
我试图在我的模态中获取由简单形式生成的表单,但是在加载页面时我一直遇到以下错误。
undefined method 'model_name' for NilClass:Class
这是我用来尝试生成表单的简单代码
_header.html.erb (在view_pages_controller下)
<%= simple_form_for @update do |f| %>
<%= f.input :lang %>
<%= f.input :book %> #temp, just for testing simpform
<%= f.button :submit %>
<% end %>
我很确定我的控制器代码存在问题
updates_controller.rb
class UpdatesController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
@update = current_user.updates.build(params[:update])
if @update.save
flash[:success] = "Update successful"
redirect_to root_path
else
flash[:error] = "Failed to update, please try again"
redirect_to root_path
end
end
end
update.rb
class Update < ActiveRecord::Base
attr_accessible :book, :user_id, :lang, :round_id
belongs_to :user
end
任何帮助/提示将不胜感激。 我知道我的代码很糟糕。
问题是@update
在您的视图中为null。 您应该明确哪个操作呈现此视图,并将值设置为@update
。 create动作仅根据参数设置它,然后重定向到root。
我相信你正在使用某种RESTfull控制器,并且你正在渲染你的表单:new。 所以,要解决你的麻烦,请添加
@update = current_user.updates.build(params[:update])
你的新行动
您是否尝试将调试器放在控制器中您声明@update的行上方。
然后,您可以使用current_user.methods之类的内容查看current_user对象以及可用的方法
您是否包含resource:'updates' or resources:'updates'
routes.rb中的resource:'updates' or resources:'updates'
。这可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.