Topic: ajax,Completed 406 Not Acceptable

if i reassign the arribute of variable in the Create action,

how deal the belowing problem:

def create
    @entry = Entry.create(params[:entry])
    @entry.url = "www.railsforum.com"

    respond_to do |format|
      if @entry.save
        format.js
      else
        format.js { render action: "new" }
      end
    end
  end

the reassign llwi be a UPDATE in database,

it will be “Processing by EntriesController#create as HTML”

and error:"Completed 406 Not Acceptable in 117ms (ActiveRecord: 104.8ms)"

how to modify the code to “Processing by EntriesController#create as JS

thanks

i am a rails rookie

Re: ajax,Completed 406 Not Acceptable

i delete the reassgin code "@entry.url = "www.railsforum.com""

the error still exists!

in my _form.html.erb contain the file_field_tag.

which arribute should be set ?

i am a rails rookie

Re: ajax,Completed 406 Not Acceptable

The code might be okay, the call might be wrong. You are calling as HTML but rendering as JS. So you need to change the call or the code.

So respond_to must be format.html or the call must be :format => 'js'

Re: ajax,Completed 406 Not Acceptable

ostraaten wrote:

The code might be okay, the call might be wrong. You are calling as HTML but rendering as JS. So you need to change the call or the code.

So respond_to must be format.html or the call must be :format => 'js'

that's my respond_to

    respond_to do |format|
      if @entry.save
        format.js
      else
        format.js { render action: "new" }
      end
    end

how to modify the code?!

thanks

i am a rails rookie

Re: ajax,Completed 406 Not Acceptable

ostraaten wrote:

The code might be okay, the call might be wrong. You are calling as HTML but rendering as JS. So you need to change the call or the code.So respond_to must be format.html or the call must be :format => 'js'

So respond_to must be format.html or the call must be :format => 'js'

i know the respond_to,and mine is format.js. what does the "call" must be :format = > 'js' mean?

even i don't deal with the params of file_field_tag in my Create action, it's also processed as HTML, how to chang the call to be JS?

thanks

Last edited by jay_li (2012-06-21 20:31:16)

i am a rails rookie

Re: ajax,Completed 406 Not Acceptable

Your question doesn't make sense
The create action will respond to whatever mime types you have set up in your routes for the controller and whatever mime type responses you respond to in the  controller.

If using RESTful routes (resources :some_controller_name) then you will get xml, html and json responses straight out of the box. Just remove the respond_to block or make sure you respond to json, xml and html

  # POST /entries
  # POST /entries.xml
  # POST /entries.json
  def create
    @entry = Entry.new(params[:entry])
    @entry.url = "www.railsforum.com"

    respond_to do |format|
      if @entry.save
        format.html { redirect_to(@entry, :notice => 'Entry successfully created.') }
        format.xml  { render xml: @entry, :status => :created, :location => @entry }
        format.json { render json: @entry, status: :created, location: @entry }
      else
        format.html { render :action => "new" }
        format.xml  { render xml: @entry.errors, :status => :unprocessable_entity }
        render json: nil, status: :unprocessable_entity
      end
    end
  end

Now your create action will respond to json requests as well as html and xml requests

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: ajax,Completed 406 Not Acceptable

Once you have that set up your action will respond to whatever format your users choose to use
you might find this useful
http://railscasts.com/episodes/350-rest-api-versioning

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: ajax,Completed 406 Not Acceptable

jamesw wrote:

Your question doesn't make sense
The create action will respond to whatever mime types you have set up in your routes for the controller and whatever mime type responses you respond to in the  controller.

If using RESTful routes (resources :some_controller_name) then you will get xml, html and json responses straight out of the box. Just remove the respond_to block or make sure you respond to json, xml and html

Now your create action will respond to json requests as well as html and xml requests

i mistake the cause of the error, it's not for the reassigning the variable!

in my app, i use Ajax and the CRUD all in index.html.erb.
the code of index

<% @entries.each do |entry| %>
    <%= render entry %>
<% end %>
</table>

<h2>Entry form</h2>
<div id="form"><%= render :partial => "form" %></div>

part codes of _form.html.erb

<%= form_for(@entry, :remote => true) do |f| %>
    <div class="field">
    <%= f.label :name%><br />
    <%= f.text_field :name%>
  </div>
  <div class="field">
    <%= file_field_tag :file%>
  </div>
   <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

the code of new.js.erb

$("#form > form").replaceWith("<%= j render 'form' %>")

the code of create.js.erb

$("<%= j render @entry %>").appendTo("#entries").hide().fadeIn();
$("#new_entry")[0].reset();
the code of update.js.erb
$("#<%= dom_id @entry %>").replaceWith("<%= j render @entry %>");
<% @entry = Entry.new  %>
$("#form > form").replaceWith("<%= j render "form" %>");
$("#new_entry")[0].reset();

there aren't any new.html.erb or show.html.erb,the result of all actions will be show in the index.html.erb

if the file_field_tag doesn't load file,the respond_to will works well and the input information just now will be show in the top of index.html.erb
(just i mentioned above the create action "Processing by EntriesController#create as JS")

but if load file, it will jump to the page (the show.html.erb that don't exists)as without ajax(create action "Processing by EntriesController#create as HTML")

about this case, how to deal with it?

Really bother you, forgive my ignorance question.

thank you

i am a rails rookie

Re: ajax,Completed 406 Not Acceptable

it's ok with the remotipart gem

https://github.com/JangoSteve/remotipart

thank ostraaten, thank all hotters

i am a rails rookie