1
votes

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.

2
Try adding protect_from_forgery with: :null_session, :if => Proc.new { |c| c.request.format == 'application/json' } to your applicationscontrollerSonalkumar sute
It's a Proc class available bydefault, you can call is as procedureSonalkumar sute
In your controller create method, you can specify that respond_to do |format| format.js } endSonalkumar sute
and add a file create.js in viewsSonalkumar sute
I am getting ActionController::UnknownFormat in respond_to do |format| and i have already create.js.erb file...plz see aboveUday kumar das

2 Answers

1
votes

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
0
votes

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.