0

我有一个页面,允许管理员用户通过在表单中​​输入详细信息来注册新用户。

<%= stylesheet_link_tag "signup" %>


<%= form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<div class="signup">
  <div class = "field">Username:  
  <%= f.text_field :username, :class => "username"  %></div>

</br>
  <div class = "field">Email:  
  <%= f.email_field :email, :class => "email"  %></div>

</br>
    <div class = "field">First Name:  
  <%= f.text_field :firstname, :class => "firstname"  %></div>

</br>
<div class = "field">Last Name:  
  <%= f.text_field :lastname, :class => "lastname"  %></div>

</br>

<div class="field">
    <%= f.label :admin %><br />
    <%= f.text_field :admin, :class => "admin" %>
  </div>

<div class = "field">
  <div class="actions">
    <%= f.submit "Register New User", :class => "button" %>
  </div>
</div>
</div>
<% end %>

这是来自 admin_controller 的 new 和 create 操作:

def create

    @user = User.new(params[:user])
if @user.save


    respond_to do |format|

        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
end
      else
respond_to do |format|
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

def new
    @user = User.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
  end

填写完表格,点击注册新用户,我回到主页,数据没有进入数据库。

编辑:点击注册新用户后的完整日志我可以看到日志中的数据:

    Started POST "/users" for 127.0.0.1 at 2012-09-17 12:27:49 +0100
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Bhh2ELWIFW9bENXeQ3Yta+bJqD7Dx8os9zdvnN0m5sM=", "user"=>{"username"=>"test", "email"=>"test@test.com", "firstname"=>"test", "lastname"=>"test", "admin"=>"false"}, "commit"=>"Register New User"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.2ms)


Started GET "/" for 127.0.0.1 at 2012-09-17 12:27:49 +0100
Processing by ProjectsController#index as HTML
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Project Load (0.2ms)  SELECT "projects".* FROM "projects" 
  Rendered projects/index.html.erb within layouts/application (1.0ms)
Completed 200 OK in 5ms (Views: 4.0ms | ActiveRecord: 0.4ms)

我是 Rails 新手,所以在尝试提供帮助时请记住这一点。提前致谢

4

1 回答 1

1

您根本没有将用户保存到数据库中,而是调用了错误的控制器操作。

首先:这在 Rails 中的工作方式是通常new动作将表单呈现给用户。一旦提交,create动作就会被击中。

然后,create您必须创建一个新的用户对象,其中包含用户在表单中输入的属性。这通过User.new(params[:user])params[:user]用户在表单中输入的数据在哪里)然后调用.save对象以将其保存在数据库中。

理想情况下,create操作代码应如下所示:

def create
    @user = User.new(params[:user])
    if @user.save

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @user }
      end
    else
      # validation failed
      respond_to do |format|
        format.html { render 'new' } #this will re-display the new form with model errors
        format.json { #give some error in the JSON }
      end
    end
  end
于 2012-09-17T11:08:28.763 回答