问题被标记为找不到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'
佣金路线
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或创建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
未定义的方法`string&#39;为#你的意思?串
<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无法找到带有&#39; id&#39; =创建
的游览 来自tour_controller的快照,从更好的错误突出显示
def show @tour = Tour.find(params [:id])
{&#34; controller&#34; =&gt;&#34; tour&#34;,&#34; action&#34; =&gt;&#34; show&#34;,&#34; id&# 34; =&GT;&#34;创建&#34;}
实例变量 - @_routes nil
执行操作后,会发生以下情况: / tours的ActionController :: RoutingError 未初始化的常量ToursController
所以对一些基础知识。 - 主机登录(使用设计并提供为host.id) - 创建一个游览(现在基本只有一个标题和描述) - 在当前主机ID下为游览分配一个唯一的ID。 - 创建具有唯一ID的游览
答案 0 :(得分:0)
由于您定义的自定义路由,导致错误。
calabash-android run "$WORKSPACE/app/build/outputs/apk/app-debug.apk" -f json -o testresult.json
已经为游览定义了所有7条RESTful路线。
立即修复的方法是改变你的路线:
resources :tours
现在,要访问新的操作路线,您应该访问:Rails.application.routes.draw do
devise_for :host
# devise_for :hosts
resources :host, only: [:show]
resources :tour
#Root
root 'static_pages#home'
#Static Pages
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
end
您最初遇到的错误是因为您尝试访问localhost:3000/tours/new
localhost:3000/tours/create
,因为resources :tours
会映射到您的tours#show
操作。因此,您的应用程序将其视为id = create。
我建议您阅读the Hartl Tutorials以深入了解Ruby on Rails框架。你阅读它会更好。
希望我能够提供帮助