Rails remote: true only if requested through ajax - ruby-on-rails

I've got my app working as I want it without AJAX.
I'm adding AJAX now to an item listing page with an 'add item' button.
Normally, clicking 'add item' it'd just go to the new item page, but with ajax it injects the new item form onto the page. On submit it updates the listing. This is all working fine, too.
However, if I visit the new item page directly, upon submitting the form it still fires the javascript. I want avoid any AJAX form submission if I'm on the form page itself.
Is it possible to only add remote => true to my form tag if the page has been requested through AJAX?
(...or suggest a better way to do this)

request.xml_http_request? will tell you if you are serving an ajax request:
<% if request.xml_http_request? %>
....
<% end %>

Related

back button for form preview in rails

I have a problem while making a back button for form in rails.
I have a dynamic form that looks like this:
dynamic form
However, when I press 'preview' button, the page will redirect to another page. In this page, I have created button:
<%= button_to 'Back to edit', 'javascript:history.go(-1);', {class:'btn btn-primary'} %>
It really back to previous form, but only 1 single form displayed
returned form
Can anyone can help me???
You can do this follow as:
<%= link_to 'Back to edit', :back %>
Most browsers will cache the back action and therefore won't issue an HTTP request to your server. If you need to display fresh data, then use a redirect via javascript's window.location rather than relying on the history state.

Rails root form will only submit when entered directly in browser

My root view has has a form, which renders but doesn't submit when a user reaches the page from a link:
<%= link_to 'home', root_path, class: 'navbar-brand' %>
If you access the root page directly through the browser (e.g. http://localhost:3000/, or refresh after link), the form submits.
Just getting started with rails (4.2), so I'm sure there's something really basic I'm missing. I'd really appreciate advice!
Add data-no-turbolink="true" inside your view's body tag.
Was solved by looking at Form Submit button only works after reload.

Submit the form after submit ( Ruby on Rails )

I am a newbie on Ruby on Rails
I have a regular form in a "new" page.
I hope when the user fulfilled the form and submit this form.
The page can popup a bootstrap modal and display the full content the user just typed in the form.(like a confirm page)
How can I do this?
I found this
http://archive.railsforum.com/viewtopic.php?id=11255
seems it can help me solve my problem
but can I change <div id="preview"> to a bootstrap modal(or other popup)?
You can use JavaScript to show the modal based on an onclick or onsubmit event
<%= submit_tag, :onclick => "launchConfirmPage();" %>
where launchConfirmPage() is some JS function that shows the modal filled with info. Then in that modal, have the actual submit button for the form. I'm not entirely sure of the specifics of how a submit_tag works, but you can also try using an anchor tag or button_tag to achieve the same thing if that doesn't work.

How to trigger Rails remote action on page ready

I've made a Rails app using UJS and remote=true links.
For a controller#index action, instead of showing the content on page load, I'd like to call the #index action via Javascript once the page has loaded.
In short, I want to trigger the same functionality that is carried out when clicking a link like this:
<%= link_to "My Link", index_controller_action, :remote => true %>
But I want this to happen on 'page ready' rather than having to click the link.
Should I use the JQuery $ajax function for this? I have hacked it for now by triggering a false 'click' action on the remote link, but I'm sure this isn't best practice...

Posting a form through popup in Rails

I have a model called Details, and two controller methods new and create. New method displays a form to create Details (in new.html.erb), which gets posted to the Create method that saves it. (when it succesffully saves, it it renders the new.html.erb file with the details.) This works as expected.
Now i want a separate page with a link to fill in these details. I want the click to do the intended work through a popup, example redbox. When you click on that link, a popup should show the form, whose submit should post the form, and if it is successfully done, then refresh the original page. If the post is unsaved, then the same form should show the errors. What do i need to do to make it work in Ror? I guess i need some stuff to go in new.js.rjs and maybe create.js.rjs, but i can't really figure out what to put in those files.
Redbox updates the page's dom to add a div at the end of it. So your form is a part of the main page.
So if you add a form tag in this redbox, all your page will be reloaded as there's only one.
So you add anywhere in your page (preferably at the end) :
<div id="redbox" style="display: none;">
<%= form_for #details do %>
# Whatever form fields you want here
<% end -%>
</div>
You do a link that'll open the redbox :
<%= link_to_redbox 'Open The Details Form', 'redbox' %>
This will display your redbox with your form.
And as it is the same page, not a new windows, when you'll validate your form, it'll reload the all of it.
I used a link_to_remote_redbox for this, which on clicking, fetches the form rendered by the new method call in a popup widnow. it submits to the create function and it is now working. actually i had previously made it work without ajax and i was having difficulty in making it work with ajax. the problem was solved once i made separate partials for ajax calls, and doing:
respond_to do |format|
format.js { render :template => 'detail/ajax_template'}
...
end
I provided different templates for both create and new method calls, used the :html => {:id => 'some-id'} in form and the :update => {:some-id} to replace the form with the message that it worked correctly.
So I didnt know that i needed separate templates for the different styles of forms, and that i needed to use the :update option to replace the form when i asked the above question.

Resources