respond-to

How to make a respond_to by AJAX in Rails 3?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 23:08:56
I have a form, that I send with using AJAX. The form is in the FirstsController . I need the form send to SecondsController . The form is sent and data saved. Then I wanna change the form by a text, I try to do by this way: def create ...saving... respond_to do |format| format.js { render :update do |page|; page << "$('#id_element').html('hellllloooooo');" end } format.html {} end end And I ma getting this error: ActionView::MissingTemplate (Missing template seconds/update, application/update with {:handlers=>[:erb, :builder, :coffee], :formats=>[:js, :html], :locale=>[:en, :en]}.): How should

How to convert this respond_to options to use Rails 3 version?

老子叫甜甜 提交于 2019-12-04 20:56:33
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,

Override the respond_to format with form button in rails 3

a 夏天 提交于 2019-12-04 03:38:57
问题 I have a set of reports that are displayed in various formats using the Rails call "respond_to", such that if the URL ends in CSV or JSON, the report is generated in that format. I had a request to make a download button to make grabbing reports easier, but since the reports have customizations like date ranges, I need to be able to submit one form and specify a response format in the form. Is this possible? How can it be done? Form Code: <%= form_tag('', method: 'get') do %> <%= hidden_field

Rails respond_with — why does POST return a URL instead of the data?

a 夏天 提交于 2019-12-03 10:07:55
This is a question "why does it work this way", not "how do I make this work". My app is calling a third party REST API that returns JSON, and returning the result as part of my own JSON API. I was using the Rails 3 respond_to and respond_with methods; in the case of GET requests, this works as I expect, just passing through the JSON. In the case of POST , it does more, including making a URL from the object returned to pass in a :location option. But since my object is just JSON (not ActiveRecord), I get an error. For example... # POST /api/products.json with params id=:id def create query

how can I generate json from respond_to method in rails?

我的梦境 提交于 2019-11-30 12:29:31
问题 If I have a block of code like this: def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @post } end end How do I add something like format.json Any tips, pointers, ideas gladly welcomed... 回答1: It's just like the other formats except that you use render :json instead. respond_to do |format| format.html # show.html.erb format.xml { render :xml => @post } format.json { render :json => @post } end 回答2: or you can handle it as

What does `:location => …` and `head :ok` mean in the 'respond_to' format statement?

家住魔仙堡 提交于 2019-11-30 11:10:07
问题 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 回答1: 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

how can I generate json from respond_to method in rails?

拥有回忆 提交于 2019-11-30 02:48:47
If I have a block of code like this: def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @post } end end How do I add something like format.json Any tips, pointers, ideas gladly welcomed... It's just like the other formats except that you use render :json instead. respond_to do |format| format.html # show.html.erb format.xml { render :xml => @post } format.json { render :json => @post } end VP. or you can handle it as javascript respond_to do |format| format.js { render :json { :only => :name }.to_json } end then you just

Given a class, see if instance has method (Ruby)

前提是你 提交于 2019-11-29 18:55:25
I know in Ruby that I can use respond_to? to check if an object has a certain method. But, given the class, how can I check if the instance has a certain method? i.e, something like Foo.new.respond_to?(:bar) But I feel like there's gotta be a better way than instantiating a new instance. horseyguy I don't know why everyone is suggesting you should be using instance_methods and include? when method_defined? does the job. class Test def hello; end end Test.method_defined? :hello #=> true NOTE In case you are coming to Ruby from another OO language OR you think that method_defined means ONLY

Given a class, see if instance has method (Ruby)

本小妞迷上赌 提交于 2019-11-28 14:11:28
问题 I know in Ruby that I can use respond_to? to check if an object has a certain method. But, given the class, how can I check if the instance has a certain method? i.e, something like Foo.new.respond_to?(:bar) But I feel like there's gotta be a better way than instantiating a new instance. 回答1: I don't know why everyone is suggesting you should be using instance_methods and include? when method_defined? does the job. class Test def hello; end end Test.method_defined? :hello #=> true NOTE In

ActionController::UnknownFormat

a 夏天 提交于 2019-11-27 00:58:55
In my rails app I have a ajax request to the server, to store some data. This used to work without any problem, but now I get an error: ActionController::UnknownFormat (ActionController::UnknownFormat): app/controllers/reservations_controller.rb:45:in `create' As following is the controller and my javascript file where I declare the datatype do be JSON class ReservationController < ApplicationController respond_to :html, :json def create ... respond_to do |format| if @reservation.save format.html do redirect_to '/' end format.json { render json: @reservation.to_json } else render 'new' end end