2017-04-03 46 views
0

我正在創建一個應用程序,允許用戶爲我附近的地方創建自定義行程。每個位置都存儲在位置表中並與place_id關聯。目前,這些地點通過行程作爲連接表與用戶相關聯。這工作正常,但我想讓用戶sign_in並被定向到一個視圖,其中將列出他們所有的行程。爲此,我需要通過一組具有標題並與user_id關聯的用戶參數創建行程的新實例。然後,用戶可以將place_ids添加到該行程的該實例。不幸的是,我無法圍繞如何在有效記錄中傳遞這些信息。任何人都可以幫助我弄清楚如何構造這個,所以它會工作,因爲我嘗試的所有東西都會返回錯誤:沒有路由匹配[GET]「/ create_itinerary」。Rails加入表中的路徑和模型問題

行程模型

class Itinerary < ApplicationRecord 
belongs_to :user 
belongs_to :place 

行程控制器

class ItinerariesController < ApplicationController 
def add_place 
    # current_user 
    @place = Place.find(params[:id]) 
    @itenerary = @place.itineraries.create(user_id: current_user.id) 

    redirect_to :back 
end 

def index 
    @itinerary_places = current_user.places 
end 

def show 
    @itinerary_places = current_user.places 
end 

def new 
    @itinerary_new = Itinerary.new(params[:itinerary]) 

    redirect_to create_itinerary_path 
end 

def create 
    itinerary = Itinerary.new(itinerary_params) 
    itinerary.user_id = session[:user_id] 
    itinerary.save 
end 

private 

def itinerary_params 
    params.require(:itinerary).permit(:title) 
end 

    end 

Ininerary(new.html.erb)

<%= form_for @itinerary_new, url: {action: "create"}, html: {} do |f| %> 

<%= f.text_field :title, placeholder: "Name Your New Itinerary" %> 

<%= f.submit :class => "user_submit" %> 

行程路由

get "/itinerary_new", to: "itineraries#new" 
get "/itinerary_portal", to: "itineraries#index" 
get "/itinerary", to: "itineraries#show" 
get "/add_place/:id", to: "itineraries#add_place" 
get "/delete_itinerary", to: "itineraries#destroy" 
post "/create_itinerary", to: "itinerary#create" 

的架構

ActiveRecord::Schema.define(version: 20170402040005) do 

create_table "itineraries", force: :cascade do |t| 
t.integer "user_id" 
t.datetime "created_at", null: false 
t.datetime "updated_at", null: false 
t.integer "place_id" 
t.string "title" 

end 

回答

0

的問題是,當你訪問/itinerary_new,它重定向到/create_itinerary。然後http方法不匹配,因爲create_itinerary只接受post,沒有get

刪除redirect_to create_itinerary_pathnew的操作。這句話是沒有意義的,也是錯誤的。