0

When I try to create some User with the POST below I received the following msg:

Completed 404 Not Found in 17005ms (Views: 1.7ms | ActiveRecord: 6.6ms).

I debugged a little and I find this error

Couldn't find Shared::Category with ID=5 for Shared::User with ID=

I don't know really what's going on. I can't create any user.

Thanks!

------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

Controller:

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

Serializers

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
Peter Tretyakov
  • 3,380
  • 6
  • 38
  • 54
DaniG
  • 108
  • 10
  • Are your models also defined under the Shared module? Doesn't look like there are from what you've posted – Mario May 07 '15 at 22:22
  • Yes they are mario. I didnt put but they are defined in the right place – DaniG May 08 '15 at 03:17

0 Answers0