I have a problem due to scopes and the form_for helper in rails 3. The routes - file looks like this:
scope "(/:tab)" do
resources :article
end
The form looks something like this:
<%= form_for(@article) %>
<%= f.label :title %>
<%= f.text_field :title %>
etc.
<%end%>
The tab - attribute is stored in params[:tab], as a string My problem is that this genereate wrong urls in the form. How could I get this to work ? The genreated url article_path(params[:tab], @article) works perfectly fine
In a very similar situation I defined the scope in routes like below:
Now I have helpers like
election_questions_path(@election)
In the forms I can use:
In above examples
@election
is an instance of the Election model.After integrating Friendly_id into this solution I got some pretty urls. For example "http://mydomain.com/elections-2012/questions/my-question"
I have found this to be a really irritating problem and have gotten around this for now with the following monkey patch. Generic like this, it's a little bid off as you're simply passing the entire bag of parameters to polymorphic_url which is what form_for uses under the hood to guess the route. A more concise approach would be to merge just the scope value.
My solution:
https://gist.github.com/1848467
The answer I came up with was quite ugly, but works with both update and create:
Update: A better solution would be to override the default_url_options-method to something like this:
Then the <%= form_for @article do |f| %> could be used, and all urls are correctly generated
You could specify the path explicitly:
My solution of similar problem with form_for and scopes is to define new method in
helpers/<model_name>/<model_name>_helper.rb
, for example mine is sessions_helper.rb which containsAnd in my view I made
Problematic routes.rb part
... and to get managed with
:tab
param you may add it to the helper method.Try: