title: Ruby on Rails: how to render a view from same controller but with a different route?
body: <p>I have a controller, <code>farms_controller</code>, that is being used for two different routes, <code>farms</code> and <code>person_farms</code>; the two routes use the same folder for view templates (i.e., <code>new_person_farm_path</code> and <code>new_farm_path</code> use the same template, <code>new.html.erb</code>). I want to access the farms register pages both separately and inside the person register, where some functionalities are different using the URL in some tests to show different functionalities (via <code>request.env['PATH_INFO'].include? 'person_farms'</code>, because I don't know a best way).</p>
  
  <p>When I submit without the required fields (name) from the <code>new_person_farm_path</code>, the create function renders the <code>new_farm_path</code> page with a different styling for incorrect fields (using the 'twitter-bootstrap-rails' gem). I want it to instead render the <code>new_person_farm_path</code> page, with the same template file but a different routing (different URL). I tried to use <code>redirect_to</code> and it shows the page in the correct URL, but not the styling in the wrong fields.</p>
  
  <p>However, all the instructions I saw in <a href="http://guides.rubyonrails.org/layouts_and_rendering.html#using-render" rel="nofollow">Rails documentation for rendering</a> are above rendering a specific file, but it's not what I need. I have a feeling that it's not the "Rails way", but I'm a starter on RoR and this a legacy system that is being rewritten to Rails, so I can't correct the DB logic for now.</p>
  
  <p>So, is there a way to render a view from same controller but with a different route?</p>
  
  <hr>
  
  <p>My code:</p>
  
  <pre><code>def create
    @farm = Farm.new(farm_params)
    respond_to do |format|
      if @farm.save
        # Param sent by the page to test if I'm in a person_farms page
        # instead of a farms page because the request.env test doesn't work here.
        # I feel that this is not the correct way to do that, but I can leave the correct way to another question.
        # However, if someone has a suggestion a comment would be appreciated.
        if params[:is_person_farm_page]
          format.html { redirect_to @person_farm_path(@farm), notice: 'Farm saved' }
          format.json { render :show, status: :created, location: @farm }
        else
          format.html { redirect_to @farm, notice: 'Farm saved' }
          format.json { render :show, status: :created, location: @farm }
        end
      else
        #This is the point where I want to redirect to new_person_farm_path
        #I would need to test using the params[:is_person_farm_page] above
        format.html { render :new }
        format.json { render json: @farm.errors, status: :unprocessable_entity }
      end
    end
  end
  </code></pre>