我有一个使用设计的rails应用程序,它完美无缺。我现在希望用户能够提供他们以前的客户的详细信息,我想将其添加到用户表中。但是,每当它尝试创建新用户时,它都会失败,因为它正在尝试使用当前会话持有者ID创建用户。救命啊!
控制器
SELECT Questions.ID, Questions.Question, Questions.Category, Answers.Answer
FROM Questions INNER JOIN Answers ON Questions.ID = Answers.FID
ORDER BY Questions.Category, RND(Questions.ID);
路线
class Suppliers::PreviousProjectsController < ApplicationController
before_action :authenticate_user!
def new
@new_user = User.new
end
def create
@new_user = User.new(previous_projects_params)
if @new_user.save
redirect_to root_path, notice: 'Previous customer created'
else
render :new
end
end
protected
def previous_projects_params
params.require(:user)
.permit(:first_name, :last_name, :phone, :email)
end
end
查看
get "suppliers/previous_projects/new" => 'suppliers/previous_projects#new'
post "suppliers/previous_projects/create" => 'suppliers/previous_projects#create'
日志
<div id="customer_dashboard">
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-2 sidebar">
<%= render 'customers/menu' %>
</div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1 class="page-header"><%= @title %></h1>
<div class= "project_form">
<%= form_for(@user, url: suppliers_previous_projects_create_path(@user)) do |f| %>
<div class="form-group col-sm-6">
<%= f.label :first_name, class: "required" %><br />
<%= f.text_field :first_name, autofocus: true, :required => 'required', class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= f.label :last_name, class: "required" %><br />
<%= f.text_field :last_name, :required => 'required', class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= f.label "Mobile Number", class: "required" %><br />
<%= f.text_field :phone, :required => 'required', class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= f.label :email, class: "required" %><br />
<%= f.email_field :email, :required => 'required', class: "form-control" %>
</div>
<div class="actions center space_big">
<%= f.submit "Save User", class: "btn btn-lg btn-success" %>
</div>
<% end %>
</div>
</div>
</div>
</div>
仅供参考 - 在这种情况下,用户应该有一个新的ID。相反,它试图给用户一个id为2,这是创建用户的id。非常感谢任何帮助!
更新
如果我使用完全随机的电子邮件地址
,这是一个单独的日志条目Started POST "/suppliers/previous_projects/create" for ::1 at 2017-01-27 11:04:08 +0200
Processing by Suppliers::PreviousProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"u+/ydnO4lSBcJtuqOWNiqccRWosTydAZ3Kd81wwO+DbveFRUYw53JsI0x6bolaSQTapgSrw6njnboBDFfSNlIw==", "user"=>{"first_name"=>"Stev", "last_name"=>"Three", "phone"=>"23092309029309", "email"=>"steve3@example.com"}, "commit"=>"Save User"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
(0.2ms) BEGIN
User Exists (1.1ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "steve3@example.com"], ["LIMIT", 1]]
(0.8ms) ROLLBACK
Rendering suppliers/previous_projects/new.html.erb within layouts/application
Rendered customers/_menu.html.erb (2.8ms)
Rendered suppliers/previous_projects/new.html.erb within layouts/application (16.3ms)
Completed 200 OK in 152ms (Views: 141.6ms | ActiveRecord: 2.5ms)
答案 0 :(得分:0)
我能够通过以下方式解决这个问题: 1.将变量从@user更改为@new_user。 2.使用:url规定表单中的创建操作和控制器 3.使用Pry来设置一个断点,并期待一切都出错的地方(binding.pry真的改变了我的生活!!)