Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:30:55+00:00 2026-05-25T21:30:55+00:00

I asked a question similar to this one about a week ago, but this

  • 0

I asked a question similar to this one about a week ago, but this is a slightly different perspective on it. The nature of the question involves being redirected to the correct controller.

I have a single resource, posts, and I have 4 different categories these posts can be under. I want each of these categories to be particular to a single controllers, and so I have the following in my routes.rb:

resources "code", :controller => :code_posts, :as => :code
resources "sports", :controller => :sports_posts, :as => :sports
resources "gaming", :controller => :game_posts, :as => :gaming
resources "the-nation", :controller => :personal_posts, :as => :the_nation

So now I can access posts through URLs like, for example, /code/1, /sports/34 to access the same post resource, but with each controller focusing on a single scope, namely a particular category.

This is all well and good, but my issue comes up when I try to edit or save particular posts. I have the following partial _form.html.erb (rendered in the new and edit views) in all the view folders for their particular controller:

<%= form_for @post do |f| %>
<div class="field">
  <%= f.label :author %><br/>
  <%= f.text_field :author %>
</div>
<div class="field">
    <%= f.label :title %><br/>
    <%= f.text_field :title %>
</div>
<div class="field">
  <%= f.label :category %>
  <%= f.select :category_id, Category.all.collect {|c| [c.name, c.id] }, {:include_blank => true} %>
</div>
<div class="field">
  <%= f.label :summary %><br/>
  <%= f.text_area :summary, :rows => 5 %>
</div>
<div class="field">
  <%= f.label :body %><br/>
  <%= f.text_area :body %>
</div>
<div class="field">
  <%= f.label :tag_tokens %><br/>
  <%= f.text_field :tag_tokens, "data-pre" => @post.tags.map(&:attributes).to_json %>
</div>
<div class="field">
    <%= f.submit "Submit" %>
</div>
<% end %>

So whenever I create or update a post, through whichever controllers, I always get redirected back to /posts/4, /posts/123, /posts/:id, whatever. I want to get redirected to the particular controller the post being edited or created lives under. So if I go to /code/new, and submit the new post, I want to be redirected to /code/1234, and not /posts/1234. How can I do this? For some reason I’m just having major mental mind blocks this morning. Thanks.

EDIT Updated <%= form_for @post do |f| %> to <%= form_for @post, :url => code_url(@post) do |f| %> and it works for /code/1/edit but not /code/new. When trying to access a new post form, I get the following error:

No route matches {:action=>"show", :controller=>"code_posts", :id=>#<Post id: nil, author: "Les Peabody", summary: nil, body: nil, created_at: nil, updated_at: nil, title: nil, category_id: 1, slug: nil>}

This is my CodePostsController#new method

def new
  @post = Post.new(:category => Category.find_by_name("Programming"), :author => current_user.full_name)
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:30:56+00:00Added an answer on May 25, 2026 at 9:30 pm

    So, ultimately what is important is how the form gets turned into HTML. If you look at the differences between a form that is meant for editing, and a form that is meant for a new object, there is only one thing that is ever really different that matters – the action URL.

    In the case of a new form, the form tag should look something like:

    <form accept-charset="UTF-8" action="/code" class="new_post" id="new_post" method="post">
    

    and in the case of an edit form:

    <form accept-charset="UTF-8" action="/code/1" class="edit_post" id="edit_post_1" method="post">
    

    The only thing that matters to rails however is the name of the input elements (which are constant in both forms) and the action attribute in the form tag. That tells Rails whether or not it’s rendering the edit or create action.

    Since we’re splitting up control of a single resource through multiple controllers, the standard form_for @post will not suffice since Rails can no longer automate the rendering process through convention (as we’re doing a very unconventional thing). It is necessary to do some manual labor. The following will do the trick.

    Convert the partial to the following:

    <%= form_for @post, :url => path do |f| %>
    <div class="field">
      <%= f.label :author %><br/>
      <%= f.text_field :author %>
    </div>
    <div class="field">
        <%= f.label :title %><br/>
        <%= f.text_field :title %>
    </div>
    <div class="field">
      <%= f.label :category %>
      <%= f.select :category_id, Category.all.collect {|c| [c.name, c.id] }, {:include_blank => true} %>
    </div>
    <div class="field">
      <%= f.label :summary %><br/>
      <%= f.text_area :summary, :rows => 5 %>
    </div>
    <div class="field">
      <%= f.label :body %><br/>
      <%= f.text_area :body %>
    </div>
    <div class="field">
      <%= f.label :tag_tokens %><br/>
      <%= f.text_field :tag_tokens, "data-pre" => @post.tags.map(&:attributes).to_json %>
    </div>
    <div class="field">
        <%= f.submit "Submit" %>
    </div>
    <% end %>
    

    The path variable in there is a variable passed in through the :locals mechanism in the partial render, like so:

    new.html.erb

    <%= render :partial => "form", :locals => {:path => code_index_path} %>
    

    and edit.html.erb

    <%= render :partial => "form", :locals => {:path => code_path(@post)} %>
    

    The nice thing with this solution is you can DRY up the code too by placing _form.html.erb in app/views/layouts or app/views/posts and reuse it in all of the new and edit views for all controllers that manipulate the Post resource in a consistent fashion. So rather than having:

    <%= render :partial => "form", :locals => {:path => code_path(@post)} %>
    

    we have:

    <%= render :partial => "layouts/form", :locals => {:path => code_path(@post)} %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already asked a similar question but this one is a bit different/specific: I'm
I asked a question similar to this one a couple of weeks ago, but
I asked this question about a year ago on another site but never got
This question is similar to another one I asked , but whereas in that
I know this sounds strange earlier I asked about similar question in one of
First of all, I know I asked a similar question before but this one
I know this question has been asked about a dozen times, but this one
My question is similar to the one being asked here , but I am
I asked similar question sometime ago, but I am making new one to be
I have asked a question similar to this in the past but this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.