加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在Rails中,为什么我使用Active Model Serializ

发布时间:2020-12-17 02:23:20 所属栏目:百科 来源:网络整理
导读:此代码用于UserList(用户可以创建用户待办事项列表).此特定资源不包含列表项,而只包含列表的标题和列表的类型. class Api::V1::UserListsController ApplicationController respond_to :json skip_before_filter :verify_authenticity_token def index if au
此代码用于UserList(用户可以创建用户待办事项列表).此特定资源不包含列表项,而只包含列表的标题和列表的类型.

class Api::V1::UserListsController < ApplicationController
    respond_to :json
    skip_before_filter :verify_authenticity_token

    def index
        if authenticate_user
            user_lists = @current_user.user_lists
            if user_lists
                respond_with user_lists,each_serializer: Api::V1::UserListSerializer
            else
                render json: { error: "Could not find user's lists."},status: :not_found
            end 
        else
            render json: { error: "User is not signed in." },status: :unauthorized
        end     
    end         

    def show
        if authenticate_user
            user_lists = @current_user.user_lists
            user_list = user_lists.find_by_id(params[:id])
            if user_list
                respond_with user_list,serializer: Api::V1::UserListSerializer
            else
                render json: { error: "Could not find user's list."},status: :unauthorized
        end     
    end     

    def create
        if authenticate_user
            user_list = @current_user.user_lists.new(user_list_params)
            if (user_list.save!)
                respond_with :api,:v1,@current_user,user_list,serializer: Api::V1::UserListSerializer
            else
                render json: { error: "Could not create new User List."},status: :unprocessable_entity
            end         
        else
            render json: { error: "User is not signed in." },status: :unauthorized
        end
    end

    def update
        if authenticate_user
            user_list = @current_user.user_lists.find_by_id(params[:id])

            if (user_list.update_attributes(user_list_update_params))
                respond_with :api,serializer: Api::V1::UserListSerializer                                  
                                    #respond_with user_list,serializer: Api::V1::UserListSerializer
            else
                render json: { error: "Could not update User List." },status: :unprocessable_entity
            end
        end
    end

    private

        def user_list_params
            params.require(:user_list).permit(:user_id,:type_id,:title)
        end

        def user_list_update_params
            params.require(:user_list).permit(:type_id,:title)
        end
end

现在更新工作时我PUT / PATCH …但我得到了

Completed 204 No Content in 24ms (ActiveRecord: 4.3ms)

自从我完成任何导轨以来已经过去了大约4个月,那时我才开始学习它.

1)有谁知道我为什么没有收到任何回报?我知道这与更新中的respond_with代码行有关,但我不确定究竟是什么.

2)有人可以向我澄清SHOW respond_with和CREATE respond_with之间的区别.我记得当时有一个问题抓住这个问题,显然现在.

节目

respond_with user_list,serializer: Api::V1::UserListSerializer

创建

respond_with :api,serializer: Api::V1::UserListSerializer

a)为什么创建需要:api和:v1首先,但show不?

b)为什么create需要@current_user,但show不需要?

附录:这是我的Serializer供参考

class Api::V1::UserListSerializer < ActiveModel::Serializer
  attributes :id,:user_id,:title
  has_many :items,embed: :ids
end

解决方法

除了204之外,您不应该得到任何回报.任何智能客户端都不需要接收它刚刚发送给您的数据 – 它只需要确认数据是否持久存在.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读