RaceController中的ActionController :: UrlGenerationError#new

时间:2016-01-29 13:35:50

标签: ruby-on-rails ruby

我查看了似乎已经提出同样问题的所有主题,但没有得到答案。当我尝试使用new_racer_path创建一个新的racer时,我收到以下错误。

ActionController::UrlGenerationError in Racers#new
Showing /home/dsmegha/RoR-Coursera/Mongo/raceday/app/views/racers/_form.html.erb where line #1 raised:

No route matches {:action=>"show", :controller=>"racers", :id=>""} missing required keys: [:id]
Extracted source (around line #1):
1  <%= form_for(@racer) do |f| %>
2  <% if @racer.errors.any? %>
3    <div id="error_explanation">
4      <h2><%= pluralize(@racer.errors.count, "error") %> prohibited this racer from being saved:</h2>
5
6     <ul>

我的佣金路线输出:

$ rake routes
    Prefix Verb   URI Pattern                Controller#Action
    racers GET    /racers(.:format)          racers#index
           POST   /racers(.:format)          racers#create
 new_racer GET    /racers/new(.:format)      racers#new
edit_racer GET    /racers/:id/edit(.:format) racers#edit
     racer GET    /racers/:id(.:format)      racers#show
           PATCH  /racers/:id(.:format)      racers#update
           PUT    /racers/:id(.:format)      racers#update
           DELETE /racers/:id(.:format)      racers#destroy
      root GET    /                          racers#index

我的控制器:

  def show
  end
  def new
     @racer = Racer.new
  end

  def create
    @racer = Racer.new(racer_params)

    respond_to do |format|
      if @racer.save
        format.html { redirect_to @racer, notice: 'Racer was successfully created.' }
        format.json { render :show, status: :created, location: @racer }
      else
        format.html { render :new }
        format.json { render json: @racer.errors, status: :unprocessable_entity }
      end
    end
  end

    def racer_params
      params.require(:racer).permit(:number, :first_name, :last_name, :gender, :group, :secs)
    end

我的new.html.erb:

<h1>New Racer</h1>

<%= render 'form' %>

<%= link_to 'Back', racers_path %>

抛出错误的_form.html.erb的起始部分。

<%= form_for(@racer) do |f| %>
  <% if @racer.errors.any? %>

index.html.erb的一部分,可以从中创建新的赛车。

<%= link_to 'New Racer', new_racer_path %>

我的理解是,当点击新用户时,我的控制器中的新动作被执行,创建一个新的Racer并将其分配给@racer,然后渲染new.html.erb,然后渲染_form。我是一个铁杆新手,如果你能帮我调试它会很棒。

2 个答案:

答案 0 :(得分:0)

我相信您的Racer模型可能设置错误。

应该定义如下:

# app/models/racer.rb
class Racer < ActiveRecord::Base
end

如果你的Racer类没有从ActiveRecord :: Base继承,它就不会响应new_record?方法,因此form_for不会正确选择路由。

答案 1 :(得分:0)

好的,这是所涉及的所有部分的代码

表单部分:_form.html.erb

<%= form_for(@racer) do |f| %>
  <% if @racer.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@racer.errors.count, "error") %> prohibited this racer from being saved:</h2>
      <ul>
      <% @racer.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

控制器:racers_controller.rb

class RacersController < ApplicationController
before_action :set_racer, only: [:show, :edit, :update, :destroy]
...
def new
  @racer = Racer.new
end

@racer = Racer.new(racer_params)

  respond_to do |format|
    if @racer.save
      format.html { redirect_to @racer, notice: 'Racer was successfully created.' }
      format.json { render :show, status: :created, location: @racer }
    else
      format.html { render :new }
      format.json { render json: @racer.errors, status: :unprocessable_entity }
    end
  end
end
...
private

def set_racer
  @racer = Racer.find(params[:id])
end

def racer_params
  params.require(:racer).permit(:number, :first_name, :last_name, :gender, :group, :secs)
end

型号:racer.rb

class Racer < ActiveRecord::Base
end

路线:routes.rb

Rails.application.routes.draw do
  resources :racers
end

确保运行rake db:migrate并重启网络服务器,你永远不知道......