首先发布这里...做ROR教程并找到关于这个问题的类似问题......但当然没有成功。无论如何,在教程之后一切正常,除非点击这个链接我得到错误The action 'edit1' could not be found for SubjectsController
...非常感谢任何帮助!我自己和教程都使用相同版本... 4.0。
SubjectsController:
class SubjectsController < ApplicationController
layout false
def index
@subjects = Subject.sorted
end
def show
@subject = Subject.find(params[:id])
end
def new
@subject = Subject.new({:name => "Default"})
end
def create
# Instantiate a new object using form parameters
@subject = Subject.new(subject_params)
# Save the object
if @subject.save
# If save succeeds, redirect to the index action
redirect_to(:action => 'index')
else
# If save fails, redisplay the form so user can fix problems
render('new')
end
end
def edit
@subject = Subject.find(params[:id])
end
def update
# Find an existing object using form parameters
@subject = Subject.find(params[:id])
# Update the object
if @subject.update_attributes(subject_params)
# If update succeeds, redirect to the index action
redirect_to(:action => 'show', :id => @subject.id)
else
# If update fails, redisplay the form so user can fix problems
render('edit')
end
end
def delete
@subject = Subject.find(params[:id])
end
def destroy
subject = Subject.find(params[:id]).destroy
redirect_to(:action => 'index')
end
private
def subject_params
# same as using "params[:subject]", except that it:
# - raises an error if :subject is not present
# - allows listed attributes to be mass-assigned
params.require(:subject).permit(:name, :position, :visible)
end
end
Edit.html
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="subjects edit">
<h2>Update Subject</h2>
<%= form_for(:subject, :url => {:action => 'update', :id => @subject.id}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th>Visible</th>
<td><%= f.text_field(:visible) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Update Subject") %>
</div>
<% end %>
</div>
路由文件
SimpleCms::Application.routes.draw do
root "demo#index"
#get "demo/index"
match ':controller(/:action(:id))', :via => [:get, :post]
除此之外,我在Chrome和Windows 7上。
Code for Show link:
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="subjects show">
<h2>Show Subject</h2>
<table summary="Subject detail view">
<tr>
<th>Name</th>
<td><%= @subject.name %></td>
</tr>
<tr>
<th>Position</th>
<td><%= @subject.position %></td>
</tr>
<tr>
<th>Visible?</th>
<td><%= @subject.visible ? 'true' : 'false' %></td>
</tr>
<tr>
<th>Created</th>
<td><%= @subject.created_at %></td>
</tr>
<tr>
<th>Updated</th>
<td><%= @subject.updated_at %></td>
</tr>
</table>
</div>
Code for delete link:
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="subjects delete">
<h2>Delete Subject</h2>
<%= form_for(:subject, :url => {:action => 'destroy', :id => @subject.id}) do |f| %>
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name"><%= @subject.name %></p>
<div class="form-buttons">
<%= submit_tag("Delete Subject") %>
</div>
<% end %>
</div>
答案 0 :(得分:1)
您的控制器包含编辑操作,但不包含edit1。 edit1是由于路由设置无效而与参数连接的操作的结果。
在您的路线配置中,缺少斜线/
应该是这样的
match ':controller(/:action(/:id))', :via => [:get, :post]
^
|
答案 1 :(得分:0)
在&#39;匹配&#39;中添加斜杠Mladen建议的路线将解决您的直接问题。但是,不再建议使用该默认路由;最好明确你的路线。
这将为您提供&#39;标准&#39; REST-ful路线,取代&#39;匹配&#39;路线档案中的路线:
resources :subjects
如果您真的使用“删除”&#39;在你的控制器中你需要一个&#39;成员&#39;添加该条目的条目,但通常只有一个链接在节目和/或编辑表单上才能直接进入&#39; destroy&#39;删除记录的行动;不确定为什么你需要单独删除&#39;图。