【发布时间】:2016-07-11 07:12:24
【问题描述】:
** 更新以匹配重新发送的评论 ** - 新问题被标记为找不到 id=create - 框架没有创建任何具有特定 ID 的游览。
这个问题困扰了一段时间,决定从头开始,看看有没有漏掉什么。我是 RoR 框架的新手,它已经是一个很好的学习曲线。
项目: 允许主持人发布旅游的旅游网站 目标: 将“游览”分配给主机(使用 Devise Gem) 当前问题:我无法为创建游览分配 ID,也无法看到正在保存或创建的任何游览。
附上我的文件快照,已根据之前的用户回答进行了更新:
routes.rb
Rails.application.routes.draw do
devise_for :host
# devise_for :hosts
resources :host, only: [:show]
resources :tour
#Root
root 'static_pages#home'
#Tour
get 'tour/index'
get 'tour/new'
get 'tour/create'
#get 'tours/:id'
patch 'tour/update'
get 'tour/show'
get 'tour/edit'
get 'tour/delete'
get 'tour/update'
#Static Pages
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
Rake 路线
Prefix Verb URI Pattern Controller#Action
new_host_session GET /host/sign_in(.:format) devise/sessions#new
host_session POST /host/sign_in(.:format) devise/sessions#create
destroy_host_session DELETE /host/sign_out(.:format) devise/sessions#destroy
host_password POST /host/password(.:format) devise/passwords#create
new_host_password GET /host/password/new(.:format) devise/passwords#new
edit_host_password GET /host/password/edit(.:format) devise/passwords#edit
PATCH /host/password(.:format) devise/passwords#update
PUT /host/password(.:format) devise/passwords#update
cancel_host_registration GET /host/cancel(.:format) devise/registrations#cancel
host_registration POST /host(.:format) devise/registrations#create
new_host_registration GET /host/sign_up(.:format) devise/registrations#new
edit_host_registration GET /host/edit(.:format) devise/registrations#edit
PATCH /host(.:format) devise/registrations#update
PUT /host(.:format) devise/registrations#update
DELETE /host(.:format) devise/registrations#destroy
host GET /host/:id(.:format) host#show
tour_index GET /tour(.:format) tour#index
POST /tour(.:format) tour#create
new_tour GET /tour/new(.:format) tour#new
edit_tour GET /tour/:id/edit(.:format) tour#edit
tour GET /tour/:id(.:format) tour#show
PATCH /tour/:id(.:format) tour#update
PUT /tour/:id(.:format) tour#update
DELETE /tour/:id(.:format) tour#destroy
root GET / static_pages#home
GET /tour/index(.:format) tour#index
tour_new GET /tour/new(.:format) tour#new
tour_create GET /tour/create(.:format) tour#create
tour_update PATCH /tour/update(.:format) tour#update
tour_show GET /tour/show(.:format) tour#show
tour_edit GET /tour/edit(.:format) tour#edit
tour_delete GET /tour/delete(.:format) tour#delete
GET /tour/update(.:format) tour#update
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
tour_controller.rb
这是我在 def show 或 create id 方面遇到的主要问题
class TourController < ApplicationController
#before_action :set_tour, only: [:show, :edit, :update, :create]
before_action :authenticate_host!, except: [:create]
before_action :host_session
def index
@tours = Tour.all
end
def show
@tour = Tour.find(params[:id])
end
def new
@tour = Tour.new
end
def create
@tour = Tour.new(tour_params)
@tour.host = current_host
@tour.save
end
def edit
@tour = Tour.find(params[:id])
end
def update
@tour = Tour.find(params[:id])
if @tour.update_attributes(tour_param)
redirect_to :action => 'show', :id => @tour
else
render :action => 'edit'
end
end
def delete
Tour.find(params[:id]).destroy
redirect_to :action => 'index'
end
def set_tour
@tour = Tour.find_by_id(params[:id])
end
private
def tour_params
#params.require(:tour_details).permit(:host_id) if params[:tour_details]
params.require(:tour).permit(:host_id) if params[:tour]
end
end
_form.html erb
表单用于创建游览,虽然在我的数据库迁移中我使用字符串字段作为标题 - 但是当我使用 f.string 时 RoR 会抛出此消息......所以我总是将它改回 f .text_field
用于#的未定义方法“字符串” 你的意思是?字符串
<h1>Add new tour</h1>
<%= form_for @tour do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :description %>
<%= f.text_field :description%>
</div>
<%= f.submit "Create" %>
<% end %>
<%= link_to 'Back', {:action => 'index'} %>
show.html.erb
这是我现在的基本设置,但当我可以让 ID 用于游览时,需要继续进行。
<h1>show tour details</h1>
<p>
<strong>Title:</strong>
<h4><%= @tour.title %></h4>
</p>
<%= link_to 'Back' %>
schema.rb
ActiveRecord::Schema.define(version: 20160711094000) do
create_table "hosts", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "hosts", ["email"], name: "index_hosts_on_email", unique: true
add_index "hosts", ["reset_password_token"], name: "index_hosts_on_reset_password_token", unique: true
create_table "tours", force: :cascade do |t|
t.string "title"
t.float "price"
t.integer "tour_id"
t.text "description"
t.datetime "created_at"
t.integer "host_id"
end
end
/tour/create 出现新错误
ActiveRecord::RecordNotFound at /tour/create 找不到带有 'id'=create 的 Tour
来自 tour_controller 的快照,从更好的错误中突出显示
默认显示 @tour = Tour.find(params[:id])
{"controller"=>"tour", "action"=>"show", "id"=>"create"}
实例变量 - @_routes nil
所以要了解它的一些基础知识。 - 主机登录(与设计一起使用并作为 host.id 提供) - 创建一个旅游(现在基本只有标题和描述) - 游览在当前主持人 ID 下分配了一个唯一 ID。 - 创建具有唯一 ID 的游览
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 activerecord devise