NoMethodError in Users#new “undefined method `simp

2019-07-11 03:08发布

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

5条回答
smile是对你的礼貌
2楼-- · 2019-07-11 03:12

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楼-- · 2019-07-11 03:14

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

查看更多
甜甜的少女心
4楼-- · 2019-07-11 03:15

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

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

查看更多
地球回转人心会变
5楼-- · 2019-07-11 03:19

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 %>
查看更多
混吃等死
6楼-- · 2019-07-11 03:25

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.

查看更多
登录 后发表回答