respond_to do |format|
if #user.save
format.js { render :nothing => true, :status => :ok, :location => #user }
else
format.js { render :json => #user.errors, :status => :unprocessable_entity }
end
end
All options I've tried (like putting respond_to :js at the top of controller, etc) don't quite work the way as in this.
Rails 3 Format:
Use respond_to :json and respond_with(#user)
respond_to :json # You can also add , :html, :xml etc.
def create
#user= User.new(params[:user])
#---For html flash
#if #user.save
# flash[:notice] = "Successfully created user."
#end
respond_with(#user)
end
# Also, add :remote => :true, :format => :json to the form.
Try using format.json instead of format.js in your controller and :remote => :true, :format => :json in corresponding form.
Though, I'm not quite sure whether format.json or format.js should be used in that case. As default scaffolding from Rails 3 generates controllers with format.json and you're doing render :json in response I believe format.json is the right way to go. And format.js should be used when you return a piece of JS that should be executed.
The URL your are requesting should end with .json like this: /controller/action.json
If that is not possible:
You should set the 'accepts' parameter to 'application/json' while sending the ajax request.
Search for how to use 'accepts' here: http://api.jquery.com/jQuery.ajax/
And in the server side:
format.json { render :json => #user.errors, :status => :unprocessable_entity }
Related
I am running rails 3.2
I have created a nested form (requests > tags) with coffeescript handling the addition of new tags.
Everything works with the exception of the form posting a blank tag.name
I am trying to write a method to delete the blank field before the form posts. I realize this may be the wrong approach, but I am still a beginner:
requests_controller.rb
def create
#request = current_user.requests.build(params[:request])
#tag = Tag.new
if #tag.name.blank?
destroy_blank
end
respond_to do |format|
if #request.save
format.html { redirect_to(#request,
:notice => 'Request was successfully created.') }
format.json { render :json => #request,
:status => :created, :location => #request }
else
format.html { render :action => "new" }
format.json { render :json => #request.errors,
:status => :unprocessable_entity }
end
end
end
request.rb
def destroy_blank
blank = #tag.name
blank.delete
end
I hope that's clear. If not let me know and I will include more information.
If you can't stop blank tags from coming in, you can create a before_create filter in the model to skip saving blank tags. Leave the controller clean and simple.
Good luck!
Is there a difference between
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Signed up!"
else
render :new
end
end
and
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to(:users, :notice => 'Registration successfull. Check your email for activation instructions.') }
format.xml { render :xml => #user, :status => :created, :location => #user }
else
format.html { render :action => "new" }
format.xml { render :xml => #user.errors, :status => :unprocessable_entity }
end
end
end
Ignore the error and notice issues, my main question is the difference between using xml format and not using it, they seem to do the exact thing.
Using respond_to with different format than html give you the ability to have the response in the specified format (useful for web-service).
In that case (User creation) I don't think it is really useful, but it's all up to you!
Not using respond_to like your first exemple will simply render html.
More infos about respond_to here:
http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to
I am using Ruby on Rails 3 and I would like to know what the :location => ... and head :ok statements mean in following code, how they work and how I can\should use those.
respond_to do |format|
format.xml { render :xml => #user, :status => :created, :location => #user }
end
respond_to do |format|
format.xml { head :ok }
end
render ... :location => #user will set the HTTP location header to inform the client of the location of the newly created resource (that is, its URL)
head :ok sets render to return an empty response (so just the header, no body) with status 200. head :ok is shorthand for render nothing: true, status: :ok.
Here's a list of all the :status options you can use for setting the appropriate status code.
I am still a bit new to RoR, and am using scaffolding to generate CRUD interfaces for data.
I am using devise for user authentication, and want to allow a user that owns a specific entry to edit or delete, but protect that data from other users. However, I would like to allow a different user to revise or create new versions.
So if a user that attempts to edit should appear as if they are editing, but when they submit, the controller should actually generate a new entry (and potentially specify the parent_id of the entry it derived from).
Any help on implementation is greatly appreciated.
Also look into Ancestry, it's a really nice library to help with versioning.
Here is my solution that I came up with. I am a ruby newb, so I assume that I can just call the create function with the same params but wasn't sure how to do that, so I just duplicated code:
def update
#section = Section.find(params[:id])
if #section.owner == current_user.id
respond_to do |format|
if #section.update_attributes(params[:section])
format.html { redirect_to(#section, :notice => 'Section was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #section.errors, :status => :unprocessable_entity }
end
end
else
# REVISE
#childsection = Section.new(params[:section])
respond_to do |format|
if #childsection.save
format.html { redirect_to(#childsection, :notice => 'Section was successfully revised.') }
format.xml { render :xml => #childsection, :status => :created, :location => #childsection }
else
format.html { render :action => "new" }
format.xml { render :xml => #childsection.errors, :status => :unprocessable_entity }
end
end
end
end
the code
respond_to do |format|
format.html
format.json { render :json => #switches }
format.xml { render :xml => #switches.to_xml }
format.all { render :text => "only HTML, XML, and JSON format are supported at the moment." }
end
the above will work in Rails 2.2.2. But in Rails 3, getting controller/index.html or index on the browser will both fall into the last line: "only HTML and JSON format are supported at the moment."
The only Rails doc I can find on this is
http://api.rubyonrails.org/classes/ActionController/MimeResponds/ClassMethods.html#method-i-respond_to
which current only states:
respond_to :html, :xml, :json
but they need separate templates for json and xml, and can't handle the "only HTML and JSON format are supported at the moment" case.
In rails3 you would write:
respond_with(#switches) do |format|
format.html
format.json { render :json => #switches }
format.xml { render :xml => #switches }
format.all { render :text => "only HTML, XML, and JSON format are supported at the moment." }
end
But this only works in correspondence with a respond_to block at the top of the file, detailing the expected formats. E.g.
respond_to :xml, :json, :html
Even in that case, if anybody for instance asks the js format, the any block is triggered.
You could also still use the respond_to alone, as follows:
#switches = ...
respond_to do |format|
format.html {render :text => 'This is html'}
format.xml {render :xml => #switches}
format.json {render :json => #switches}
format.all {render :text => "Only HTML, JSON and XML are currently supported"}
end
Hope this helps.
You may find it useful to watch this episode of railscasts, which illustrates the changes to controllers in Rails 3 and in particular the changes to the responder class (putting respond_to in the controller class itself and only using respond_with #object in the action):
http://railscasts.com/episodes/224-controllers-in-rails-3
The following works for me. I believe you have to specify the "render" part for html explicitly or it will use the format.any.
respond_to do |format|
format.html { render :html => #switches }
format.json { render :json => #switches }
format.xml { render :xml => #switches }
format.all { render :text => "we only have html, json, and xml" }
end