In _form.html.erb
:
<%= simple_form_for(@activity,:remote=>true, html: {multipart: true}) do |f| %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :logo%>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
In create.js.erb
:
$(document).ready(function(){
<%if @activity.save %>
$('#divShow').html("<%= j (render 'activities/index') %>");
$('#myModal').modal('hide');
$('#notice').html("Activities is successfully created.").show();
<%else%>
$('.modal-body').html("<%= j (render 'activities/form') %>");
<%end%>
});
In activities_controller
:
def create
@activity = Activity.new(activity_params)
@activity.save
end
In logs
:
Processing by ActivitiesController#create as HTML
Parameters: {"utf8"=>"✓", "activity"=>{"name"=>"sds", "logo"=>#, @original_filename="1241112.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"activity[logo]\"; filename=\"1241112.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Create Activity"}
Can't verify CSRF token authenticity
Completed 422 Unprocessable Entity in 1ms
ActionController::InvalidAuthenticityToken
In the view page i am using simple_form
gem and CURD operation with JS. But, i am getting error from file upload(:logo) while creating a new record.Please help me out,thanks.
protect_from_forgery with: :null_session, :if => Proc.new { |c| c.request.format == 'application/json' }
to yourapplicationscontroller
– Sonalkumar suteProc
class available bydefault, you can call is as procedure – Sonalkumar suterespond_to do |format| format.js } end
– Sonalkumar sutecreate.js
in views – Sonalkumar suteActionController::UnknownFormat
inrespond_to do |format|
and i have alreadycreate.js.erb
file...plz see above – Uday kumar das