Rails root form will only submit when entered directly in browser - ruby-on-rails

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.

Related

Rails 6 prompt to download on mobile

I have a Rails 6 app where users can submit changes to a model and a confirmation email will be sent to site admins. The changes are submitted through a modal popup and once submitted the popup is replaced with another that confirms that the email has been sent.
The problem I am having is when this is done on a mobile device (I'm using an iPhone, not sure if this happens on Android), once the change is submitted another popup appears prompting me to download a file of size 0 with the name of the model being updated (it happens in both Safari and Chrome). This doesn't happen on the desktop version of the site or in the mobile emulator on the desktop, so I can't think of how I could diagnose this issue.
Here is the code being called just before the download popup appears:
<%= button_tag "Submit", type: 'submit',
id: 'modal-subimt',
class: 'btn btn-primary',
onClick: 'replaceModal()' %>
Here is the create function being called when the form is submitted:
def create
... # Setting the parameters for the model being changed
if #model.save
#model.send_confirmation_email()
else
# Irrelevant because the email gets sent
end
end
Here is the send_confirmation_email function being called in create:
def send_confirmation_email()
UserMailer.model_confirmation(self).deliver_now
end
And here is the model_confirmation function being called by send_confirmation_email:
def model_confirmation(model)
#model = model
recipient = <admin email>
mail to: recipient, subject: "Model Confirmation"
end
Nowhere in this code can I see where I might be prompted to download a file, but alas it is happening. Any help would be appreciated on how to properly diagnose or solve this problem. Thanks!
EDIT:
I changed my code so that the model would be updated without sending a confirmation email Also, the modal is no longer replaced with the new one verifying an email has been sent, and I am still having the same issue. This leads me to believe that the problem is something with the creation of the model.
EDIT 2:
Previously I was creating the model and remaining on the page (with the confirmation that the email was sent). I changed my controller to redirect_to request.referer to reload the page once the change is made, and when that is done I am no longer prompted to download the empty file. Unfortunately, the way I want this to work, reloading the page isn't optimal. Is there any reason that I would be prompted for a download when updating a model without reloading the page?
SOLUTION:
I was able to solve this problem by adding remote: true to the form_for line like the following:
<%= form_for(Model.new, remote: true) do |f| %>
...
<%= button_tag "Submit", type: 'submit',
id: 'modal-subimt',
class: 'btn btn-primary',
onClick: 'replaceModal()' %>
<% end %>
I'm not sure why exactly this solved my problem though so if anyone can provide some insight that would be much appreciated!
The problem is related to how you instantiated the form_for. When you submit the form to a Model.new object, form_for try to infer the controller with the create method for handling it. See more how form_for works in https://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
With that said, the create method is probably outputting something or nothing instead of redirecting, that's why you get a download on a mobile device and in browsers it's just an empty page because they can handle it. If there is no render on the method, Rails will answer with a 204 No Content or an empty text page. You must check your method about this.
When you use remote: true, I will quote from the docs:
:remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the submit behavior. By default this behavior is an ajax submit.
So instead of reloading the page, UJS sends a XMLHttpRequest to the "backend" page, that's why you don't get a redirect. The result of the submit is treated without redirecting. But based on the result of the request the content of the page can come. So it's important that you give a response accordingly. In the links below you will find the right way to do it.
See more in these questions:
How does ':remote => true' works in rails
:remote => true confusion with form_for
Also, in the Rails Guides:
https://guides.rubyonrails.org/form_helpers.html
https://guides.rubyonrails.org/working_with_javascript_in_rails.html
This is also a good reference:
http://www.korenlc.com/remote-true-in-rails-forms/

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.

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.

Rails remote: true only if requested through ajax

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 %>

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