模型文件在类声明之外没有代码.我添加了index,new,show和_form .HTML.erb的视图页面.
config / routes.rb包含行资源:external_databases.该模型目前不包含嵌套资源.此应用程序中还声明了其他模型,但目前它们都没有与此模型交互,并且在声明资源之前它们都经过测试并且功能和关闭:external_databases.
我有一个从索引视图到new_external_database_path的链接,其行为与{:action =>完全相同“new”,:controller => “external_database”}如果我正确关注.
这理论上应该加载application / external_databases / new,它将呈现_form.HTML.erb. _form的第一行是<%= form_for(@ external_database)do | f | %GT; 使用/ new链接时会出现帖子标题中所述的问题. url application / external_databases / new出现以下错误: 没有路线匹配{:action =>“show”,:controller =>“external_databases”}
当我使用rails控制台创建数据成员时,它通过索引和显示正确显示. edit方法使用相同的_form文件,并成功编辑控制台创建的数据成员.毁灭也功能.
……那么为什么不认识新方法呢?
我的这个模型的控制器代码:
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 > <%= f.label :name %><br/> <%= f.text_fIEld :name %><br/> </div> <div > <%= f.label :description %><br/> <%= f.text_fIEld :description %><br/> </div> <div > <%= f.label :url %><br/> <%= f.text_fIEld :url %><br/> </div> <div > <%= 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',:method => :delete %>解决方法 您的“返回”链接中的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 %>总结
以上是内存溢出为你收集整理的ruby-on-rails – Rails Routing – :action =>’new’返回错误“没有路由匹配{:action =>”show“…在同一个控制器中全部内容,希望文章能够帮你解决ruby-on-rails – Rails Routing – :action =>’new’返回错误“没有路由匹配{:action =>”show“…在同一个控制器中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)