我是Rails的新手,并且一直在开发一个Web应用程序,并且必须在此过程中破坏某些东西。在创建新的“体验”时,我收到此错误“没有路由匹配[POST]”/ experience / index“'。我尝试添加”post“体验#index'来停止错误,但是经验不是创建。
的routes.rb
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resource :experiences do
member do
get 'dashboard'
get 'index'
get 'bookings'
end
end
resources :experiences, except: [:edit] do
member do
get 'listing'
get 'pricing'
get 'description'
get 'photo_upload'
get 'amenities'
get 'location'
get 'about'
get 'subcats'
get 'languages'
get 'timeslots'
end
resources :photos, only: [:create, :destroy]
resources :timeslots, only: [:create, :destroy]
resources :reservations, only: [:create]
resources :calendars
end
resources :experiences do
resources :subcategories
end
resources :subcategories do
resources :experiences
end
resources :categories do
resources :experiences
resources :subcategories
end
devise_for :users, controllers: {
sessions: 'users/sessions',
passwords: 'users/passwords',
registrations: 'users/registrations'
}
namespace :admin do
get '', to: 'dashboard#index', as: '/'
end
resources :host_requests do
resources :users
end
resources :static_pages do
resources :users
end
resources :users, only: [:show]
get '/host_calendar' => "calendars#host"
get 'search' => 'static_pages#search'
root to: "static_pages#home"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
experiences_controller.rb
class ExperiencesController < ApplicationController
before_action :set_experience, only: [:show, :edit, :update, :destroy, :listing, :pricing, :description, :photo_upload, :amenities, :location, :subcats, :languages, :about, :timeslots]
before_action :set_subcategories, only: [:subcats]
before_action :set_languages, only: [:languages]
before_action :set_amenities, only: [:amenities]
before_action :is_authorised, except: [:show, :index, :new, :create, :dashboard, :bookings]
before_action :authenticate_user!
load_and_authorize_resource
# GET /experiences
# GET /experiences.json
def index
if current_user.admin_role?
redirect_to admin_url
elsif current_user.host_role?
@experiences = current_user.experiences
else
@experiences = Experience.published
end
end
def dashboard
if current_user.admin_role?
redirect_to admin_url
elsif current_user.host_role?
@experiences = current_user.experiences
else
redirect_to root_path, alert: "You don't have permission. Apply to be a host!"
end
end
# GET /experiences/1
# GET /experiences/1.json
def show
if current_user.user_role?
if !@experience.published?
redirect_to root_path, alert: "You don't have permission."
end
end
@base_latitude = @experience.latitude
@base_longitude = @experience.longitude
@base_timezone = Timezone.lookup(@base_latitude, @base_longitude)
@timezone_name = @base_timezone.name
end
# GET /experiences/new
def new
@experience = current_user.experiences.new
@categories = Category.all.map{|c| [ c.name, c.id ] }
@subcategories = Subcategory.all
@languages = Language.all
@amenities = Amenity.all
end
# GET /experiences/1/edit
def edit
@categories = Category.all.map{|c| [ c.name, c.id ] }
@languages = Language.all
@amenities = Amenity.all
@experience.subcategory_ids = params[:subcategory_ids]
@experience.language_ids = params[:language_ids]
@experience.amenity_ids = params[:amenity_ids]
redirect_to listing_experience_path(@experience)
end
# POST /experiences
# POST /experiences.json
def create
@experience = current_user.experiences.build(experience_params)
@experience.subcategory_ids = params[:subcategory_ids]
@experience.language_ids = params[:language_ids]
@experience.amenity_ids = params[:amenity_ids]
if @experience.save
redirect_to listing_experience_path(@experience), notice: "Saved..."
else
render :new, notice: "Something went wrong..."
end
end
# PATCH/PUT /experiences/1
# PATCH/PUT /experiences/1.json
def update
new_params = experience_params
new_params = experience_params.merge(active: true) if is_ready_experience
if @experience.update(new_params)
flash[:notice] = "Updated..."
else
flash[:notice] = "Something went wrong..."
end
redirect_back(fallback_location: request.referer)
end
# DELETE /experiences/1
# DELETE /experiences/1.json
def destroy
@experience.destroy
respond_to do |format|
format.html { redirect_to experiences_url, notice: 'Experience was successfully destroyed.' }
format.json { head :no_content }
end
def listing
end
def pricing
end
def description
end
def about
end
def photo_upload
@photos = @experience.photos
end
def amenities
@experience.amenity_ids = params[:amenity_ids]
end
def subcats
@experience.subcategory_ids = params[:subcategory_ids]
end
def languages
@experience.language_ids = params[:language_ids]
end
def timeslots
end
def location
end
def bookings
if current_user.admin_role?
redirect_to admin_url
elsif current_user.host_role?
@experiences = current_user.experiences.published
else
redirect_to root_path, alert: "You don't have permission. Apply to be a host!"
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_experience
@experience = Experience.find(params[:id])
@categories = Category.all.map{|c| [ c.name, c.id ] }
@photos = @experience.photos
end
def is_authorised
redirect_to root_path, alert: "You don't have permission" unless current_user.id == @experience.user_id
end
def set_subcategories
@subcategories = Subcategory.all
end
def set_languages
@languages = Language.all
end
def set_amenities
@amenities = Amenity.all
end
def is_ready_experience
!@experience.active && !@experience.name.blank? && !@experience.summary.blank? && !@experience.what_do.blank? && !@experience.host_bring.blank? && !@experience.location.blank? && !@experience.price.blank? && !@experience.length.blank? && !@experience.accomodate.blank? && !@experience.notracetip.blank? && !@experience.difficulty.blank? && !@experience.cat_help.blank? && !@experience.subcategories.blank? && !@experience.languages.blank? && !@experience.amenities.blank? && !@experience.photos.blank? && !@experience.user_bring.blank? && !@experience.what_learn.blank? && !@experience.about_guide.blank?
end
def has_inventory
@experience.timeslot.inventory > 0
end
# Never trust parameters from the scary internet, only allow the white list through.
def experience_params
params.require(:experience).permit(:name, :summary, :about_guide, :what_do, :host_bring, :user_bring, :what_learn,
:location, :price, :length, :difficulty, :cat_help, :accomodate, :notracetip, :subcategory_ids => [], :language_ids => [], :amenity_ids => [])
end
end
_form.html.erb
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h1 class="text-center">Create Your Experience</h1>
<div class="container">
<%= form_for @experience do |f| %>
<!-- Experience Name // Text Field -->
<div class="row">
<div class="form-group">
<label>Experience Name</label>
<%= f.text_field :name, placeholder: "Name of your experience", class: "form-control", required: true %>
</div>
</div>
<!-- Experience Description // Text Area -->
<div class="row">
<div class="form-group">
<label>Experience Description</label>
<%= f.text_area :summary, rows: 5, placeholder: "Describe your experience", class: "form-control", required: true %>
</div>
</div>
<!-- Location // Text Field -->
<div class="row">
<div class="form-group">
<label>Location</label>
<%= f.text_field :location, placeholder: "Where is your experience?", class: "form-control", required: true %>
</div>
</div>
<!-- Length of Trip // Dropdown Select -->
<div class="row">
<div class="select">
<div class="form-group">
<label>Category Type</label> <br/>
<%= f.select :cat_help, [["Activity", 1], ["Food & Beverage", 2], ["Vista Point", 3]],
id: "cat_help", prompt: "Select...", class: "form-control", required: true %>
</div>
</div>
<!-- Length of Trip // Dropdown Select -->
<div class="row">
<div class="col-md-3 select">
<div class="form-group">
<label>Length Type</label> <br/>
<%= f.select :length, [["Daytrip", "Daytrip"], ["Overnight", "Overnight"], ["Weekend Getaway", "Weekend"], ["Multi-Day", "Multi-Day"]],
id: "length", prompt: "Select...", class: "form-control", required: true %>
</div>
</div>
<!-- Difficulty Rating // Dropdown Select -->
<div class="row">
<div class="col-md-3 col-md-offset-1 select">
<div class="form-group">
<label>Difficulty</label> <br/>
<%= f.select :difficulty, [["Light", "Light"], ["Moderate", "Moderate"], ["Advanced", "Advanced"], ["Extreme", "Extreme"]],
id: "difficulty", prompt: "Select...", class: "form-control", required: true %>
</div>
</div>
<!-- Group Size // Dropdown Select -->
<div class="col-md-3 col-md-offset-1 select">
<div class="form-group">
<label>Group Size</label> <br/>
<%= f.select :accomodate, [["1", 1], ["2-4", 2], ["5-8", 3], ["8+", 4]],
id: "accomodate", prompt: "Select...", class: "form-control", required: true %>
</div>
</div>
</div>
<!-- Final Submit // Button -->
<div>
<%= f.submit "Create Your Experience", class: "btn btn-normal btn-block" %>
</div>
<% end %>
</div>
</div>
</div>
从Rails路由输出 - 更新了新路由
Prefix Verb URI Pattern Controller#Action
bookings_experience GET /experiences/:id/bookings(.:format) experiences#bookings
dashboard_experience GET /experiences/:id/dashboard(.:format) experiences#dashboard
listing_experience GET /experiences/:id/listing(.:format) experiences#listing
pricing_experience GET /experiences/:id/pricing(.:format) experiences#pricing
description_experience GET /experiences/:id/description(.:format) experiences#description
photo_upload_experience GET /experiences/:id/photo_upload(.:format) experiences#photo_upload
amenities_experience GET /experiences/:id/amenities(.:format) experiences#amenities
location_experience GET /experiences/:id/location(.:format) experiences#location
about_experience GET /experiences/:id/about(.:format) experiences#about
subcats_experience GET /experiences/:id/subcats(.:format) experiences#subcats
languages_experience GET /experiences/:id/languages(.:format) experiences#languages
timeslots_experience GET /experiences/:id/timeslots(.:format) experiences#timeslots
experience_photos POST /experiences/:experience_id/photos(.:format) photos#create
experience_photo DELETE /experiences/:experience_id/photos/:id(.:format) photos#destroy
experience_timeslots POST /experiences/:experience_id/timeslots(.:format) timeslots#create
experience_timeslot DELETE /experiences/:experience_id/timeslots/:id(.:format) timeslots#destroy
experience_reservations POST /experiences/:experience_id/reservations(.:format) reservations#create
experience_calendars GET /experiences/:experience_id/calendars(.:format) calendars#index
POST /experiences/:experience_id/calendars(.:format) calendars#create
new_experience_calendar GET /experiences/:experience_id/calendars/new(.:format) calendars#new
edit_experience_calendar GET /experiences/:experience_id/calendars/:id/edit(.:format) calendars#edit
experience_calendar GET /experiences/:experience_id/calendars/:id(.:format) calendars#show
PATCH /experiences/:experience_id/calendars/:id(.:format) calendars#update
PUT /experiences/:experience_id/calendars/:id(.:format) calendars#update
DELETE /experiences/:experience_id/calendars/:id(.:format) calendars#destroy
experience_subcategories GET /experiences/:experience_id/subcategories(.:format) subcategories#index
POST /experiences/:experience_id/subcategories(.:format) subcategories#create
new_experience_subcategory GET /experiences/:experience_id/subcategories/new(.:format) subcategories#new
edit_experience_subcategory GET /experiences/:experience_id/subcategories/:id/edit(.:format) subcategories#edit
experience_subcategory GET /experiences/:experience_id/subcategories/:id(.:format) subcategories#show
PATCH /experiences/:experience_id/subcategories/:id(.:format) subcategories#update
PUT /experiences/:experience_id/subcategories/:id(.:format) subcategories#update
DELETE /experiences/:experience_id/subcategories/:id(.:format) subcategories#destroy
experiences GET /experiences(.:format) experiences#index
POST /experiences(.:format) experiences#create
new_experience GET /experiences/new(.:format) experiences#new
edit_experience GET /experiences/:id/edit(.:format) experiences#edit
experience GET /experiences/:id(.:format) experiences#show
PATCH /experiences/:id(.:format) experiences#update
PUT /experiences/:id(.:format) experiences#update
DELETE /experiences/:id(.:format) experiences#destroy
subcategory_experiences GET /subcategories/:subcategory_id/experiences(.:format) experiences#index
POST /subcategories/:subcategory_id/experiences(.:format) experiences#create
new_subcategory_experience GET /subcategories/:subcategory_id/experiences/new(.:format) experiences#new
edit_subcategory_experience GET /subcategories/:subcategory_id/experiences/:id/edit(.:format) experiences#edit
subcategory_experience GET /subcategories/:subcategory_id/experiences/:id(.:format) experiences#show
PATCH /subcategories/:subcategory_id/experiences/:id(.:format) experiences#update
PUT /subcategories/:subcategory_id/experiences/:id(.:format) experiences#update
DELETE /subcategories/:subcategory_id/experiences/:id(.:format) experiences#destroy
subcategories GET /subcategories(.:format) subcategories#index
POST /subcategories(.:format) subcategories#create
new_subcategory GET /subcategories/new(.:format) subcategories#new
edit_subcategory GET /subcategories/:id/edit(.:format) subcategories#edit
subcategory GET /subcategories/:id(.:format) subcategories#show
PATCH /subcategories/:id(.:format) subcategories#update
PUT /subcategories/:id(.:format) subcategories#update
DELETE /subcategories/:id(.:format) subcategories#destroy
category_experiences GET /categories/:category_id/experiences(.:format) experiences#index
POST /categories/:category_id/experiences(.:format) experiences#create
new_category_experience GET /categories/:category_id/experiences/new(.:format) experiences#new
edit_category_experience GET /categories/:category_id/experiences/:id/edit(.:format) experiences#edit
category_experience GET /categories/:category_id/experiences/:id(.:format) experiences#show
PATCH /categories/:category_id/experiences/:id(.:format) experiences#update
PUT /categories/:category_id/experiences/:id(.:format) experiences#update
DELETE /categories/:category_id/experiences/:id(.:format) experiences#destroy
category_subcategories GET /categories/:category_id/subcategories(.:format) subcategories#index
POST /categories/:category_id/subcategories(.:format) subcategories#create
new_category_subcategory GET /categories/:category_id/subcategories/new(.:format) subcategories#new
edit_category_subcategory GET /categories/:category_id/subcategories/:id/edit(.:format) subcategories#edit
category_subcategory GET /categories/:category_id/subcategories/:id(.:format) subcategories#show
PATCH /categories/:category_id/subcategories/:id(.:format) subcategories#update
PUT /categories/:category_id/subcategories/:id(.:format) subcategories#update
DELETE /categories/:category_id/subcategories/:id(.:format) subcategories#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
答案 0 :(得分:2)
尝试将 _form.html.erb 设为:
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h1 class="text-center">Create Your Experience</h1>
<div class="container">
<%= form_for @experience, url: experiences_path, method: :post do |f| %>
<%= render partial: '/path/to/form', locals: { f: f } %>
<% end %>
</div>
</div>
</div>
然后 new.html.erb :
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :experiences do
collection do
get 'bookings'
get 'dashboard'
get 'listing'
get 'pricing'
get 'description'
get 'photo_upload'
get 'amenities'
get 'location'
get 'about'
get 'subcats'
get 'languages'
get 'timeslots'
end
resources :photos, only: [:create, :destroy]
resources :timeslots, only: [:create, :destroy]
resources :reservations, only: [:create]
resources :calendars
resources :subcategories
end
resources :subcategories do
resources :experiences
end
resources :categories do
resources :experiences
resources :subcategories
end
devise_for :users, controllers: {
sessions: 'users/sessions',
passwords: 'users/passwords',
registrations: 'users/registrations'
}
namespace :admin do
get '', to: 'dashboard#index', as: '/'
end
resources :host_requests do
resources :users
end
resources :static_pages do
resources :users
end
resources :users, only: [:show]
get '/host_calendar' => "calendars#host"
get 'search' => 'static_pages#search'
root to: "static_pages#home"
暂时将 routes.rb 更新为:然后使用新的佣金路线输出更新您的答案
gsutil mb -p my-project-id gs://my-bucket-name