简体   繁体   中英

ActionController::UnknownFormat (Ruby on Rails)

I am creating a Ruby on Rails application where I render my 'new.html.erb' (in my klass controller) and on top of it render my partial '_select_previous.html.erb,' also, I have a 'link_to' to switch back and forth from one to the other. However, when I try to start my project at the 'index.html.erb' and then use a 'link_to' to go to the new file, it shows me that error above.

I am using jQuery to do the switching from one view to the other.

Here is my new.html.erb:

<p> Or select from the previous created classes <%= link_to "here", 
new_klass_path, id: "link_to", :remote => true %> </p>

<div id="previous">
   <%= render :partial => 'select_previous' %>
</div>

<div id="content">
 <!-- Ruby form -->
</div>

This is my klasses controller:

def index
    @klass = Klass.all.paginate(:page => params[:page])
    #import_klasses(@klass)
end

def new
    @klass = Klass.new
    #saved = @klass.save
    #@klass.save!
    #if saved
       #s1 = Student.last.id
       #k1 = Klass.last.id
       #@enrollments = Enrollment.new(student_id: s1, klass_id: k1)
       #binding.pry
       #@enrollments.save!
     #redirect_to :controller => 'enrollments', :action => 'index'
     #flash[:alert] = "Class Created"
  #end
#end

  respond_to do |format|
     format.html {}
     format.js
  end
end

And this is my show.js:

$(document).ready(function() {
$("#days").hide();
$("#grade").hide();
$("#class").hide();
$("#all").hide();
$("#previous").hide();

$("#link_to").click(function () {
    var count = $(this).data("count") || 0;
    if(count == 1)
    {
        $("#content").hide();
        $("#previous").show();
    }
    else if(count == 2)
    {
        $("#content").show();
        $("#previous").hide();
        count = 0;
    }

    $(this).data("count", ++count);
});
});

Thank in advance for any help you may provide me.

My last attempt, as I am out of my depth it seems. Use this along with your original code, files unrenamed. Leave your js file where it is.

respond_to do |format|
    format.html {}
    format.js do
        redirect('/assets/show.js')
    end
end

Though I really appreciate all the effort that has been put towards solving this question, I must advice that I found the solution to the problem.

The error was on my index.html.erb, which I did not put because I thought it had nothing to do with the problem. My index looked like this:

<div>
    <%= link_to "Create new class", new_klass_path(@klass) %>
</div>

The unknown format error came because it did not know what "@klass" was; or so I believe. As soon as I took it away, the transition from one page to the other went smoothly. Moreover, it looks like this know:

<div>
    <%= link_to "Create new class", new_klass_path %>
</div>

If this pisses anyone off, I deeply apologize. I really appreciate everyone's help. I am going to try to level up quickly, to give you guys checks for useful answers.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM