アプリで Globalize3 を動作させようとしています。翻訳したいブログ投稿のおもちゃのプロジェクトがあります。
私のURLは次のようになります:localhost/en/posts
localhost/fr/posts
これが私のやり方ですApplicationController
before_action :set_locale
def set_locale
I18n.locale = params[:locale] if params[:locale] || I18n.default_locale
end
form_for
投稿の作成と更新にも同じものを使用しています。ビューコードは次のとおりです。
<%= form_for(@post) do |f| %>
...
<% end %>
ページに移動すると正常に動作し/new
ます。コントローラーコードは次のとおりです。
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to action: :show, id: @post.id
else
render 'new'
end
end
しかし、投稿を url/en/posts/1/edit
で編集しようとすると、渡されたパラメータが混在しますform_for
。それがエラーメッセージです:
{:action=>"show", :controller=>"posts", :locale=>#, :id=>nil, :format=>nil} に一致するルートがありません。必要なキーがありません: [:locale, :id]
私の質問は次のとおりです。なぜそれが行われ、どうすればthixを修正できますか?
form_for
宣言を次のように変更するなど、すでにいくつかのことを試しました。
<%= form_for(@post, url: {action: 'show', id: @post, locale: params[:locale]} ) do |f| %>
私の投稿にupdate
はnew
id
No route matches {:action=>"show", :locale=>"en", :id=>#<Post id: nil, created_at: nil, updated_at: nil, title: nil, text: nil>, :controller=>"posts"}
そうです、作成と更新に2つのフォームを使用することを避けることができれば、実際には使用したくありません。これを行う良い方法はありますか?
編集:
ここに私のレーキルートがあります
Prefix Verb URI Pattern Controller#Action
posts GET /:locale/posts(.:format) posts#index {:locale=>/en|fr/}
POST /:locale/posts(.:format) posts#create {:locale=>/en|fr/}
new_post GET /:locale/posts/new(.:format) posts#new {:locale=>/en|fr/}
edit_post GET /:locale/posts/:id/edit(.:format) posts#edit {:locale=>/en|fr/}
post GET /:locale/posts/:id(.:format) posts#show {:locale=>/en|fr/}
PATCH /:locale/posts/:id(.:format) posts#update {:locale=>/en|fr/}
PUT /:locale/posts/:id(.:format) posts#update {:locale=>/en|fr/}
DELETE /:locale/posts/:id(.:format) posts#destroy {:locale=>/en|fr/}