id = 0注册时将新用户分配给模型时出错

时间:2012-04-26 18:10:59

标签: ruby-on-rails devise registration

我正在尝试允许当前用户邀请其他用户并将新的被邀请者分配到商家信息,但在受邀用户注册时收到错误。

有两个模型,User和Listing,与每个模型上的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时收到此错误。

0 个答案:

没有答案