Ruby On Rails - 路由错误 - 没有路由匹配{:action =>“show”,:controller =>“trips”}

时间:2015-07-12 16:16:20

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

我们正在使用Ruby on Rails 3.2开发社交网络原型。 该系统是为旅行者设计的,因此每个用户必须有很多旅行。 我们创建了旅行模型,并在旅行模型和用户模型中分别设置了“belongs_to”和“has_many”的关联。 我们创建了一个表单来为特定用户创建一个新的行程,但是当我们提交表单时,系统会返回此错误:

Routing Error
No route matches {:action=>"show", :controller=>"trips"}

我们知道此行已成功创建,因为如果我们输入网址http://localhost:3000/users/1/trips/1,我们会看到已创建的行程。

这是代码:

trip.rb

class Trip < ActiveRecord::Base
  attr_accessible :name, :departure, :arrive, :trip_objects

  belongs_to :user

  default_scope order: 'trips.created_at DESC'
  validates :user_id, presence: true
  validates :name, :departure, :arrive, :trip_objects, presence: true
end

trips_controller.rb

def create
  # refine the trip variable content with the data passed by the sign up form
  @trip = current_user.trips.build(params[:trip])
  if @trip.save
    # handle a successful save
    flash[:success] = 'Trip created!'
    redirect_to user_trip_path
  else
    @trip = []
    redirect_to home_path
  end
end

def index
 @user = User.find(params[:user_id])
 # get all her trips
 @trips = @user.trips.paginate(page: params[:page])
end

进入 routes.rb

resources :users do
  member do
     get :info
  end
  resources :trips, only: [:new, :create, :index, :show, :destroy]
end

此外,在 trips_controller.rb - 创建操作中,如果我们重定向到 user_trips_path而不是 user_trip_path 我们正确地得到了创建的旅程的索引,相应的路线。

这是您的路线

    info_user GET    /users/:id/info(.:format)           users#info
   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
    user_trip GET    /users/:user_id/trips/:id(.:format) trips#show
              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
              PUT    /users/:id(.:format)                users#update
              DELETE /users/:id(.:format)                users#destroy

你对这个问题有什么看法吗?我们对Rails很新,所以任何帮助都会受到赞赏。非常感谢!

2 个答案:

答案 0 :(得分:3)

正如您所看到的,user_trip_path需要:id和a:user_id。

user_trip GET /users/:user_id/trips/:id(.:format) trips#show
def create
  # refine the trip variable content with the data passed by the sign up form
  @trip = current_user.trips.build(params[:trip])
  if @trip.save
    # handle a successful save
    flash[:success] = 'Trip created!'
    redirect_to user_trip_path(id: @trip, user_id: @user)
  else
    @trip = []
    redirect_to home_path
  end
end

解决此问题的另一种方法是使用浅嵌套:

resources :users, shallow: true do
  resources :trips
end

它为您提供了嵌套在用户之下而不是单独路线(显示,编辑,销毁)的行程的收集路线(新建,创建,索引)。

这使您可以改为redirect_to @trip

答案 1 :(得分:0)

问题在于您的create方法中的这一行redirect_to user_trip_path。它应该是redirect_to user_trip_path(@trip, user_id: current_user.id)

正如您在rake route输出中所看到的那样,

user_trip GET    /users/:user_id/trips/:id(.:format) trips#show

您必须为路线传递:user_id:id