简体   繁体   中英

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

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

<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

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 . If there's going to be more than one Meterpreter job in the system, they should both be plural, ie. resources :meterpreters .

  • Controllers should always be named plurally, following Rails conventions, ie. 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 . 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. 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 . That's a really long URL, likely with some unnecessary information that should be stripped out for brevity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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