Questions tagged [respond-to]
112 questions
251
votes
12 answers
Given a class, see if instance has method (Ruby)
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…

user94154
- 16,176
- 20
- 77
- 116
56
votes
7 answers
ActionController::UnknownFormat
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):
…

tvieira
- 1,865
- 3
- 28
- 45
55
votes
3 answers
Rails: respond_to JSON and HTML
I have a controller "UserController" that should respond to normal and ajax requests to http://localhost:3000/user/3.
When it is a normal request, I want to render my view. When it is an AJAX request, I want to return JSON.
The correct approach…

Don P
- 60,113
- 114
- 300
- 432
55
votes
2 answers
How to tell which format a controller has resolved to render
In a rails controller action with the following code:
respond_to do |format|
format.json{ render :json=> {:status => 200, :response=>@some_resource} }
format.html { redirect_to(some_resource_path)}
end
How can I log the format the controller…

Undistraction
- 42,754
- 56
- 195
- 331
45
votes
1 answer
What does `:location => ...` and `head :ok` mean in the 'respond_to' format statement?
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 =>…

user502052
- 14,803
- 30
- 109
- 188
30
votes
2 answers
how can I generate json from respond_to method in rails?
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,…

Oberon Dude
- 455
- 2
- 5
- 6
20
votes
3 answers
Rails respond_with -- why does POST return a URL instead of the data?
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…

Tom Harrison
- 13,533
- 3
- 49
- 77
9
votes
3 answers
How does respond_to and respond_with work in rails?
When there is
def some_action
respond_to do |format|
format.html {}
format.js {}
format.json { respond_with @objects}
end
end
It seems like html line and the js line automatically serve up/call the file matching the action's name.…

ahnbizcad
- 10,491
- 9
- 59
- 85
5
votes
4 answers
Rails : how to render an other action in the controller, in js not in html?
I have two actions in my controller :
def find
@item = Item.find(params[:id])
render 'result', :id => @item.id
end
def result
@item = Item.find(params[:id])
respond_to do |format|
format.js
end
end
The issue in that I call first the…

Maxxx
- 1,179
- 3
- 16
- 21
5
votes
1 answer
Rails: Respond with js to Ajax request
I encountered some weird behaviour when using an ajax request to get a response from a rails controller action.
$.ajax({
type: 'GET',
url: '/notifications',
success: function(result) {
eval(result);
}
});
def index
…

heroxav
- 1,387
- 1
- 22
- 65
5
votes
1 answer
where do I put the respond_to if there is an if-statement in the controller in rails?
I have a controller that has if-condition:
def update
@contact_email = ContactEmail.find(params[:id])
if @contact_email.update_attributes(params[:contact_email])
flash[:notice] = "Successfully updated contact email."
…

Satchel
- 16,414
- 23
- 106
- 192
5
votes
1 answer
Override the respond_to format with form button in rails 3
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…

Chris
- 171
- 10
5
votes
1 answer
What does respond_to do when called without a block?
I understand how respond_to works when it's called with something like this:
def index
@users = User.all
respond_to do |format|
format.html
format.json { render json: @users }
end
end
But I've seen some apps which pass respond_to a…

GMA
- 5,816
- 6
- 51
- 80
5
votes
2 answers
Test contents of file in rspec from respond_to format.csv
I have the following code in my controller that exports a csv file
...
def export
@filename = 'users.csv'
@output_encoding = 'UTF-8'
@users = User.active_users #not the actual scope but this only returns active
respond_to do…

kwbock
- 647
- 5
- 13
5
votes
2 answers
How to set custom flash with respond_to in Rails
In respond_to you can set flash[:notice] like this
respond_to do |format|
format.html { redirect_to photo_path(photo), :notice => 'The photos was saved') }
format.xml { render :xml => photo, :status => :created}
end
I am trying to set…

Nick Ginanto
- 31,090
- 47
- 134
- 244