Rails 4 ActionController :: UrlGenerationError

时间:2015-01-03 06:14:43

标签: ruby ruby-on-rails-4 actioncontroller

我收到此错误ActionController::UrlGenerationError in Manufacturers#index以及No route matches {:action=>"new", :controller=>"cars"} missing required keys: [:manufacturer_id]以获取以下代码。我环顾四周,找不到这个问题的答案。

cars_controller.rb

class CarsController < ApplicationController
  def index
   @cars = Car.all
  end

  def show
   @car = Car.find(params[:id])
  end

  def new
   @manufacturer = Manufacturer.find(params[:manufacturer_id])
   @car = Car.new(params[:id])
   @car.manufacturer = @manufacturer
 end

 def create
  @manufacturer = Manufacturer.find(params[:manufacturer_id])
  @car = Car.new(cars_params)
  @car.manufacturer = @manufacturer

 if @car.save
  flash[:notice] = "You have successfully created a car"
  redirect_to manufacturer_cars_path
 else
   render 'new'
 end
end

private

def cars_params
  params.require(:car).permit(:make, :color, :year, :mileage, :description, :country)
end
end

manufacturers_controller.rb

class ManufacturersController < ApplicationController
  def index
    @manufacturers = Manufacturer.all
  end

  def show
    @manufacturer = Manufacturer.find(params[:id])
    @car = Car.new
  end

  def new
    @manufacturer = Manufacturer.new
  end

  def create
    @manufacturer = Manufacturer.new(manufacturer_params)

    if @manufacturer.save
      flash[:notice] = "You have successfully created a new manufacturer"
      redirect_to root_path(@manufacturer)
    else
      flash[:alert] = "Please try again"
      render :new
    end
  end

  private

  def manufacturer_params
    params.require(:manufacturer).permit(:name, :country)
  end
end

模型/ car.rb

  class Car < ActiveRecord::Base
    belongs_to :manufacturer

    validates :make, presence: true
    validates :color, presence: true
    validates :year, presence: true
    validates :mileage, presence: true
    validates :country, presence: true
  end

模型/ manufacturer.rb

  class Manufacturer < ActiveRecord::Base
    has_many :cars

    validates :name, presence: true
    validates :country, presence: true
  end

此链接适用于我的cars / index.html.erb <%= link_to "Add Car", new_manufacturer_car_path %>,但不适用于我的layouts / application.html.erb或其他地方。在manufacturer / index.html.erb中,它要求:manufacturer_id所以我尝试了不同的方法而没有运气。也许我在这里错过了一些东西。

这些是我的路线

    Prefix Verb URI Pattern                                        Controller#Action
    root GET  /                                                  manufacturers#index
    manufacturer_cars GET  /manufacturers/:manufacturer_id/cars(.:format)     cars#index
    POST /manufacturers/:manufacturer_id/cars(.:format)     cars#create
    new_manufacturer_car GET  /manufacturers/:manufacturer_id/cars/new(.:format) cars#new
    manufacturer_car GET  /manufacturers/:manufacturer_id/cars/:id(.:format) cars#show
    manufacturers GET  /manufacturers(.:format)                           manufacturers#index
    POST /manufacturers(.:format)                           manufacturers#create
    new_manufacturer GET  /manufacturers/new(.:format)                       manufacturers#new
    manufacturer GET  /manufacturers/:id(.:format)                       manufacturers#show

我很感激帮助。

0 个答案:

没有答案