我一直在关注教程http://edgeguides.rubyonrails.org/getting_started.html
虽然遵循每一步但是将文章更改为Tour(我是RoR的新手) - 但现在在尝试编辑特定游览时,消息会出现:
ActionComtroller :: UrlGenerationError at / tours / 3 / edit 没有路线匹配{:action =>" show",:controller =>" tour",: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操作都有效。 任何帮助,并确定它是我缺少的基本东西..
感谢。
答案 0 :(得分:0)
巡视控制器中似乎没有编辑操作
def edit
@tour = Tour.find(params[:id])
end