简体   繁体   English

Rails中嵌套控制器的路径错误

[英]Wrong path for nested controller in Rails

When i try to create a new Job at Url http://localhost:3000/operations/OPERATION_ID/agents/AGENT_ID/jobs/meterpreter/new 当我尝试在网址http:// localhost:3000 / operations / OPERATION_ID / agents / AGENT_ID / jobs / meterpreter / new创建新作业时

undefined method `operation_agent_meterpreters_path' for #<#<Class:0x007ff2f7822638>:0x007ff2f9a7e0d8>
Did you mean?  operation_agent_jobs_meterpreter_path

I have the following routes 我有以下路线

Rails.application.routes.draw do


  get 'agents/index'

  get 'agents/show'

  devise_for :users

  resources :operations do
    resources :agents do
      namespace :jobs do
        resources :meterpreter
      end
    end
  end

  root 'operations#index'
end

The Controller in jobs/ 作业中的控制器/

class Jobs::MeterpreterController < ApplicationController

  def new
    @operation = Operation.find params[:operation_id]
    @agent = Agent.find params[:agent_id]
    @job = MeterpreterJob.new agent: @agent
  end

  private

  def jobs_params
    params.require(:job).permit(:ip, :port)
  end
end

The model 该模型

class Jobs::Meterpreter < Job
    jsonb_accessor :options,
        ip: :string,
        port: :integer

    #== VALIDATIONS
    validates :ip, :port, presence: true
end

And the view with the form in jobs/meterpreter/new.html.erb 以及带有Jobs / meterpreter / new.html.erb中的表单的视图

<div class="card">
    <h4 class="card-header">New Meterpreter Job</h4>
  <div class="card-body">
  <%= form_with model: [@operation, @agent, @job] do |form| %>
      <div class="form-row">
        <div class="col-8">
            <label for="name" class="col-form-label">Name:</label>
          <%= form.text_field :ip, class: "form-control", placeholder: "0.0.0.0", required: true %>
        </div>
        <div class="col-4">
            <label for="name" class="col-form-label">Name:</label>
          <%= form.text_field :port, class: "form-control", placeholder: "443", required: true %>
        </div>
      </div>
      <%= form.submit "Create", class: "btn btn-primary mt-4" %>
    <% end %>
  </div>
</div>

All Routes https://gist.github.com/drale2k/a25c4cbffe8c5e6446df2141a393cd17 所有路线https://gist.github.com/drale2k/a25c4cbffe8c5e6446df2141a393cd17

There are a few things wrong with this code as it sits now: 现在,此代码存在一些错误:

  • In your routes, you have a plurality mismatch between resources and :meterpreter . 在您的路线中, resources:meterpreter之间存在多个不匹配的情况。 If there's going to be more than one Meterpreter job in the system, they should both be plural, ie. 如果系统中将有多个Meterpreter作业,则它们都应为复数,即。 resources :meterpreters . resources :meterpreters

  • Controllers should always be named plurally, following Rails conventions, ie. 控制器应始终遵循Rails约定(即。 Jobs::MeterpretersController . Jobs::MeterpretersController You'll have to rename the controller file, and the view folder too. 您必须重命名控制器文件和视图文件夹。

  • You have a naming mismatch between Jobs::Meterpreter and MeterpreterJob . Jobs::MeterpreterMeterpreterJob之间的命名不匹配。 I don't know what's going on there, I think they're supposed to be the same. 我不知道发生了什么,我认为它们应该是相同的。

But for the main problem: 但是对于主要问题:

  • You've defined a routing namespace :jobs , meaning /jobs/ has to be part of the URL, but you're not including that namespace in your form_with tag. 您已经定义了路由命名空间:jobs ,这意味着/jobs/必须是URL的一部分,但是您没有在form_with标记中包含该命名空间。 It should include the namespace, ie. 它应该包括名称空间,即。 <%= form_with model: [@operation, @agent, :jobs, @job] do |form| %>

I'd also ask yourself if you really need a URL like /operations/123/agents/345/jobs/meterpreters/567 . 我还会问自己,您是否真的需要/operations/123/agents/345/jobs/meterpreters/567类的URL。 That's a really long URL, likely with some unnecessary information that should be stripped out for brevity. 这是一个非常长的URL,可能带有一些不必要的信息,为简洁起见,应将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM