HABTM Rails - 创建方法不起作用

时间:2015-05-07 21:15:45

标签: ruby-on-rails serialization has-and-belongs-to-many

当我尝试使用下面的POST创建一些用户时,我收到了以下消息:

  

在17005ms完成404未找到(浏览次数:1.7ms | ActiveRecord:6.6ms)。

我调试了一下,我发现了这个错误

  

对于ID =

的Shared :: User,找不到ID = 5的Shared :: Category

我真的不知道发生了什么。我无法创建任何用户。

谢谢!

------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[username]"
asdasdasd

------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[first_name]"
asd
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[last_name]"
asd
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[email]"
asdasdad@tob.uy
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[birth_date]"
Wed Apr 01 2015 00:00:00 GMT-0300 (ART)
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[brand]"
false
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[influencer]"
false
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[inspirational]"
false
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[gender]"
male
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[avatar]"; filename="ic_launcher.png"
Content-Type: image/png
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[categories_attributes][0][id]"
5
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[categories_attributes][0][name]"
facebook
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[categories_attributes][0][avatar]"
http://localhost:3000/uploads/shared/category/avatar/5/big_facebook.png.webp
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[categories_attributes][1][id]"
7
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[categories_attributes][1][name]"
tobuycat
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[categories_attributes][1][avatar]"
http://localhost:3000/uploads/shared/category/avatar/7/big_ic_launcher.png.webp
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[password]"
123123
------WebKitFormBoundaryZnTz2ebfP2GeXgF8
Content-Disposition: form-data; name="user[password_confirmation]"
123123
------WebKitFormBoundaryZnTz2ebfP2GeXgF8--

Clases:

class User < ActiveRecord::Base
    has_and_belongs_to_many :categories, class_name: 'Shared::Category', join_table: "categories_users"
    accepts_nested_attributes_for :categories

class Category < ActiveRecord::Base
    self.table_name = :categories

    has_and_belongs_to_many :users, class_name: 'Shared::User', join_table: "categories_users"
    accepts_nested_attributes_for :users

控制器:

def create
    user = Shared::User.new(user_params)
    user.created_by = staff_from_token
    location = user.save ? v1_private_user_url(user) : nil
    respond_with(user, location: location, serializer: Private::UserSerializer)
end

串行器

module Shared
  class CategorySerializer < ActiveModel::Serializer
    attributes :name, :id, :avatar, :avatar_thumb
    def avatar_thumb
      Rails.env.test? ? object.avatar.url : object.avatar.thumb_webp.url
    end
    def users
      object.users.order(name: :asc)
    end
    def avatar
      Rails.env.test? ? object.avatar.url : object.avatar.big_webp.url
    end
  end
end


module Shared
  class UserSerializer < ActiveModel::Serializer
    attributes :first_name, :last_name, :email, :gender, :username, :inspirational,
               :location, :brand, :influencer, :birth_date, :avatar, :avatar_thumb, :id,
               :facebook_uid, :categories
    def avatar_thumb
      Rails.env.test? ? object.avatar.url : object.avatar.thumb_webp.url
    end
    def avatar
      Rails.env.test? ? object.avatar.url : object.avatar.big_webp.url
    end
    def categories
      object.categories.map do |category|
        CategorySerializer.new(category)
      end
    end
  end
end

0 个答案:

没有答案