2014-06-11 53 views
1

通過「使用rails 4.0進行敏捷web開發」學習RoR,並在本地化選擇器(第15.4章)中遇到問題。沒有路由匹配[POST]「/ store/index」

在〜/應用/視圖/佈局/ application.html.erb

<!DOCTYPE html> 
<html> 
<head> 
    <title>Pragprog Books Online Store</title> 
    <%= stylesheet_link_tag "application", media: "all", 
    "data-turbolinks-track" => true %> 
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 
    <%= csrf_meta_tags %> 
</head> 
<body class="<%= controller.controller_name %>"> 
    <div id="banner"> 
    <%= form_tag store_index_path, class: 'locale' do %> 
     <%= select_tag 'set_locale', 
     options_for_select(LANGUAGES, I18n.locale.to_s), 
     onchange: 'this.form.submit()' %> 
     <%= submit_tag 'submit' %> 
     <%= javascript_tag "$('.locale input').hide()" %> 
    <% end %> 
    <%= image_tag("logo.png") %> 
    <%= @page_title || t('.title') %> 
    </div> 
    <div id="columns"> 
    <div id="side"> 
     <% if @cart %> 
     <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %> 
      <%= render @cart %> 
     <% end %> 
     <% end %> 

     <ul> 
     <li><a href="http://www...."><%= t('.home') %></a></li> 
     <li><a href="http://www..../faq"><%= t('.questions') %></a></li> 
     <li><a href="http://www..../news"><%= t('.news') %></a></li> 
     <li><a href="http://www..../contact"><%= t('.contact') %></a></li> 
     </ul> 

     <% if session[:user_id] %> 
     <ul> 
      <li><%= link_to 'Orders', orders_path %></li> 
      <li><%= link_to 'Products', products_path %></li> 
      <li><%= link_to 'Users', users_path %></li> 
     </ul> 
     <%= button_to 'Logout', logout_path, method: :delete %> 
     <% end %> 

    </div> 
    <div id="main"> 
     <%= yield %> 
    </div> 
    </div> 
</body> 
</html> 

在〜/應用程序/控制器/ store_controller.rb

class StoreController < ApplicationController 
    skip_before_action :authorize 
    include CurrentCart 
    before_action :set_cart 
    def index 
    if params[:set_locale] 
     redirect_to store_index_url(locale: params[:set_locale]) 
    else 
     @products = Product.order(:title) 
    end 
    end 
end 

當我從選擇器中選擇的區域設置,我有一個路由錯誤:沒有路由匹配[POST]「/ store/index」

請幫忙。在GitHub上

全部項目:https://github.com/hronny/depot

+0

問題可能與POST ...索引操作通常是GET –

回答

1

問題是您的索引操作路由只響應GET請求(這是默認行爲),但您要做的是向此操作提交表單(默認情況下爲POST請求)。

有兩種方法來解決此

  1. 設置相應的路線,按您的要求:

    match 'store/index', to: 'store#index', via: [:get, :post] 
    
  2. 讓你的表單提交一個GET請求,而不是默認的POST請求

    <%= form_tag store_index_path, class: 'locale', method: 'GET' do %> 
    

編輯

順便說一句,我不明白你正試圖在這裏做:[:區域設置],然後

if params[:set_locale] 
     redirect_to store_index_url(locale: params[:set_locale]) 
    else 
     @products = Product.order(:title) 
    end 

這將只是使用參數重定向回同樣的動作你最終得到的是Product.order(:title)。另外,我還沒有讀過這本書。

+0

這是本書中的代碼。我想,如果使用默認語言環境(不設置任何語言環境),請直接訪問product.order。如果設置任何語言環境,請顯示本地化的產品。訂單。本地化:針對不同語言的I18n和YML文件。所以在任何情況下我都會得到product.order – hronny

0

在你的路由文件,你只響應get 'store/index'

您必須還要回應POST(因爲這就是您的「選擇語言」表單正在執行的操作 - POST請求),或者更改表單以執行GET請求。