0

为了创建新用户,我使用搜索视图在 ldap 中查找用户信息,并在新用户创建表单中填充数据。然后提交该表单将在 mysql 本地创建一个新用户。所以新的用户创建表单已经填充,我可以输入这些值。但在提交该表单后,页面返回消息称字段为空白。不知道为什么会这样。在 http 调试中,我看到这些值已发布到 /users/new

Form is invalid

User can't be blank
Firstname can't be blank
Lastname can't be blank
Email can't be blank
Role can't be blank

控制器 -

class UsersController < ApplicationController


  def new
        @user = User.new
        @result = User.FindActiveDirectory(params[:user_id])
    if @result.empty?
        render action: 'find', message: "Could not find a user with the id #{params[:user_id]}"
else
    return @result

    end

  end

def find
end


def display_result

    @result = User.FindActiveDirectory(params[:user_id])
    if @result.empty?
        render action: 'find', message: "Could not find a user with the id #{params[:user_id]}"
    end
return @result
end


  def create

        @user = User.new(params[:user_id])
        if @user.save
                redirect_to users_added_url, :notice => "Signed up!"
        else
                render "new"
        end
  end

end

用户视图查找 (find.html.erb) -

<h1>find User </h1>
<p><%= notice %></p>

<%= form_tag users_new_path do %>
  <p>
    <%= label_tag :Username %><br />
    <%= text_field_tag :user_id %>
  </p>
  <p class="button"><%= submit_tag " Search " %></p>
<% end %>

用户视图新建(new.html.erb)-

<h1>Add user to access this system</h1>

<% if @result %>
<% @result.each do |key, values| %>
<% if "#{key}" == "samaccountname" %>
<% $username = values.to_s %>
<% end %>
<% if "#{key}" == "sn" %>
<% $lastname = values.to_s %>
<% end %>
<% if "#{key}" == "givenname" %>
<% $firstname = values.to_s %>
<% end %>
<% if "#{key}" == "mail" %>
<% $email = values.to_s %>
<% end %>
<% end %>
<% end %>


<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
 <p>
    <%= f.label :NT_Username %><br />
    <%= f.text_field :user_id, :value => $username  %>
  </p>
 <p>
    <%= f.label :Firstname %><br />
    <%= f.text_field :firstname, :value => $firstname  %>
  </p>
 <p>
    <%= f.label :Lastname %><br />
    <%= f.text_field :lastname, :value => $lastname %>
  </p>
  <p>
    <%= f.label :Email %><br />
    <%= f.text_field :email, :value => $email %>
  </p>
  <p>
    <%= f.label :Role %><br />
    <%= f.collection_select :role, User::ROLES, :to_s, :to_s, :include_blank => false %>
  </p>
  <p class="button"><%= f.submit %></p>
<% end %>

要求 -

Started POST "/users" for 10.15.21.28 at 2012-04-07 00:00:17 -0400
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"â", "authenticity_token"=>"RDxvWXxVwoJArNN1s4Q8M8cDr5rzjP4wNF4gOnteGGY=", "user"=>{"user_id"=>"jkills000", "firstname"=>"James", "lastname"=>"Kills", "email"=>"jkills000@mysite.com", "role"=>"User"}, "commit"=>"Create User"}
4

1 回答 1

4

在你的create你做:

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

这是不正确的。它应该是:

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

正如您在请求中看到的,params[:user]包含有关用户的所有信息。params[:user_id]不存在。

于 2012-04-07T04:46:14.727 回答