Ruby Rails:添加其他 RESTFUL 操作

Ruby on Rails: Adding Additional RESTFUL Actions

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

首先,我将有一个 'account_settings' GET 请求,它将使用标准 edit/update REST 操作来更新某些参数。然后,我想有一个额外的 'edit_profile' 和 'update_profile' 操作来获得一个页面,该页面将允许用户更新不同的用户属性。这是它在我的 users_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

我当前的选择 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

佣金路线结果:

 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

在 edit_profile 页面上,我的表单如下所示:

=form_for @user, path: update_profile_user_path(@user) do |f|

在我当前的实施中,访问 edit_profile 页面并发布表单将返回到常规编辑页面,我的 rails 服务器说参数是不允许的。但是,正如您在我的控制器中的 update_profile 方法中看到的那样, update_profile 的控制器方法接受 user_profile_params 而不是 account_settings_params 。对它为什么会这样做有任何见解吗?

一些注意事项:

  • 您不需要 render "edit_profile" 因为这是默认设置
  • 您不需要覆盖编辑路线
  • 我强烈建议实际上有一个单独的配置文件控制器,而不是试图将其作为对用户的额外操作进行破解。

也就是说,路线看起来像

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

那么您的 link 将发送至 users/1/edit_profile (link_to "edit", edit_profile_path(@user)),表格将发送至 <%= form_for @user, path: update_user_path(@user) %>

要将 RESTful 路由添加到当前资源,您可以根据需要使用集合或成员。 collection 用作非成员资源,如 index 操作,它提供对象集合,而 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