2015-07-12 131 views
1

我们正在开发一个Ruby on Rails 3.2的社交网络原型。 该系统是为旅行者设计的,所以每个用户都必须有很多旅行。 我们创建了旅行模型,并在旅行模型和用户模型中分别设置了与“belongs_to”和“has_many”的关联。 我们创建了一个表单创建一个特定的用户一个新的旅程,但是,当我们提交表单系统返回此错误:Ruby On Rails - 路由错误 - 无路由匹配{:action =>“show”,:controller =>“trips”}

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非常新,所以任何帮助将不胜感激。非常感谢!

+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

我认为rails应该可以自己做'to_param'的东西。 'user_trip_path(@trip,user_id:current_user)'应该工作得很好,我想。 –

+0

它通常。在处理一些涉及RSpec和FreindlyID – max

+0

的错误之后,我只是从习惯中输入信息,非常感谢。解决了问题! –

0

问题是你的创建方法中的这条线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的路线。

相关问题