【问题标题】:Update don´t get the value of local when doing an update using i18n使用 i18n 进行更新时,更新不会获取本地的值
【发布时间】:2012-05-27 08:15:08
【问题描述】:

我尝试在我的应用程序中使用 i18n 国际化。

当我进行显示(索引)、创建和编辑时,它工作得很好。我的问题是当我进行更新时。

当我按下更新按钮时,我没有恢复 :locale 值

我通常会恢复这些值。

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

网址如下所示:
本地主机: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 现在看起来像: 本地主机: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 现在看起来像: 本地主机: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 %>

【问题讨论】:

    标签: ruby-on-rails-3 internationalization


    【解决方案1】:

    如果您从参数中选择 :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

    【讨论】:

    • 您好论文谢谢您的回答...我试图确定所有操作的范围,但它不起作用。这是一个混淆除更新之外的所有其他操作的出价。
    • 嗯,奇怪。请在此处发布您的更新表格和rake routes 命令的结果。
    猜你喜欢
    • 1970-01-01
    • 2021-03-03
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    相关资源
    最近更新 更多