【问题标题】:No route matches [POST] in Ruby app I'm building我正在构建的 Ruby 应用程序中没有路线匹配 [POST]
【发布时间】:2014-07-25 21:08:37
【问题描述】:

我正在尝试构建一个应用程序,让用户可以使用安静的路由创建行程,但是,当用户登录并尝试创建行程时,我遇到了问题。 我得到“没有路线匹配 [POST] “/users/8/trips/new””

这些是路线:

resources :users do 
  resources :trips
end

这是行程控制器:

class TripsController < ApplicationController
  def new
    @trip = Trip.new 
  end

  def create
    @trip = Trip.create(trip_params)
    redirect_to root_path
  end
end

这是创建新行程的表格。这是我单击提交并收到错误的地方:

<div class="trip_form">
<%= form_for :trip do |f| %>  

    <%= f.label :where, "Where?" %><br/>
    <%= f.text_field :where, placeholder: "Hawaii" %><br>

    <%= f.label :when, "When?" %><br/>
    <%= f.text_field :when, placeholder: "#" %><br>


    <%= f.label :price_per_person, "Price per person? (Approximately)" %><br/>
    <%= f.text_field :price_per_person, placeholder: "$550" %><br>

    <%= f.submit "Create Trip Idea"%>
<% end %>

这些是路线:

$ rake routes
    Prefix Verb   URI Pattern                              Controller#Action
    user_trips GET    /users/:user_id/trips(.:format)          trips#index
               POST   /users/:user_id/trips(.:format)          trips#create
 new_user_trip GET    /users/:user_id/trips/new(.:format)      trips#new
edit_user_trip GET    /users/:user_id/trips/:id/edit(.:format) trips#edit
 user_trip GET    /users/:user_id/trips/:id(.:format)      trips#show
           PATCH  /users/:user_id/trips/:id(.:format)      trips#update
           PUT    /users/:user_id/trips/:id(.:format)      trips#update
           DELETE /users/:user_id/trips/:id(.:format)      trips#destroy
     users GET    /users(.:format)                         users#index
           POST   /users(.:format)                         users#create
  new_user GET    /users/new(.:format)                     users#new
 edit_user GET    /users/:id/edit(.:format)                users#edit
      user GET    /users/:id(.:format)                     users#show
           PATCH  /users/:id(.:format)                     users#update
           PUT    /users/:id(.:format)                     users#update
           DELETE /users/:id(.:format)                     users#destroy
      root GET    /                                        users#index

我想我应该可以只填写旅行新表格,然后按提交即可 自动与行程控制器中的创建方法通信?当我在表单中将 :trip 更改为 @trip 时,我收到此错误:

Trips 中的 NoMethodError#new

未定义的方法`trips_path'

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby rest controller routing


    【解决方案1】:
    <%= form_for :trip do |f| %>  
    

    应该是

    <%= form_for [@user, @trip] do |f| %> 
    

    因为你使用的是嵌套资源,所以你也需要这个

    # controller
    def new
      @user = User.find(1)
      @trip = Trip.new
    end
    

    更多内容请关注documentation for form_for

    【讨论】:

    • 谢谢!但是当我将其更改为@trip 我有另一个错误:NoMethodError in Trips#new undefined method `trips_path'
    • @Hotconnection 好的,我错过了您正在使用嵌套资源,更新了答案。
    • 太棒了!这解决了它:) 你怎么知道你需要写 ?是因为它是嵌套的,并且要创建一个旅行,你必须有一个与之关联的用户?
    • @PeterSorowka 或者太多空闲时间:)
    • @Hotconnection 是的,没错。
    【解决方案2】:

    您的 form_for 助手中有一个小错误。应该是:

    <%= form_for @trip do |f| %>
      ...
    

    助手将查看@trip 对象并看到它是一个新对象(= 尚未保存到数据库中),因此将选择POST /users/8/trips 作为表单操作。

    通过不将 ActiveRecord 对象交给助手,生成的 HTML 表单没有任何动作,因此提交表单将 POST 到当前路径(即新路径)

    编辑

    为了让助手为您拥有的嵌套资源情况选择路线,请使用:

    <%= form_for [current_user, @trip] do |f| %>
      ...
    

    假设 current_user 是您所指的用户对象。

    【讨论】:

    • 是的,但是当我将其更改为 @trip 时出现另一个错误:NoMethodError in Trips#new undefined method `trips_path'
    • 啊,我明白了。这是因为您在用户范围之外没有ressources :trips。我将编辑我的答案以反映这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多