2

有类似的问题,但我已经研究过了,但没有一个申请过。

我正在使用设计设计邀请。导轨 3.2.6。

棘手的是我有两个用户模型。用户和艺术家从用户继承的单表继承设置。我有两个邀请控制器允许分别向用户和艺术家发送邀请。

现在的问题是艺术家无法接受他们的邀请。在提交更新密码的表单后,他们会收到 406 不可接受的错误。

相关代码:

路由:

  devise_for :users,
             :class_name => User,
             :controllers => { :invitations => 'user/invitations' }

  namespace :artist do
    # Special route for devise, artists invitation
    devise_scope :user do
      resource :invitation, :controller => :invitations, :only => [:create, :new, :update] do
        get :accept, :action => :edit
      end
    end
  end

艺术家邀请控制器

class Artist::InvitationsController < Devise::InvitationsController
  respond_to :html

  # PUT /resource/invitation
  def update
    self.resource = Artist.accept_invitation!(params[resource_name])

    if resource.errors.empty?
      resource.pending
      flash[:notice] = "Welcome, complete your artist agreement to get started"
      sign_in(:user, resource)
      respond_with resource, :location => after_accept_path_for(resource)
    else
      respond_with_navigational(resource){ render :edit }
    end
  end
end

表格说明。编辑.html.erb。

<%= form_for resource, :as => resource_name, :url => artist_invitation_path(resource_name), :html => { :method => :put } do |f| %>
  <%= f.hidden_field :invitation_token %>
  <%= f.label :password, class: 'control-label' %>
  <%= f.password_field :password, :placeholder => 'Enter a password' %>
  <%= f.label :password_confirmation, class: 'control-label' %>
  <%= f.password_field :password_confirmation, :placeholder => 'Confirm your password' %>
  <%= f.submit "Set my password", :'data-disable-with' => "Hang on..." %>
<% end %>

接受标头

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:236
Content-Type:application/x-www-form-urlencoded

退货

Request URL:http://localhost:3000/artist/invitation.user
Request Method:POST
Status Code:406 Not Acceptable

我可以看到格式中的某些内容必须关闭,因为在该 .user 末尾添加了响应。我终其一生都看不到这种情况发生的原因或地点。如果我遗漏了任何可以提供帮助的信息,请告诉我。

4

1 回答 1

1

终于明白了

关键是要准确查看整个表单和控制器操作中的 resource_name 为您提供了什么。在我的情况下,当它需要成为 :artist 时,它继续回退到 :user ,正如 Frost 在评论中提到的那样,如果您注意使用 resource_name 的位置,生成的表单 html 会发生很大变化。需要重写控制器中的更新操作才能直接使用 :artist 并且需要调整 form_for 以从参数和 :as 参数中删除 resource_name。

于 2012-07-28T21:45:38.303 回答