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.
To resolve this in the activities_controller.rb put this on the top:
skip_before_filter :verify_authenticity_token, :only => :create
Also you have to define templates, so the view will be:
def create
@activity = Activity.new(activity_params)
respond_to do |format|
if @activity.save(activity_params)
format.js {}
else
format.js {}
end
end
end
I am using gem "remotipart"
to solve the issue:
1.Add gem "remotipart"
to your gemfile.
2.Run bundle exec rails g remotipart:install
.
3.Add <%= javascript_include_tag :defaults %>
to the layout page.
4.Add //= require jquery.remotipart
to the application.js
file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.