0

我收到以下错误

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

在尝试实施设置/新建时。

我的 settings/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>
Name: <%= f.text_field :name %><br>

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

<% end %>

我的 routes.rb 设置文件是:

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

    collection do
     get  'add'
     get  'list' => "settings#index"
     post 'get_settings'
     get  'get_settings'
    end
  end

  resources :settings 

我的设置控制器:

class SettingsController < ApplicationController
  # GET /Settings
  # GET /Settings.json
  def index
    @settings = Setting.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @settings }
    end
  end

  # GET /Settings/1
  # GET /Settings/1.json
  def show
    @setting = Setting.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @setting }
    end
  end

  # GET /Settings/new
  # GET /Settings/new.json
  def new
    @setting = Setting.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @setting }
    end
  end

  # GET /Settings/1/edit
  def edit
    @setting = Setting.find(params[:id])
  end

  # POST /Settings
  # POST /Settings.json
  def create
    @setting = Setting.new(params[:setting])

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

  # PUT /Settings/1
  # PUT /Settings/1.json
  def update
    @setting = Setting.find(params[:id])

    respond_to do |format|
      if @Setting.update_attributes(params[:setting])
        format.html { redirect_to @setting, notice: 'Lov value was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @setting.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /Settings/1
  # DELETE /Settings/1.json
  def delete
    @setting = Setting.find(params[:id])
    @setting.deleted = 1
    @setting.save

    respond_to do |format|
      format.html { redirect_to settings_url }
      format.json { render :json => { :success => true } }
    end
  end
end

我的耙子路线如下:-

               add_settings POST   /settings/:id/add(.:format)            settings#add

            remove_settings POST   /settings/:id/remove(.:format)         settings#remove

       settings_id_settings GET    /settings/:id/settings_id(.:format)    settings#settings_id

         add_settings_index GET    /settings/add(.:format)                settings#add

        list_settings_index GET    /settings/list(.:format)               settings#index

get_settings_settings_index POST   /settings/get_settings(.:format)       settings#get_settings

                           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

请检查并告诉我。

4

2 回答 2

0

成功保存后,您的Create操作将重定向到。Show

但是,即使您的控制器有方法,您routes.rb也没有 的条目。ShowShow

您可以尝试运行此命令rake routes吗?这将列出您的 rails 应用程序的所有有效路线。

于 2013-03-09T07:01:37.687 回答
0

你只需要在你的路由文件中使用资源 :settings,rails RESTFUL 路由会处理所有事情。您不需要显式声明用于 restful 路由的 HTTP 动词。
同样在您的控制器的创建和更新操作中使用@setting 而不是@Setting。

于 2013-03-09T09:45:33.177 回答