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从参数中选择您的值,那么您在路由中的方法不正确。

更新:确保在更新操作时将 :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 回答