1

アプリケーションで i18n 国際化を使用しようとしました。

表示 (インデックス)、作成、および編集を行うと、非常にうまく機能します。私の問題は、更新を行うときです。

更新ボタンを押しても、:locale の値が返されません

通常、これらの値が返されます。

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
level: '5_3'
menu_item: '5'
menu_level: '5_3_1'
action: index
controller: communication_mails
locale: dk

URL は次のようになります:
localhost:3000/dk/communication_mails

[編集] ボタンを押すと、これらの値が返されます。(「ボタン」はリンクです)。ロケールはまだ大丈夫です。

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
level: '5_3'
menu_item: '5'
menu_level: '5_3_2'
action: edit
controller: communication_mails
locale: dk
id: '2'

URL は次のようになります: localhost:3000/dk/communication_mails/2/edit

私の問題は、フォーム フィールドの下にある [更新] ボタンを押すと、リダイレクトによってロケールの値が返されないことです...代わりに ID が使用されます。

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
level: '5_3'
menu_item: '5'
menu_level: '5_3_1'
action: index
controller: communication_mails
locale: '2'

URL は次のようになります: localhost:3000/2/communication_mails

私のルートは次のようになります。

 scope "/:locale" do
    resources :communication_mails
  end


  resources :communication_mails do
    collection do
      get 'communication_mails'
      post 'communication_mails'
      put 'communication_mails'
    end
  end

  root :to => redirect("/dk/tests/")
  match '/communication_mails/:id/edit(.:format)', :to => 'communication_mails#edit'
  match '/communication_mails/:id/', :to => 'communication_mails#index'
  match '/communication_mails', :to => 'communication_mails#index'

私のコントローラーはこのように作られています。

  def edit
    @communication_mail = CommunicationMail.find(params[:id])
    @show_calendars = Calendar.where(:calendar_priority => '1')
    @show_email_text = CommunicationMail.find_by_sql("SELECT calendars.id,
    calendar_name, communication_mail_headline, communication_mail_text
    FROM calendars LEFT OUTER JOIN communication_mails 
    ON communication_mails.communication_mail_calendar_id = calendars.id
    WHERE communication_mails.communication_mail_priority = '1'")
  end

  def update
    @communication_mail = CommunicationMail.update(params[:id], params[:communication_mail])
    @show_calendars = Calendar.where(:calendar_priority => '1')
    @show_email_text = CommunicationMail.find_by_sql("SELECT calendars.id,
    calendar_name, communication_mail_headline, communication_mail_text
    FROM calendars LEFT OUTER JOIN communication_mails 
    ON communication_mails.communication_mail_calendar_id = calendars.id
    WHERE communication_mails.communication_mail_priority = '1'")
    if @communication_mail
      redirect_to communication_mails_path(:menu_item => '5', :level => '5_3', :menu_level => '5_3_1'), 
        :notice => 'Opdateringen af E-mail tekst lykkedes'
    else
      redirect_to communication_mails_path(:menu_item => '5', :level => '5_3', :menu_level => '5_3_1'), 
        :notice => 'Opdateringen af E-mail tekst lykkedes IKKE'
    end    
  end

これらのページを見て、私は多くの疑問を解決しました。しかし、私はこれに対する答えを見つけることができません。誰かが私を助けてくれることを願っています。よろしくお願いします。

アップデート

私のルートは次のようになります。

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

データの送信と更新に使用するフォーム

<%= form_for(@communication_mail) do |f| %>
  <table class="toggle_header2">
    <tr>
      <td class="nobr width50px font_size13 font_weight_bold color03 border_left">
        <%= t('forms.calendar') %> 
      </td>
      <td class="color03 border_right">      
        <%= collection_select(:communication_mail, 
          :communication_mail_calendar_id, 
          @show_calendars, 
          :id, 
          :calendar_name, 
          {:include_blank => true }, 
          {:prompt => true})
       %>
      </td>
    </tr>
    <tr>
      <td class="nobr width50px font_size13 font_weight_bold color03 border_left">
        <%= t('forms.headline') %>
      </td>
      <td class="color03 border_right">
        <%= f.text_field :communication_mail_headline, :class => 'width350px' %>
      </td>
    </tr>

    <tr>
      <td class="nobr align_top font_size13 font_weight_bold color03 border_left">
        <%= t('forms.text') %>
      </td>
      <td class="color03 border_right">
        <%= f.text_area :communication_mail_text, :class => 'width350px' %>
      </td>
    </tr>

    <tr>
      <td class="color03 border_left">
        <%= f.hidden_field :communication_mail_priority, :value => '1' %>
      </td>
      <td class="color03 border_right">
      <% if action_name == 'edit' || action_name == 'update'  %>
        <%= f.submit t('buttons.update') %> 
      <% else %>
        <%= f.submit t('buttons.insert') %> 
      <% end %>
      </td>
    </tr>
  </table>
<% end %>
4

1 に答える 1

0

パラメータから値を選択する:localeと、ルーティングに間違ったアプローチがあります。

更新: update アクションで :locale 値をルーティング ヘルパーに渡していることを確認してください。

index, show, new, create, edit, update, destroyこのコードは、アクションのみのスコープでルートを生成します。

scope "/:locale" do
  resources :communication_mails
end

このコードは :communication_mails にルートを追加しますが、スコープはありません。

  resources :communication_mails do
    collection do
      get 'communication_mails'
      post 'communication_mails'
      put 'communication_mails'
    end
  end

解決策は結合することです。すべての:communication_mailsアクションに対して :scope を定義する必要があります。

scope "/:locale" do
  resources :communication_mails do
    collection do
      get 'communication_mails'
      post 'communication_mails'
      put 'communication_mails'
    end
  end
end

ルーティングに関するドキュメントをお読みください (基本です): http://guides.rubyonrails.org/routing.html

于 2012-05-27T08:24:42.560 に答える