Reputation: 6925
I have an issue with controller action respond in Ruby on rails app.
So, I have two actions in my taskasset_controller with simple code:
def destroy
if params[:id].present?
@asset = TaskAsset.find(params[:id])
authorize @asset, :destroy?
@asset.destroy
respond_to do |format|
format.js { @asset.id }
end
end
end
def create
@task = Task.find(params[:task_id])
authorize @task
respond_to do |format|
if params[:images]
params[:images].each { |image|
@task.task_assets.create(image: image)
}
end
@task_assets = @task.task_assets
authorize @task_assets, :create?
format.js { @task_assets }
end
end
Also I have regulary created two forms destroy.js.erb and create.js.erb and in case of destroy when I delete image from form I got respond id of deleted image so that info I am using to hide modal preview of image.
But in case of create action I am getting error about:
ActionController::UnknownFormat
Extracted source (around line #14):
authorize @task
respond_to do |format|
if params[:images]
params[:images].each { |image|
@task.task_assets.create(image: image)
What em I missing in case of create action respond format?
Upvotes: 0
Views: 1515
Reputation: 6925
So solution in my case was:
**<script src="http://malsup.github.com/jquery.form.js"></script>**
**<%= form_for TaskAsset.new, url: task_assets_path(format: :js),remote: true, :html => {remote: true, :multipart => true,id: 'ajaxformtask' } do |form| %>**
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<%= hidden_field_tag 'task_id', task.id %>
<div class="form-group">
<div class="col-md-12">
<label>Select Images</label>
</div>
<%= file_field_tag "images[]", id: "imageUploadInput", type: :file, multiple: true, class:'form-control display-block' %>
</div>
<div class="actions">
<div class="padding-bottom-3x text-center">
<button type="button" id="cancel-add-task-assets" class="btn btn-lg btn-warning">Cancel</button>
<%= form.submit "Save", id:"add-task-assets", class: 'btn btn-lg task-btn-color' %>
</div>
</div>
<% end %>
**<script>
$(document).ready(function(){
$("#ajaxformtask").ajaxForm()
})
</script>**
http://malsup.github.com/jquery.form.js
into
project here is only example where we put it on partial view.Adding form ID name and (format: :js)
{remote: true, :multipart => true,id: 'ajaxformtask' } do |form| %>Adding:
$(document).ready(function(){ $("#ajaxformtask").ajaxForm() })
Here is also final look of create controller action:
def create
@task = Task.find(params[:task_id])
#@task = policy_scope(@venue.tasks).find(params[:task_id]).decorate
#authorize @task
if params[:images]
params[:images].each do |image|
@task.task_assets.create(image: image)
end
end
@task_assets = @task.task_assets
authorize @task_assets, :create?
@result = [@task_assets, @venue]
respond_to do |format|
format.json { @task_asset.to_json }
format.js{ @result }
#format.js {render js: 'alert("success")'} # now you cann append that image to te image list here or create create.js.erb ...ok I have that alreday let try
end
end
Upvotes: 0
Reputation: 2088
It might have something to do with you doing something inside respond_to
block without calling format.[your_format]
first.
Try changing your code to
respond_to do |format|
format.js do
if params[:images]
params[:images].each { |image|
@task.task_assets.create(image: image)
}
end
@task_assets = @task.task_assets
authorize @task_assets, :create?
render @task_assets
end
end
or another option
if params[:images]
params[:images].each do |image|
@task.task_assets.create(image: image)
end
end
@task_assets = @task.task_assets
authorize @task_assets, :create?
respond_to do |format|
format.js { @task_assets }
end
The point being that the only thing you can do inside a respond_to do |format| ... end
is to call one or multiple format.[some_format]{ what_to_do }
Check the documentation for more info
Upvotes: 1