【问题标题】:No route matches {:controller没有路线匹配 {:controller
【发布时间】:2012-11-29 05:39:34
【问题描述】:

我已经阅读了 20 多篇关于非常相似的问题的 StackOverflow 文章(以及网络上的许多其他文章),并尝试了他们的解决方案,但没有一个奏效。请帮助这个初学者!

特别是,我最常找到的解决方案是

form_for [@parent, @child] do |f|

但它并没有像为其他人那样修复错误。

错误:

错误发生在 localhost:3000/locations/1/restaurants/new

餐厅中的 NoMethodError#new

显示第 1 行出现的 /app/views/restaurants/_form.html.erb:

undefined method `restaurants_path' for #<#<Class:0x007fb7ab89ea80>:0x007fb7aaadc3c0>

提取的源代码(第 1 行附近):

1: <%= form_for [@location, @restaurant] do |f| %>
2:   <% if @restaurant.errors.any? %>
3:     <div id="error_explanation">
4:       <h2><%= pluralize(@restaurant.errors.count, "error") %> prohibited this restaurant from being saved:</h2> 

我在任何应用程序代码中都找不到任何提及 restaurant_path ,所以我假设它是一些神奇的 Rails 默认值。

代码:

我正在使用 has_many/belongs_to 模型:一个位置有很多餐馆。

config/routes.rb

  resources :locations do
    resources :restaurants
  end

$ rake 路线

location_restaurants GET    /locations/:location_id/restaurants(.:format)          restaurants#index
                         POST   /locations/:location_id/restaurants(.:format)          restaurants#create
new_location_restaurant GET    /locations/:location_id/restaurants/new(.:format)      restaurants#new
edit_location_restaurant GET    /locations/:location_id/restaurants/:id/edit(.:format) restaurants#edit
location_restaurant GET    /locations/:location_id/restaurants/:id(.:format)      restaurants#show
                         PUT    /locations/:location_id/restaurants/:id(.:format)      restaurants#update
                         DELETE /locations/:location_id/restaurants/:id(.:format)      restaurants#destroy

app/controllers/restaurants_controller.rb

  def new
    @restaurant = Restaurant.new

    respond_to do |format|
      format.html
      format.json { render json: @restaurant }
    end
  end

  def edit
    @restaurant = Restaurant.find(params[:id])
  end

  def create
    @location = Location.find(params[:location_id])
    @restaurant = @location.restaurants.create(params[:restaurant])

    respond_to do |format|
      if @restaurant.save
    format.html { redirect_to location_restaurants_path(@location), notice: 'Restaurant was successfully created.' }
        format.json { render json: @restaurant, status: :created, location: @restaurant }
      else
        format.html { render action: "new" }
        format.json { render json: @restaurant.errors, status: :unprocessable_entity }
      end
    end
  end

【问题讨论】:

    标签: ruby-on-rails routing


    【解决方案1】:

    您尚未初始化表单所需的@location(在form_for [@location, @restaurant] 行中)。只需将查找它的行(使用Location.find)添加到您的new 操作,然后构建@restaurant 使其与位置相关联:

    def new
      @location = Location.find(params[:location_id])
      @restaurant = @location.restaurants.build
      ...
    
    end
    

    由于您的所有操作都需要@location(包括edit,我认为它在您当前的代码中也不能正常工作),因此将其放入单独的方法中然后调用它是有意义的来自before_filter,即:

    before_filter :find_location
    
    private
    
    def find_location
      @location = Location.find(params[:location_id])
    end
    

    然后,您还可以删除您在create 操作(以及从new 操作)中找到该位置的行。

    【讨论】:

    • 有一个错字:@location.restaurant.build 应该是 @location.restaurants.build。我已经更新了我的答案。
    【解决方案2】:

    我认为问题在于您在 new 操作中实例化 @restaurant 变量的方式。因为它嵌套在位置下,所以您需要这样构建它:

    @location = Location.find(params[:location_id]) @restaurant = @location.restaurants.new

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      • 2012-07-22
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多