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 

私のレーキルートには次のものがあります:-

      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

良い出発点は、常に次の方法でルートを印刷することです。

rake routes

次に、コントローラーで show メソッドを定義する必要があります。

def show
  ..
end

そしてビュー:

app/views/settings/show.html.erb

resources :settingsはショーパスがあることを認識する必要があります。魔女は次のようになります。

settings_path(user.id)

resources :settings2 回含める必要はありません。

UserControllerそして、それはあなたの推測とは何の関係もありません。重要な部分はSettingController. /view/user/show.html.erb?に SettingsShow リンクを表示すると、エラーが表示されることがあります。(100% 確実ではありません。もっとコードを見る必要がありますSettingController。興味深いでしょう)。

于 2013-03-12T12:21:08.447 に答える
0

new.html.erb次のような代わりにフォームを入れてくださいcreate.html.erb

new.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 がポスト メソッドであると言うようなことはありません。また、コントローラーの新しいメソッドで定義したものとしてで@settingはなく、コード全体で使用する必要があります。@settings

于 2013-03-12T12:48:48.050 に答える