I am currently trying to use rails client_side_validations gem with rails 5 but it just does not work, I´ve been trying to use form_with tag and also form_for.
I was trying to follow this:
https://github.com/DavyJonesLocker/client_side_validations/wiki/Bootstrap-Modal-Validationscode
In the last link it shows that the form code should produce html like this:
<form data-validate="true" ...>
but for me it doesnt even add the data-validate attribute...
And here is my _new.html.erb code:
<%= form_for #invmtoproducto, validate: true, :remote => true do |form| %>
...fields
<% end %>
Here the _new.js.erb file for the modal:
$('#modalW').html("<%= j (render 'new') %>");
$('#myModal').modal('show');
$("#modalW").slideDown(350);
$('#myModal').on('shown.bs.modal', function () {
$('#modalW').focus()
})
So, what should I do?
Related
I'm trying to place a bootstrap modal to create a model inside another model's new page. The html follows:
new.html:
<div class="page-header">
<h1>Novo Parceiro</h1>
</div>
<%= render :partial => 'form' %>
<%= render :partial => "/contatos/form" %>
_form.html:
<%= form_for #parceiro, :html => { :class => "ketchup" } do |f| %>
(fields and submit here...)
<% end %>
<script type="text/javascript">
// some javascript here OUTSIDE the form
</script>
But what happens when the page is loaded is that everything inside new.html, including the partial "/contatos/form" which is an ajax-able form for another model, is being rendered inside the tag for #parceiro.
Why does Rails do this, and how do I keep my second form outside the first? As it is, I can't use AJAX because nested forms don't work very well (in fact it's wrong even for the html specifications)
[EDIT]: Issue solved, I had a div in the first form with a self closing tag (), so I changed that to a regular closing tag and it now works.
I am using HAML based templates and pagination seems to be broken for me with Kaminari. I'm sure it's my fault but here's what my template looks like:
:javascript
$(function() {
$('#events').html('<%= escape_javascript render(#events) %>');
$('#paginator').html('<%= escape_javascript(paginate(#events, :remote => true).to_s) %>');
});
%ul.activity_list
#events
= render :partial => 'event'
my _event.html.haml looks like:
- #events.each do |event|
= display_event(event)
And finally:
%nav
%ul.pagination
#paginator
= paginate #events, :remote => true
What happens now when I load the page is where the paginated events should be I literally see the following markup:
<%= escape_javascript render(#events) %gt;
And it renders on the site as:
<%= escape_javascript render(#events) %>
What am I doing wrong here to get XHR enabled pagination here?
UPDATE
I've updated my javascript to the following as per Dylan's request:
:javascript
$(function() {
$('#events').html('#{escape_javascript render('event')}');
$('#paginator').html('#{escape_javascript(paginate(#events, :remote => true).to_s)}');
});
It seems XHR is working, but for some reason, it will never go past page=2. This particular fetch should have 3 pages total and it'll only flip between pages 1 and 2. Any reason for this?
You're trying to use erb tags in a haml file. You should be using haml's version of interpolating ruby code (#{} instead of <%= %>):
:javascript
$(function() {
$('#events').html('#{escape_javascript render(#events)}');
$('#paginator').html('#{escape_javascript(paginate(#events, :remote => true).to_s)}');
});
See the Haml documentation about this
I have been trying to create a multi-part form in Rails for the last day, which is crazy, but I am really not sure how to get around this one.
Currently, here is the code in my view:
<%= form_for #account, :html => {:multipart => true } do |f| %>
However, the HTML returned is as follows:
<form accept-charset="UTF-8" action="/accounts/1" class="edit_account" id="edit_account_1" method="post">
For the life of me I can't figure out why the form is not showing up as a multi-part. This particular form is used to upload an image using paperclip to AWS, but fails each time, presumably because it isn't a multipart form.
Help! :) And thanks.
Hi according to Rails API v3.1.3, your code should look like following:
<%= form_for #account,{},:html => {:multipart => true } do |f| %>
The difference is by passing empty options to rails helper and it will read your html parameters.
Thanks
UPDATE:
Here is the code copied from one of my projects:
It is working and runs under Rails 3.1
May be you could try to put brackets after the "form_for"
<%= form_for(#account,{},:html => { :id=>"account_form",:multipart => true }) do |f| %>
<%= render :partial => "form", :object => f %>
<%= f.submit 'create' %>
<% end %>
This worked for me.
<%= form_for(#account, html: { :multipart => true }) do |f| %>
or
<%= form_for(#account, html: { :enctype => 'multipart/form-data' }) do |f| %>
As per #peterpengnz's answer, providing an empty {} parameter to form_for got ArgumentError:
wrong number of arguments (3 for 1..2)
It turns out that I'm a huge idiot, and the original form was working fine, EXCEPT...
I was rendering the form in a partial, but I wrapped the partial in a standard, non-multipart form tag, which overwrote the multipart form, and somewhat surprisingly didn't raise any errors.
Either way, I am a stupid one for not noticing this, but it is now resolved and the file uploading is working perfectly.
This form to update a work item does not work anymore.
The form is generated in the erb file using a this command:
<%= form_for(#work_item, :url => admin_workitem_update_path) do |f| %>
The generated tag looks like this:
<form accept-charset="UTF-8" action="/admin/workitem/define/14" class="edit_workitem" enctype="multipart/form-data" id="edit_workitem_14" method="put">
</form>
The route looks like this:
admin_workitem_update PUT /admin/workitem/define/:workitem_id(.:format)
Follow-up: using :method => :post in the form tag works.
Why does rails generate the put method attribute?
NEVER use equal sign befor form. I have the same problem with Instant Rails implementation. The tutorial on rails documentation should be used without <&=
<%= form_for(#work_item, :url => admin_workitem_update_path) do |f| %>
use insteand:
<% form_for(#work_item, :url => admin_workitem_update_path) do |f| %>
this is all
I have the following form in my Rails application:
<% form_tag password_resets_path, :id => 'recoverPasswordForm' do %>
<label for="passwordRecoveryEmailAddress">Email Address:</label>
<%= text_field_tag "passwordRecoveryEmailAddress" %>
<%= submit_tag 'Recover' %>
<br />
<div id="forgotPasswordLoginLinkContainer">
<a id="forgotPasswordLoginLink" href="/login">Login Instead</a>
</div>
<% end %>
When this form is submitted, the page must reload. I would like to easily turn this form into an AJAX form, such that the form submits via AJAX, and a page reload does not happen.
I could do this easily using jQuery, hooking into the .submit() function. But, I am curious: does Rails provide some easy way to turn any given form into an AJAX form? Or, what's the simplest (yet elegant) way possible? Maybe something like
<% form_tag password_resets_path, :id => 'recoverPasswordForm', :ajax => true do %>
I'm using Rails 2.
Yes, and you were close with your guess. Rails 3 allows you to do form_tag ..., :remote => true to let the form use AJAX if Javascript is available.
See http://railsapi.com/doc/rails-v3.0.0/classes/ActionView/Helpers/FormTagHelper.html#M002483