我正在尝试使用AJAX
使所有请求/响应完全使用JSON
设计Devise。
到目前为止,这是我的代码:
我已经开始使用Devise::RegistrationsController
:
class Users::RegistrationsController < Devise::RegistrationsController
respond_to :json
end
我的相应观点是:
<div class="row">
<div class="col-lg-4"></div>
<div class="col-lg-4">
<h1>Sign up</h1>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), remote: true, format: :json, html: {id: "registration"}) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<% if @validatable %>
(<%= @minimum_password_length %> characters minimum)
<% end %>
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
</div>
<%= f.submit "Sign up", class: "btn btn-primary" %>
<% end %>
<%= render "users/shared/links" %>
</div>
<div class="col-lg-4"></div>
</div>
AJAX
请求已成功完成,但我一直收到HTML
的回复。
这是日志条目:
Started POST "/users" for ::1 at 2015-05-21 21:22:10 -0700
Processing by Users::RegistrationsController#create as JS
Parameters: {"utf8"=>"✓", "user"=>{"email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(1.0ms) begin transaction
(0.3ms) rollback transaction
Rendered users/shared/_links.html.erb (2.2ms)
Rendered users/registrations/new.html.erb within layouts/application (20.0ms)
Completed 200 OK in 444ms (Views: 361.7ms | ActiveRecord: 1.2ms)
我做错了什么?
答案 0 :(得分:0)
更改你的devise.rn initalizer添加以下行:
config.navigational_formats = ['*/*', :html, :json]
你做对了RegistrationsController
,但你需要告诉设计使用你的而不是他的。
将您的routes.rb更改为:
devise_for :users, controllers: {registrations: "users/registrations"}
答案 1 :(得分:0)
在视图中更改此行。
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), remote: true, format: :json, html: {id: "registration"}) do |f| %>
到此。
<%= form_for(resource, as: resource_name, url: registration_path(resource_name, format: :json), remote: true, html: {id: "registration"}) do |f| %>
您发送给设计的Post Ajax请求应为。
POST "/users.json"
而不是
POST "/users"