我正在创建一个应用程序,允许用户为我附近的地方创建自定义行程。每个地方都存储在一个位置表中,并与place_id相关联。目前,这些地方通过行程作为连接表与用户相关联。这工作正常,但我想允许用户sign_in并被定向到一个列出所有行程的视图。为此,我需要通过一组具有标题并与user_id相关联的用户参数创建行程的新实例。然后,用户可以将place_ids添加到该行程的实例中。不幸的是,我无法理解这应该如何在活动记录中进行路由。任何人都可以帮我弄清楚如何构建它,所以它会工作,因为我尝试的一切都会返回错误:没有路由匹配[GET]" / create_itinerary"。
行程模型
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
行程(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 :(得分:0)
问题是,当您访问/itinerary_new
时,它会重定向到/create_itinerary
。然后http方法不匹配,因为create_itinerary
只接受post
,没有get
。
在redirect_to create_itinerary_path
操作中移除new
。这句话毫无意义和错误。