Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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 Rails-多个相同http方法的相同路由_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails-多个相同http方法的相同路由

Ruby on rails Rails-多个相同http方法的相同路由,ruby-on-rails,Ruby On Rails,在HTTP方法相同的情况下,有没有可能为多个操作(在不同的控制器中)使用相同的URL e、 g 目前我有: get 'users/:username/profile' => 'users#profile', as: :user_profile put 'users/:username/profile' => 'users#avatar_upload', as: :edit_user_avatar post 'users/:username/profile' => 'us

在HTTP方法相同的情况下,有没有可能为多个操作(在不同的控制器中)使用相同的URL

e、 g

目前我有:

get 'users/:username/profile' => 'users#profile', as: :user_profile
  put 'users/:username/profile' => 'users#avatar_upload', as: :edit_user_avatar
  post 'users/:username/profile' => 'users#update', as: :user_edit_profile
  post 'users/:username/profile/payment-details/add' => 'userspayment#create', as: :add_user_payment_details
  post 'users/:username/profile/payment-details/edit' => 'userspayment#update', as: :user_edit_payment_details
但是,当我在
userspayment\u controller.rb
中运行
add
update
方法时,我随后会呈现
users/profile
视图,URL会发生变化。我不想重定向,因为添加和更新表单位于选项卡中,重定向将使第一个选项卡处于活动状态。这是我的
UserpaymentsController

class UserspaymentController < ApplicationController
  before_action :authenticate_user!
  before_action :set_user

  def create
    respond_to do |format|
      @user_payment_details = UsersPaymentDetails.new(users_payment_details_params)
      if @user_payment_details.save
        record_activity('Updated payment information.')
        format.html { render :template => "users/profile", :locals => { :user => @user }, notice: 'Payment details was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render(:file => Rails.root.join('public', '422'), :formats => [:html], :status => 422, :layout => false) }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      @user_payment_details = UsersPaymentDetails.find_by(user_id: @user.id)
      if @user_payment_details.update(users_payment_details_params)
        record_activity('Updated payment information.')
        format.html { render :template => "users/profile", :locals => { :user => @user }, notice: 'Payment details was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render(:file => Rails.root.join('public', '422'), :formats => [:html], :status => 422, :layout => false) }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find_by(username: params[:username])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def users_payment_details_params
      params.require(:users_payment_details).permit(:bank, :branch, :code, :account, :holder, :user_id)
    end
end
class UserspaymentController“users/profile”,:locals=>{:user=>@user},注意:'付款详细信息已成功更新。'}
format.json{render:show,status::ok,location:@user}
其他的
format.html{render(:file=>Rails.root.join('public','422'),:formats=>[:html],:status=>422,:layout=>false)}
format.json{render json:@user.errors,status::unprocessable_entity}
终止
终止
终止
def更新
回应待办事项|格式|
@user\u payment\u details=UsersPaymentDetails.find\u by(user\u id:@user.id)
如果@user\u payment\u details.update(用户\u payment\u details\u参数)
记录活动(“更新的付款信息”)
format.html{render:template=>“users/profile”,:locals=>{:user=>@user},注意:'付款详细信息已成功更新。'}
format.json{render:show,status::ok,location:@user}
其他的
format.html{render(:file=>Rails.root.join('public','422'),:formats=>[:html],:status=>422,:layout=>false)}
format.json{render json:@user.errors,status::unprocessable_entity}
终止
终止
终止
私有的
#使用回调在操作之间共享公共设置或约束。
def set_用户
@user=user.find_by(用户名:params[:用户名])
终止
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def用户\付款\详细信息\参数
参数要求(:用户\付款\详细信息)。许可证(:银行,:分行,:代码,:账户,:持有人,:用户\ id)
终止
终止

正如另一条评论所说,你的方法行不通。然而

routes.rb中,您可以执行以下操作:

resources :users, param: :user_name

scope :users do 
  patch ':user_name/*other', controller: :users, action: :update
  get   ':user_name/*other', controller: :users, action: :show
  post  ':user_name/*other', controller: :users, action: :create
end
这将给你:

    users GET    /users(.:format)                       users#index
          POST   /users(.:format)                       users#create
 new_user GET    /users/new(.:format)                   users#new
edit_user GET    /users/:user_name/edit(.:format)       users#edit
     user GET    /users/:user_name(.:format)            users#show
          PATCH  /users/:user_name(.:format)            users#update
          PUT    /users/:user_name(.:format)            users#update
          DELETE /users/:user_name(.:format)            users#destroy
          PATCH  /users/:user_name/*other(.:format)     users#update
          GET    /users/:user_name/*other(.:format)     users#show
          POST   /users/:user_name/*other(.:format)     users#create
现在,当你有一个
url
时,你会发现:

/users/foo-bar/profile
它将路由到相应的
更新
显示
、或
创建
操作(取决于HTTP动词),其中
参数[:其他]
等于
配置文件

应用程序控制器中,可以执行以下操作:

class ApplicationController < ActionController::Base

  private

    def call_action_service
      action_service.call params.except(:controller, :action, :other)
    end

    def action_service
      "#{params[:controller].camelize}::#{params[:other].gsub('-','_').camelize}::#{params[:action].camelize}Service".constantize
    end

end
class UsersController < ApplicationController

    def show
      unless params[:other]
        # do regular show
      else
        call_action_service
      end
    end

    def update
      unless params[:other]
        # do regular update
      else
        call_action_service
      end
    end

    def create
      unless params[:other]
        # do regular create
      else
        call_action_service
      end
    end

  private 

end
other_user_path('profile/payment-details')
您可以使您的
用户控制器
类似于:

class ApplicationController < ActionController::Base

  private

    def call_action_service
      action_service.call params.except(:controller, :action, :other)
    end

    def action_service
      "#{params[:controller].camelize}::#{params[:other].gsub('-','_').camelize}::#{params[:action].camelize}Service".constantize
    end

end
class UsersController < ApplicationController

    def show
      unless params[:other]
        # do regular show
      else
        call_action_service
      end
    end

    def update
      unless params[:other]
        # do regular update
      else
        call_action_service
      end
    end

    def create
      unless params[:other]
        # do regular create
      else
        call_action_service
      end
    end

  private 

end
other_user_path('profile/payment-details')
如果我们继续执行调用
/users/foo bar/profile
get
示例,那么您将需要一个
users::profile::ShowService

#app/services/users/profile/show_service.rb
module Users
  module Profile
    class ShowService < ServiceBase 

      def call
        # do useful stuff
      end

    end
  end
end    
为了更容易地生成路径,您可以执行以下操作:

#app/helpers/application_helper.rb
module ApplicationHelper

  def other_user_path(other)
    user_path(@user.name).send(:<<, "/#{other}")
  end

end
得到

/users/foo-bar/profile/payment-details

当然,您需要在
respond|u to do | format |
内容中添加回内容。但是,我会让你来处理的。

我认为这是不可能的。想想看,如果你发布到一个url,那么你的路由器怎么知道它必须去哪个控制器?你可以做的是,如果你知道它呈现什么,点击匹配标签。我不确定使用不同的路由名称是否有帮助。将尝试触发单击。此外,您需要在更新时使用
PUT
方法。幂等性…我正在使用
PUT
更新用户头像,而无需页面重定向。如何让用户个人详细信息也使用
PUT
?是同一型号的。谢谢,@jvillian!我没想到会有这么详细的答案,但这太完美了。真的很感激!当然可以很高兴这有帮助。欢迎来到服务和瘦控制器的世界。
/users/foo-bar/profile/payment-details