deni5n
deni5n

Reputation: 101

form_tag + file_field_tag causes the controller to respond to format.html?

my routes.rb:

post 'home/review'

my home_conrtoller.rb:

def review
  @review = Review.new(review_params)
  @review.image = params[:review][:image]
  if @review.save
    respond_to do |format|
      format.js   { render 'review', locals: {review_name: @review.review_name, review_body: @review.review_body} }
    end
  else
    format.html { respond_to root_path}
  end
end

private
def review_params
  params.require(:review).permit(:review_name, :review_body, :image)
end

my review.js:

$(function() {
  var theHTML = [];
  theHTML.push('somewhat');
 $(".wrap-body").append(theHTML.join(''));
});

my index.html.erb

<%= form_tag home_review_path, :method => 'post', :multipart => true,  :remote => true do %>
  <%= text_area_tag  'review[review_body]'%>
  <%= text_field_tag 'review[review_name]'%>
  <%= file_field_tag 'review[image]' %>   <==== problem !!!
  <%= submit_tag 'Send'%>
<%end %>

Problem: if I add <% = file_field_tag 'review [image]'%> in a index.html.erb, rails throws an error:

ActionController :: UnknownFormat in HomeController#review

but if the field <% = file_field_tag 'review [image]'%> in a index.html.erb rails no returns review.js !!!

How to change the index.html.erb or action in home_controller.rb that always rails returns an review.js never reload the page?

Sorry for my English ((

Upvotes: 2

Views: 989

Answers (3)

deni5n
deni5n

Reputation: 101

I solved the problem as follows:

my home_conrtoller.rb:

def review
  @review = Review.new(review_params)
  if @review.save
    render json: {review_name: @review.review_name, review_body: @review.review_body, review_image: @review.image.url}
  else
    respond_to root_path
  end

private       
def review_params
    params.require(:review).permit(:review_name, :review_body, :image)
  end    
end

my review.js

$(function() {
  "use strict"; // Start of use strict
  $('#form_review').ajaxForm({
      success: SubmitSuccesful
  });
});

function SubmitSuccesful(responseText) {
  var theHTML = [];
  theHTML.push('<img src="'+ responseText.review_image+'">');
  $(".wrap-body").append(theHTML.join(''));
}

and my index.html.erb, delete ":remote => true"

<%= form_tag home_review_path, :method => 'post', :multipart => true do %>
  <%= text_area_tag  'review[review_body]'%>
  <%= text_field_tag 'review[review_name]'%>
  <%= file_field_tag 'review[image]' %> 
  <%= submit_tag 'Send'%>
<%end %>

result: form (file and text_fields) send ('post' method) to controller. Controller answer in format JSON. Callback function received event "success" and update html without reload page.

Upvotes: 0

Prakash Subramani
Prakash Subramani

Reputation: 716

You can't submit file with remote true option, you may try following

$("#send").click(function () {        
    var fd;
    fd = new FormData(document.getElementById("reviewForm"));
    $.ajax({
        url: "/home/review",
        type: "POST",
        data: fd,
        processData: false,  
        contentType: false 
    });
});

Upvotes: 0

Sajan
Sajan

Reputation: 1923

Maybe not a direct solution for you but I would suggest use Remotipart gem, It is only built for ajax file upload in rails using remote: true form since when using file upload in remote form, rails falls back to HTML submission. PS: I am not sure I got your problem fully, This might not be the solution for exact problem.

Upvotes: 1

Related Questions