【发布时间】:2016-07-13 02:23:30
【问题描述】:
我一直在关注一个教程http://edgeguides.rubyonrails.org/getting_started.html
虽然遵循每一步但将文章更改为巡回赛(我是 RoR 的新手) - 但现在在尝试编辑特定巡回赛时会出现消息:
ActionController::UrlGenerationError 在 /tours/3/edit 没有路线匹配 {:action=>"show", :controller=>"tours", :id=>nil} 缺少必需的键:[:id] - 有更好的错误它指向我的edit.html.erb
edit.html.erb
<h1>Editing TOURS</h1>
<%= form_for :tour, url: tour_path(@tour), method: :patch do |f| %>
<% if @tour.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@tour.errors.count, "error") %> prohibited
this tour from being saved:
</h2>
<ul>
<% @tour.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', tours_path %>
routes.rb
Prefix Verb URI Pattern Controller#Action
tours GET /tours(.:format) tours#index
POST /tours(.:format) tours#create
new_tour GET /tours/new(.:format) tours#new
edit_tour GET /tours/:id/edit(.:format) tours#edit
tour GET /tours/:id(.:format) tours#show
PATCH /tours/:id(.:format) tours#update
PUT /tours/:id(.:format) tours#update
DELETE /tours/:id(.:format) tours#destroy
static_about GET /static/about(.:format) static#about
static_contact GET /static/contact(.:format) static#contact
root GET / static#home
tours_controller.rb
class ToursController < ApplicationController
def index
@tours = Tour.all
end
def new
@tour = Tour.new
end
def show
@tour = Tour.find(params[:id])
end
def create
@tour = Tour.new(tour_params)
if @tour.save
redirect_to @tour
else
render 'new'
end
end
def update
@tour = Tour.find(params[:id])
if @tour.update(tour_params)
redirect_to @tour
else
render 'edit'
end
end
private
def tour_params
params.require(:tour).permit(:title, :text)
end
end
-- 除了编辑操作之外,我所有的其他 CRUD 操作都有效。 任何帮助,并确保这是我缺少的基本内容..
谢谢。
【问题讨论】:
标签: ruby-on-rails actioncontroller