我正在为名为“ExternalDatabase”的模型编写代码。
模型文件在类声明之外没有代码。我添加了index,new,show和_form .html.erb的视图页面。
config / routes.rb包含行resources :external_databases
。该模型目前不包含嵌套资源。此应用程序中还声明了其他模型,但目前它们都没有与此模型交互,并且在声明resources :external_databases
之前它们都经过测试并且功能正常并已关闭。
我有一个从索引视图到new_external_database_path
的链接,如果我正确关注,其行为与{:action => "new", :controller => "external_database"}
完全相同。
理论上,这应该加载将呈现_form.html.erb
的application / external_databases / new。 _form
的第一行是<%= form_for(@external_database) do |f| %>
使用/ new链接时会出现帖子标题中所述的问题。 url application / external_databases / new出现以下错误:
No route matches {:action=>"show", :controller=>"external_databases"}
当我使用rails console
创建数据成员时,它通过索引和显示正确显示。 _form
方法使用相同的edit
文件,并成功编辑控制台创建的数据成员。毁灭也功能。
...那么为什么不识别新方法呢?
此模型的控制器代码:
class ExternalDatabasesController < ApplicationController
def index
@external_databases = ExternalDatabase.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @external_databases }
end
end
# POST /external_databases
# POST /external_databases.json
def new
@external_database = ExternalDatabase.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @external_database }
end
end
def show
@external_database = ExternalDatabase.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json {render :json => @external_database }
end
end
def create
@external_database = ExternalDatabase.new(params[:external_database])
respond_to do |format|
if @external_database.save
format.html {redirect_to @external_database, :notice => "New External Database Profile was created successfully!"}
format.json {render :json => @external_database, :status => :created, :location => @external_database}
else
format.html {render :action => "new"}
format.json { render :json => @external_database.errors, :status => :unprocessable_entity }
end
end
end
#GET /external_databases/1/edit
def edit
@external_database = ExternalDatabase.find(params[:id])
end
# PUT /external_databases/1
# PUT /external_databases/1.json
def update
@external_database = ExternalDatabase.find(params[:id])
respond_to do |format|
if @external_database.update_attributes(params[:external_database])
format.html {redirect_to @external_database, :notice => "External Database Profile was updated successfully!" }
format.json {head :ok}
else
format.html { redner :action => "edit" }
format.json {render :json => @external_database.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /external_databases/1
# DELETE /external_databases/1.json
def destroy
@external_database = ExternalDatabase.find(params[:id])
@external_database.destroy
respond_to do |format|
format.html { redirect_to external_databases_rul }
format.json { head :ok }
end
end
end
更新:添加了routes.rb和Views代码
我的routes.rb文件:
App::Application.routes.draw do
resources :type_as do
resources :type_bs
end
resources :type_bs do
resources :type_as
resources :type_cs
end
resources :type_cs do
resources :type_bs
end
resources :external_databases
root :to => "home#index"
end
查看:
external_databases_form
<%= form_for(@external_database) do |f| %>
<div class="field">
<%= f.label :name %><br/>
<%= f.text_field :name %><br/>
</div>
<div class="field">
<%= f.label :description %><br/>
<%= f.text_field :description %><br/>
</div>
<div class="field">
<%= f.label :url %><br/>
<%= f.text_field :url %><br/>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
index.html.erb
<p id="notice"><%= notice %></p>
<h1>External Databases</h1>
<table border="1">
<tr>
<th>Database Name</th>
</tr>
<% @external_databases.each do |exdb| %>
<tr>
<td><%= exdb.name %></td>
<td><%= truncate(exdb.description) %></td>
<td><%= link_to 'Show', exdb %></td>
<!-- link_to.... :target => "_blank" will open the url in a new window -->
<td><%= link_to 'Visit', exdb.url, :target => "_blank" %></td>
<td><%= link_to 'Edit', edit_external_database_path(exdb)%></td>
<td><%= link_to 'Destroy', exdb, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br/>
<%= link_to 'New External Database Profile', { :action => "new", :controller => "external_databases" }%> |
<%= link_to 'Home', root_path %>
new.html.erb
<h1>Creating a new External Database Profile</h1>
<%= render 'form' %>
<%= link_to 'Back', external_database_path %>
show.html.erb
<p id="notice"><%= notice %></p>
<table cellspacing="3" cellpadding="5" border="1">
<tr>
<th><b>External Database Name</b></th>
<th><b>Database ID</b></th>
</tr>
<tr>
<td><%= @external_database.name %></td>
<td><%= @external_database.id %></td>
</tr>
<tr colspan="2">
<th><b>URL</b></th>
</tr>
<tr colspan="2">
<td><%= @external_database.url %></td>
<td><%= link_to 'Visit External Database', @external_database.url %></td>
</tr>
</table>
<p>
<h3>Description</h3>
<!% @external_database.description.split.scan(/.{,60}/).each do |line| %>
<!%= line %><br/>
<!% end %>
</p>
<br /><br /><br />
<%= link_to 'Back to External Database Index', external_databases_path %> |
<%= link_to 'Edit', edit_external_database_path(@external_database) %> |
<%= link_to 'Destroy', :confirm => 'Are you sure?', :method => :delete %>
答案 0 :(得分:2)
您的“后退”链接中的new.html.erb文件中有拼写错误。它应该是:
external_databases_path
您可以将它与上面show.html.erb中您自己的反向链接进行比较。
修正:new.html.erb
<h1>Creating a new External Database Profile</h1>
<%= render 'form' %>
<%= link_to 'Back', external_databases_path %>
答案 1 :(得分:0)
我遇到了类似的问题。按照这里的说明没有运气。在我的form_for提交代码的基础上,我有了“创建新”和“删除”按钮。原来我的create new因为一个狡猾的删除按钮而产生错误:/。无论如何,我至少可以评论一下是为了让新按钮工作,然后专注于调试删除按钮。
= f.submit "Submit", class: "btn btn-large btn-primary"
= link_to 'Create New', new_matrix_header_path, class: 'btn btn-primary'
-#= link_to 'Delete', matrix_header_path(@matrix_header), method: :delete, data: { confirm: 'Are you sure?'}, class: 'btn btn-danger'
我查看了堆栈,发现它在我的视图的第13行附近开始出现错误,这是我删除按钮的行。
Started GET "/matrix_headers/new" for 127.0.0.1 at 2014-10-28 21:08:48 +1000
Processing by MatrixHeadersController#new as HTML
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Rendered shared/_error_messages.html.erb (0.4ms)
Rendered matrix_headers/_form.html.haml (21.0ms)
Rendered matrix_headers/new.html.haml within layouts/application (22.6ms)
Completed 500 Internal Server Error in 36ms
ActionController::UrlGenerationError - No route matches {:action=>"show", :controller=>"matrix_headers", :format=>nil, :id=>#<MatrixHeader id: nil>} missing required keys: [:id]:
actionpack (4.1.1) lib/action_dispatch/journey/formatter.rb:39:in `generate'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:597:in `generate'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:627:in `generate'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:663:in `url_for'
actionpack (4.1.1) lib/action_dispatch/routing/url_for.rb:155:in `url_for'
actionview (4.1.1) lib/action_view/routing_url_for.rb:83:in `url_for'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:228:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:179:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:268:in `block (2 levels) in define_url_helper'
() media/jay/DATA/rails_projects/my_app/app/views/matrix_headers/_form.html.haml:13:in `block in _app_views_matrix_headers__form_html_haml__2669648707298145379_8667520'
通过注释掉删除按钮,它现在可以正确路由到新的,而不会出现No route matches {:action=>"show"
错误。