Note that there are some explanatory texts on larger screens.

plurals
  1. POform_for mixing id and locale parameters
    primarykey
    data
    text
    <p>I'm trying to get Globalize3 working on my app. I have the toy projet of blog posts, that I want to translate.</p> <p>My urls look like this : <code>localhost/en/posts</code> <code>localhost/fr/posts</code></p> <p>Here's how I do it in my <code>ApplicationController</code></p> <pre><code>before_action :set_locale def set_locale I18n.locale = params[:locale] if params[:locale] || I18n.default_locale end </code></pre> <p>I'm using the same <code>form_for</code> for creating and updating posts. Here's the view code :</p> <pre><code>&lt;%= form_for(@post) do |f| %&gt; ... &lt;% end %&gt; </code></pre> <p>It works fine for when I go to the <code>/new</code> page, here's the controller code: </p> <pre><code>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 </code></pre> <p>But when I try to edit a post with the url <code>/en/posts/1/edit</code>, it mixes the parameters passed to the <code>form_for</code>. That's the error message: </p> <blockquote> <p>No route matches {:action=>"show", :controller=>"posts", :locale=>#, :id=>nil, :format=>nil} missing required keys: [:locale, :id]</p> </blockquote> <p>My question is : why does it do that and how can I fix thix?</p> <hr> <p>I've already tried some things like changing the <code>form_for</code> declaration to this:</p> <p><code>&lt;%= form_for(@post, url: {action: 'show', id: @post, locale: params[:locale]} ) do |f| %&gt;</code></p> <p>so it works with <code>update</code> but not with <code>new</code> because my post doesn't have an <code>id</code> </p> <pre><code>No route matches {:action=&gt;"show", :locale=&gt;"en", :id=&gt;#&lt;Post id: nil, created_at: nil, updated_at: nil, title: nil, text: nil&gt;, :controller=&gt;"posts"} </code></pre> <p>So yeah, I don't really want to use 2 forms for create and update if I can avoid doing it. Is there a good way of doing this?</p> <p>edit:</p> <p>here's my rake routes</p> <pre><code> Prefix Verb URI Pattern Controller#Action posts GET /:locale/posts(.:format) posts#index {:locale=&gt;/en|fr/} POST /:locale/posts(.:format) posts#create {:locale=&gt;/en|fr/} new_post GET /:locale/posts/new(.:format) posts#new {:locale=&gt;/en|fr/} edit_post GET /:locale/posts/:id/edit(.:format) posts#edit {:locale=&gt;/en|fr/} post GET /:locale/posts/:id(.:format) posts#show {:locale=&gt;/en|fr/} PATCH /:locale/posts/:id(.:format) posts#update {:locale=&gt;/en|fr/} PUT /:locale/posts/:id(.:format) posts#update {:locale=&gt;/en|fr/} DELETE /:locale/posts/:id(.:format) posts#destroy {:locale=&gt;/en|fr/} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload