1

我正在尝试允许当前用户邀请另一个用户并将该新受邀者分配给列表,但是当受邀用户去注册时出现错误。

有两个模型,用户和列表,它们与每个模型上的 has_and_belongs_to_many 相关。还有一个邀请模型,其中包含一个令牌和受邀者需要分配到的列表。

我正在覆盖设计注册控制器:

  def new
    resource = build_resource({:invitation_token => params[:invitation_token]})
    resource.email = resource.invitation.recipient_email if resource.invitation
    resource.listings << resource.invitation.listing if resource.invitation
    respond_with resource
  end

  # POST /resource
  def create
    build_resource

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_in(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

在我的注册/新视图中,我有:

= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
  = f.hidden_field :listing_ids

当我邀请新用户并转到注册(注册/新)页面时,填写表格,然后单击提交,我收到以下错误:

Couldn't find Listing with id=0

以下输出到服务器日志:

Started POST "/users" for 127.0.0.1 at 2012-04-26 14:02:51 -0400
  Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"KdWS08JnnbN61JwKkUsIBNDFvJZnX6E8iN3QOS9149U=", "user"=>{"invitation_token"=>"80c98940448829e7edc623f9886e6930434e245c", "listing_ids"=>"[1]", "first_name"=>"John", "last_name"=>"Williams", "email"=>"johnpkelleher@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  Invitation Load (0.8ms)  SELECT "invitations".* FROM "invitations" WHERE "invitations"."token" = '80c98940448829e7edc623f9886e6930434e245c' LIMIT 1
  Listing Load (0.3ms)  SELECT "listings".* FROM "listings" WHERE "listings"."id" = $1 LIMIT 1  [["id", 0]]
Completed 404 Not Found in 170ms

ActiveRecord::RecordNotFound (Couldn't find Listing with id=0):
  app/controllers/registrations_controller.rb:12:in `create'

当分配的列表的 id 为 1 时,为什么我会收到此错误。

4

0 回答 0