0

我是 Rails 新手。尝试实施创建操作时出现以下错误。

Routing Error

No route matches {:action=>"show", :controller=>"settings", :format=>nil}

我的控制器文件如下:-

   @settings = Setting.new(params[:settings])

respond_to do |format|
  if @settings.save
    format.html { redirect_to @settings, notice: 'Setting was successfully created.' }
    format.json { render json: @settings, status: :created, location: @settings }
  else
    format.html { render action: "new" }
    format.json { render json: @settings.errors, status: :unprocessable_entity }
  end
end

结尾

我的 Routes.rb 文件如下:-

resources :settings do
    member do
     post 'add'
     post 'remove'
     get 'settings/id'
    end

    collection do
     get  'add'
     get  'list' 
     post 'get_settings'
     get  'get_settings'
    end
  end

  resources :settings 

我的 rake 路线有以下几点:-

      GET    /settings/get_settings(.:format
settings#get_settings
                      GET    /settings(.:format)
settings#index
                      POST   /settings(.:format)
settings#create
                      GET    /settings/new(.:format)
settings#new
                      GET    /settings/:id/edit(.:format)
settings#edit
                      GET    /settings/:id(.:format)
settings#show
                      PUT    /settings/:id(.:format)
settings#update
                      DELETE /settings/:id(.:format)
settings#destroy
                      GET    /settings(.:format)
settings#index
                      POST   /settings(.:format)
settings#create
                      GET    /settings/new(.:format)
settings#new
                      GET    /settings/:id/edit(.:format)
settings#edit
                      GET    /settings/:id(.:format)
settings#show
                      PUT    /settings/:id(.:format)
settings#update
                      DELETE /settings/:id(.:format)
settings#destroy

我的 create.html.erb 如下:_

<%= form_for @setting  do |f| %>
  <% if @setting.errors.any? %>  
  <div id="errorExplanation">  
    <h2><%= pluralize(@setting.errors.count, "error") %> prohibited this setting from being saved:</h2>  
    <ul>  
    <% @setting.errors.full_messages.each do |msg| %>  
      <li><%= msg %></li>  
    <% end %>  
    </ul>  
  </div>  
  <% end %> 
</br></br>


Id: <%= f.text_field :id %><br>
Name: <%= f.text_field :name %><br>


<%= f.submit "Create" %>&nbsp;&nbsp;

我的错误日志是:-

Started GET "/settings/new" for 127.0.0.1 at 2013-03-12 18:57:09 +0530
Processing by SettingsController#new as HTML
  Rendered settings/new.html.erb within layouts/application (170.2ms)
Completed 500 Internal Server Error in 1112ms

ActionController::RoutingError (No route matches {:action=>"show", :controller=>
"settings", :format=>nil}):
  app/views/settings/new.html.erb:7:in `_app_views_settings_new_html_erb__979995
802_23360592'
  app/controllers/settings_controller.rb:29:in `new'


  Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.1
1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within resc
ues/layout (0.0ms)

我的 new.html:-

<h1>New setting</h1>




<%= form_for @setting  do |f| %>
  <% if @setting.errors.any? %>  
  <div id="errorExplanation">  
    <h2><%= pluralize(@setting.errors.count, "error") %> prohibited this setting from being saved:</h2>  
    <ul>  
    <% @setting.errors.full_messages.each do |msg| %>  
      <li><%= msg %></li>  
    <% end %>  
    </ul>  
  </div>  
  <% end %> 
</br></br>


Id: <%= f.text_field :id %><br>
First Name: <%= f.text_field :name %><br>


<%= f.submit "Create" %>&nbsp;&nbsp;

<% end %>




<%= link_to 'Back', settings_path %>

谁能帮帮我吗。

4

2 回答 2

0

把你的表格放进去,new.html.erb而不是create.html.erb像这样:

新的.html.erb

<%= form_for @setting  do |f| %>

#Your stuff

<% end %>

你的新动作是:

新动作

def new
 @setting = Setting.new
end 

你的控制器然后是-

创建动作:

更新:

def create
  if request.setting?
     @setting = Setting.new(params[:setting])
    # other setup for save
    if @setting.save
      flash[:notice] = 'Setting was successfully created.'
      redirect_to @setting
    else
      render :action => 'new'
    end
  end 
end

注意:create.html.erb当你说 create 是一个 post 方法时,没有这样的事情。此外,您应该@setting在整个代码中使用它,而不是@settings在新的控制器方法中定义它。

于 2013-03-12T12:48:48.050 回答
0

一个好的起点总是通过以下方式打印出您的路线:

rake routes

然后你需要在控制器中定义一个show方法:

def show
  ..
end

还有一个观点:

app/views/settings/show.html.erb

有了resources :settings他应该认识到有一条表演路径。女巫可能看起来像这样:

settings_path(user.id)

resources :settings不需要包含两次。

UserController我猜这与你无关。重要的部分是SettingController. /view/user/show.html.erb如果您在?上显示 SettingsShow 链接,可能会出现错误。(不是 100% 确定,需要查看更多代码,SettingController这会很有趣)。

于 2013-03-12T12:21:08.447 回答