Ruby on Rails Friday, July 15, 2016

> On Jul 14, 2016, at 10:21 PM, Padmahasa B N <lists@ruby-forum.com> wrote:
>
>> This signals to me that your new action does not begin with a / or a
>> http(s)://.
>>
>> This is normal browser behavior.
>>
>> If you assign a fully-qualified URL or a complete (root-relative) path
>> to either an a#href or form#action, the browser will accept that. But if
>> your new URL or path is not complete, then the browser will attempt to
>> "canonicalize" it by adding the current context to it.
>>
>> Walter
>
> Hello Walter, it does begin with http://. Another weird thing is if
> current URL is localhost..../reports/new, It only discards "/new" part
> but keeps "/reports" part. After some time I got to know that "/reports"
> is controller and "/new" is action. That's why I want to change
> controller dynamically. And as rails converts <% form_tag %> hash to a
> valid URL its not converting JS supplied URL.
>
> But I'm OK if I have to use fully qualified URL but need some help with
> how I have to tell rails to match that URL to a controller's action.
> Finally all I want is to change both controller and action using
> JavaScript(which may not possible at all because "controller" hash is of
> form_tag helper). Or a workaround equivalent to this.

Once the HTML is in the page, and JavaScript is working on it, the form_tag helper has nothing more to say about it. form_tag is Ruby that writes HTML. JavaScript acts on that HTML and modifies the DOM in the browser -- Ruby is all done at that point.

I suspect now that the issue may be in your JavaScript. I know this technique works, because I wrote something that did exactly what you describe not too long ago. I don't have it in front of me, but the basic idea was to add data- attributes to the various buttons, and then register a click handler to use that data- attribute as the form's Action. Off the top of my head, something like this:

$(document).on('click', 'input[data-action]', function(){
$(this).closest('form').attr('action', $(this).data('action'));
});

If that doesn't work, then the handler may have to be registered on the form's submit method instead, but I think that the click on the button will register and complete before the form within it gets the message to submit itself.

To set up that data attribute, you would simply add it to the f.submit helper like this:

<%= f.submit data: { action: '/some/complete/route' } %>

Walter

>
> Thank you.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a7d3cff6183e3b7d3a307d182329bd3d%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/018C146A-6787-480B-9633-5F774F7A6513%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment