簡體   English   中英

Ruby On Rails - 路由錯誤 - 沒有路由匹配{:action =>“show”,:controller =>“trips”}

[英]Ruby On Rails - Routing Error - No route matches {:action=>“show”, :controller=>“trips”}

我們正在使用Ruby on Rails 3.2開發社交網絡原型。 該系統是為旅行者設計的,因此每個用戶必須有很多旅行。 我們創建了旅行模型,並在旅行模型和用戶模型中分別設置了“belongs_to”和“has_many”的關聯。 我們創建了一個表單來為特定用戶創建一個新的行程,但是當我們提交表單時,系統會返回以下錯誤:

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

我們知道該行程已成功創建,因為如果我們輸入URL 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很新,所以任何幫助都會受到贊賞。 非常感謝!

如您所見, 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

問題是你的create方法中的這行redirect_to user_trip_path 它應該是redirect_to user_trip_path(@trip, user_id: current_user.id)

正如你在rake route s輸出中看到的,

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

您必須傳遞:user_id:id作為路由。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM