【问题标题】:Routing error that makes no sense...to me anyway毫无意义的路由错误……无论如何对我来说
【发布时间】:2013-03-27 15:24:00
【问题描述】:

好的,当我单击特定链接时,我收到的浏览器中会显示此一般错误消息:

没有路线匹配 {:action=>"edit", :controller=>"timesheets", :incident_id=>nil}

这是包含链接的页面的代码(包括创建新记录的路径)

<div style="float:left;padding:25px;">
<p><%= link_to "Add a Command Officer", new_incident_timesheet_command_officer_path %></p>
</div>

在我点击链接之前,浏览器的 url 如下所示:

localhost:3000/incidents/197/timesheet/edit

上面的浏览器 url 完全有意义。

当我将鼠标悬停在“添加指挥官”的链接上时,浏览器底部会显示: localhost:3000/incidents/197/timesheets/command_officers/new

但是当我点击它时,我得到了我在顶部提到的路由错误。这是 Command_Officers_controller 和路由文件中的代码。

我相信我已经包含了所有必需的信息,以便有人帮助我,但如果我遗漏了什么,请告诉我。感谢您的帮助

Routes.rb

root :to => "incidents#index"

resources :users
resources :profiles

resources :incidents do

resource :mat_list
member do
post :send_notice
end
member do
get :changestatus
end



resource :timesheet do
    resources :command_officers
    resources :fire_chiefs
    resources :fire_fighters
    resources :safety_officers
    resources :emts
    resources :hazmat_specialists
    resources :command_vehicles
    resources :engines
    resources :emergency_supports
    resources :hazmat_units
    resources :field_units
    resources :pumpers
    resources :tankers
    resources :rescue_units
    resources :ladder_truck75s
    resources :ladder_truck150s
end
end


resource :session do
  member do
  get :resetpass
  post :resetpass
 end
end

command_officers_controller.rb

def new
   @command_officer = CommandOfficer.new
   @timesheet = Incident.find(params[:incident_id]).timesheet

   respond_to do |format|
     format.html # new.html.erb
   end
end

# GET /command_officers/1/edit
def edit
   @command_officer = CommandOfficer.find(params[:id])
end

# POST /command_officers
# POST /command_officers.xml
def create
   # @new_command_officer = CommandOfficer.new(params[:command_officer])
  @timesheet = Incident.find(params[:incident_id]).timesheet
      @command_officer = @timesheet.command_officer.build(params[:command_officer])

  respond_to do |format|
    if @command_officer.save
               format.html { redirect_to(edit_incident_timesheet_path, :notice => 'Command officer was successfully created.') }
    else
        flash[:error] = "What happened?"
        format.html { render :new }
        end
end
 end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2 routes


    【解决方案1】:

    问题是您试图在下一页上生成一些链接,但是您传递给 rails 的 event_id 是 nil。检查您的视图/控制器并删除/修复有问题的代码。

    更一般地,学习如何阅读 rails 为您提供的堆栈跟踪,以便您自己跟踪这些。

    【讨论】:

    • 感谢您的建议,但部分问题是该应用程序曾经运行良好。但是我不得不在我的电脑上更新 ruby​​ 和 rails 的版本,现在以前可以工作的代码在某些地方被破坏了。诚然,我不是最好的程序员,我对“修复”一开始并没有真正破坏的代码有点怀疑。但我会尝试阅读 rails 堆栈来解决问题。
    • 通读堆栈,发现问题。感谢您提供简单而有效的建议。
    【解决方案2】:

    你做了一个嵌套路由但没有传递事件ID,如果你有一个名为@incident的实例变量,你可以这样做:

    <%= link_to "Add a Command Officer", new_incident_timesheet_command_officer_path(@incident) %>
    

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 2014-04-21
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多