0

In a rails 4.1.5 app, views/posts/new.html.erb contains some content before the actual form. So, I want the new action to go to posts/new#form_id where the form element's id is form_id.

How do I achieve this?

I have seen posts that suggest a redirect with anchor: 'form_id' , but since I would call this from the same action, I would go into a redirect loop.

I also considered changing this in the routes, but the route DSL has a special meaning for '#'.

Update 1:

I can think of two options:

a) redirect in the action:

views/posts/new.html.erb

def new
  @post = Post.new
  if params[:anchor] == 'form_id'
    render 'new'
  else
    redirect_to new_post_path(anchor: 'form_id')
  end
end  

Turns out this won't work because 1) anchor is not available on the server, and 2) without that, redirecting to the route with anchor will keep coming back to the same action handler.

b) In routes, override new and edit to append an anchor tag - something like:

routes.rb resources :posts do get 'new', to: 'new#form_id' get 'new', to: 'edit#form_id' end

But b will not work, since the '#' is interpreted as controller#action in this context.

Update 2: A third option: use ajax on the page:

I tried the below two AJAX options, and in both cases it does jump to the anchor tag, but it doesn't align the tag to the top of the page (the form shows up midway down the page) - even though I have at least two pages of content above the form, the form shows up misaligned from the top:

2a

<script>
    $(document).load(function() {
        $('html, body').animate({
            scrollTop: $("#form-header").offset().top
        }, 2000);
    });
  };

2b

<script>
 $(document).load(function() {
        window.location = window.location.href + '#form-header';
 };
Anand
  • 3,690
  • 4
  • 33
  • 64
  • 1
    **"I would go into a redirect loop"** what makes you think that? =) – D-side Dec 24 '14 at 07:35
  • Sorry, I have never used anchors from actions before. How would the action say : if anchor == 'form_id' render 'new' else redirect_to new_post_path, anchor: 'form_id'. Is it params[:anchor]? Am typing from mobile app. Will try on computer later and update. – Anand Dec 24 '14 at 09:56
  • According to below link, I cannot access the anchor on the server side - if that is the case, how do I avoid a redirect loop? : http://stackoverflow.com/questions/4108082/get-anchor-part-of-url-in-controller-in-ruby-on-rails – Anand Dec 24 '14 at 10:59
  • A redirect loop is only possible if you're redirecting from that endpoint. Are you? – D-side Dec 24 '14 at 11:02
  • Yes, I want the 'posts/new' action to redirect to `posts/new#form_id`. – Anand Dec 24 '14 at 11:07
  • See my update above for the intent of the code (that doesn't work) – Anand Dec 24 '14 at 11:22
  • A weird concept. Why not just place the form above and the info below and discard all the anchor hackery? This way you can't get into the `new` action without an anchor. If a part of the app is inaccessible for all, why make it? – D-side Dec 24 '14 at 11:31
  • @D-side - consider a scenario like a blog that shows all its posts on one page, and when you want to edit a post, it shows the edit post form in the context of the post list (ordered by time or popularity or some such). – Anand Dec 24 '14 at 16:06
  • I've only seen this pattern implemented using AJAX on the page, in-place. If you're providing a fallback implementation (which should rarely be used anyway), it's best to edit on a separate page. – D-side Dec 24 '14 at 22:15
  • Thanks, @D-side I tried two approaches with Ajax (see Update 2 above), but even when there is more than two pages of content above the form tag, the page does not align the form tag to the top. – Anand Dec 25 '14 at 02:24

0 Answers0