NoMethodError in Users#new “undefined method `simple_form_for' for #<#<Class:0x9f9b568>:0xaa02440>”

ぃ、小莉子 提交于 2019-12-23 21:12:49

问题


My new.html.erb code in views/users:

 <h1>Sign Up</h1>

 <%= simple_form_for(@user) do |f| %>
 <%= f.input :username %>
 <%= f.input :password %>
 <%= f.input :password_confirmation %>
 <%= f.button :submit %>
 <% end %>

this is the error:

 undefined method `simple_form_for' for #<#<Class:0x9f9b568>:0xaa02440>

I know what this error is asking for:

 def simple_form_for
 end

but the problem is where to put it. Also this is my users_controller.rb:

 class UsersController < ApplicationController
     def new
          @user = User.new
     end

     def create
         @user = User.new(params[:user])
         if @user.save
             redirect_to @user, notice: 'User was successfully created.'
         else
             render action: "new"
         end
     end

     def show
         @user = User.find(params[:id])
     end
 end

回答1:


That's not what it's expecting at all.

simple_form_for means your app is expecting to have the Simple Form (https://github.com/plataformatec/simple_form) gem available, which provides that method for your forms to use. You should either install the gem, by adding it to your Gemfile, or rewrite your form to use Rails form helpers instead of Simple Form.




回答2:


If it does not find the method but you had installed the gem. In that case, I think you forgot to restart the server. Restart the server and it will work.




回答3:


Gemfile: gem 'bootstrap-sass'
gem 'simple_form'

you must have a setting for bootstrap "rails generate simple_form:install --bootstrap"




回答4:


you of proplem is rails no method simple_form_for you can try with form_for and read more http://guides.rubyonrails.org/getting_started.html




回答5:


The error is basically saying that the simple_form_for method is not available (as you know).

The solution is to determine what that method is, and how it needs to be used.


Simple Form

The error is caused by simple form not being available in your app.

To include it, you just have to add the gem:

#Gemfile
gem 'simple_form'

--

If you don't have the simple_form gem installed, you'll be able to fix the issue by using Rails' form_for which isn't that much different:

<%= form_for @user do |f| %>
   <%= f.text_field     :username %>
   <%= f.password_field :password %>
   <%= f.password_field :password_confirmation %>
   <%= f.submit %>
<% end %>


来源:https://stackoverflow.com/questions/34304238/nomethoderror-in-usersnew-undefined-method-simple-form-for-for-class0x9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!