Tasos Anesiadis
Tasos Anesiadis

Reputation: 1150

Rails, trying to run a method from one controller to another gives Unknown Format

Ok,so basically I upload an image, and I just want to reload my partial when an image is uploaded.

Every Language has many images. (Weird structure of the app, yeah.)

I want to redirect from a method from the images_controller to a method of the languages_controller.

On my images_controller:

def create
    @image = Image.new(image_params)

    if !params[:image][:image].nil?
        if @image.save(image_params)
            @language = Language.find(params[:image][:language_id])
            @language.increment(:last_position)
            @language.save

            redirect_to load_language_partial_path(@language),:remote => true

        end
    else
        respond_to do |format|
            format.js {render :action => 'required_fields'}
        end
    end         
end

And the method on my languages_controller:

def load_partial
    @language = Language.find(params[:id])
    @image = Image.new
    @images = @language.images.order(priority: :asc)

    respond_to do |format|
        format.js
    end
end

The thing is when I run the load partial from a link in my view, it runs perfectly, but with this it gives me

ActionController::UnknownFormat (ActionController::UnknownFormat): app/controllers/languages_controller.rb:10:in `load_partial'

I tried removing :remote = > true, but the same thing.

Is what I am trying to do not a good practice? Should I try some other way?

Thanks for reading.

EDIT In my language partial I got a form in which I call the create function for @image

<%=form_for @image , url: images_create_path, remote: true, multipart: true, authenticity_token: true, html: {class: "form-horizontal", :style=> "" } do |f|%>
    <h3 class="" style="float:left;margin-top:5px;"> Upload Image: </h3> 
    <div style="">
    <span>
    <%= f.file_field :image, :class => 'btn btn-default', 'data-show-upload' => false, :accept => 'image/*', :style=> 'margin-left:10px; float:left' %>

    <%= f.number_field :language_id, :value=> @language.id, :style=> 'display:none' %>
    <%= f.number_field :priority, :value=> @language.last_position, :style=> 'display:none' %>
    </span>
    <span>
    <%= submit_tag "Upload",:class => 'btn btn-primary', :style=> "margin-left:10px;height:44px" , data: { disable_with: "Uploading..." } %>
    </span>
    </div>
<% end %>

And my load_partial.js.erb

$('.page-content').html('<%= j(render partial: "/languages/languagePartial") %>')

Upvotes: 0

Views: 59

Answers (1)

Tushar H
Tushar H

Reputation: 805

Instead of

redirect_to load_language_partial_path(@language),:remote => true

Try :

protocol = 'https://' or 'http://'
redirect_to protocol: protocol, controller: "languages", action: "load_partial", format: "js", languages: @language

You can skip protocol part if you want. I just gave it as I used it for my purpose. So you can use only:

redirect_to controller: "languages", action: "load_partial", format: "js", languages: @language

languages at the end is the parameter you want to pass, you can pass multiple parameters also separated by comma(,).

Upvotes: 1

Related Questions