Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails:添加额外的RESTFUL操作_Ruby On Rails_Ruby_Rest_Routing_Crud - Fatal编程技术网

Ruby on rails RubyonRails:添加额外的RESTFUL操作

Ruby on rails RubyonRails:添加额外的RESTFUL操作,ruby-on-rails,ruby,rest,routing,crud,Ruby On Rails,Ruby,Rest,Routing,Crud,在我的rails应用程序中,我希望有两种补丁方法来更新用户配置文件 首先,我将有一个“account\u settings”GET请求,它将使用标准的编辑/更新REST操作来更新某些参数。然后,我希望有一个额外的“编辑配置文件”和“更新配置文件”操作,以获得一个允许用户更新不同用户属性的页面。下面是我的users\u controller.rb中的外观,以了解更好的想法: #For account settings page #This is for the account setti

在我的rails应用程序中,我希望有两种补丁方法来更新用户配置文件

首先,我将有一个“account\u settings”GET请求,它将使用标准的编辑/更新REST操作来更新某些参数。然后,我希望有一个额外的“编辑配置文件”和“更新配置文件”操作,以获得一个允许用户更新不同用户属性的页面。下面是我的
users\u controller.rb
中的外观,以了解更好的想法:

#For account settings page

    #This is for the account settings page 
    #(only changing email and password)
    def edit
        @user = User.find(params[:id])
    end

    def update
        @user = User.find(params[:id])
        respond_to do |format|
            if @user.update_attributes(account_settings_params)
                flash.now[:success] = "Your settings have been successfully updated."
                format.html {redirect_to @user}
            else
                format.html {redirect_to edit_user_path}
                flash[:error] = "Please be sure to fill out your email, password, and password confirmation."
            end
        end
    end

    def edit_profile
        @user = User.find(params[:id])
    end

    #For update profile
    def update_profile
        @user = User.find(params[:id])
        respond_to do |format|
            if @user.update_attributes(user_profile_params)
                flash.now[:success] = "Your profile has been updated."
                format.html {redirect_to @user}
            else
                format.html {redirect_to edit_profile_user_path}
                flash[:error] = "Please be sure to fill out all the required profile form fields."
            end
        end
    end


    private

    def account_settings_params
        params.require(:user).permit(:email, :password, :password_confirmation)
    end

    def user_profile_params
        params.require(:user).permit(:name, :about_me, :location, :image_url)
    end
从my current routes.rb中选择:

#Account Settings Just for Email and Password
get 'account_settings' => 'users#edit'
patch 'settings' => 'users#update'
resources :users do
    member do
        get :edit_profile
        put :update_profile
    end
end
rake路由的结果:

 edit_profile_user GET    /users/:id/edit_profile(.:format)       users#edit_profile
update_profile_user PATCH  /users/:id/update_profile(.:format)     users#update_profile
              users GET    /users(.:format)                        users#index
                    POST   /users(.:format)                        users#create
           new_user GET    /users/new(.:format)                    users#new
          edit_user GET    /users/:id/edit(.:format)               users#edit
               user GET    /users/:id(.:format)                    users#show
                    PATCH  /users/:id(.:format)                    users#update
                    PUT    /users/:id(.:format)                    users#update
                    DELETE /users/:id(.:format)                    users#destroy
我的导航栏部分:

            -if logged_in?
                -# Logged in links
                %li
                    =link_to 'Logout', logout_path, method: :delete
                %li
                    =link_to 'Account Settings',edit_user_path(@current_user)
                %li
                    =link_to 'Edit My Profile', edit_profile_user_path(@current_user)
                %li
                    =link_to 'View My Profile', user_path(@current_user)
                %li
                    =link_to 'Members', users_path
在“编辑配置文件”页面上,我的表单如下所示:

=form_for @user, path: update_profile_user_path(@user) do |f|
在我当前的实现中,访问edit_profile页面并发布表单将返回到常规编辑页面,我的rails服务器会说参数是不允许的。但是,正如您在我的控制器中的“我的更新配置文件”方法中所看到的,更新配置文件的控制器方法接受
用户配置文件参数
,而不是
帐户设置参数
。了解它为什么会这样做吗?

一些注意事项:

  • 您不需要
    渲染“edit_profile”
    ,因为这是默认情况下完成的
  • 您不需要覆盖编辑路由
  • 我强烈建议实际使用一个单独的配置文件控制器,而不是试图将其作为对用户的额外操作进行入侵
也就是说,路线看起来像

resources :users do
  member do
    get :edit_profile
    patch :update_profile
  end
end

然后您的链接将是用户/1/编辑配置文件(
链接到“编辑”,编辑配置文件\u路径(@user)
),表单将是

,将RESTful路由添加到当前资源中,您可以根据需要使用集合或成员。
collection
用作非成员资源,如索引操作,它提供了一个对象集合,其中as show操作需要显示一个对象。因此,
member
用于获取单个对象的动作。 在这里你可以使用

resources users do
  resources member do
    get :edit_profile
    put :update_profile
  end
end
你也可以使用

resources :users do
  get :edit_profile, on: :member
  put :update_profile, on: :member
end

使用
member
collection
在用户路由内阻止他们,他们如何确切地知道编辑配置文件将以用户id作为参数来为我提供该路径?使用当前路由更新我的原始帖子,链接错误
rake routes
将为您提供路由列表。。!它可能是edit_profile_user_path(@user)-正如这里的另一位成员所说,用户id是因为它位于“member”块中。然而,如果我查看求职者的示例代码,他们使用的是带有编辑/更新配置文件的单独控制器,我会选择后者。更干净,更专业。没错。Tbh我想我可能会这样做,但我认为知道如何进行自定义REST操作也很好:)