RAILS: Internet Explorer 8 not rendering actions, download popup instead - ruby-on-rails

I have a controller and all of the actions are not render by IE8, in Firefox and Chromer works ok. The problem is only with this controller, i have another that work ok.
The code is:
class CustomersController < ApplicationController
auto_complete_for :customers, :name
def search
unless params[:q].nil?
#customers = Customer.find(:all, :conditions => ['name LIKE ?',
"%#{params[:q]}%"])
end
respond_to do |format|
format.xml { render :xml => #customers}
format.json { render :json => #customers}
end
end
def index
#customers = Customer.find_index
end
def show
#customer = Customer.find(params[:id])
end
For example the index action is not getting render and instead IE8 display a download dialog, but if i edit the index action like this:
def index
#customers = Customer.find_index // a find all but ordered.
respond_to do |format|
format.html
end
end
Any ideas what is happening?
UPDATE:
For all of you interested, the problem was the templates files names.
All the files with a name like "invoice.erb" was sent back to the browser as content "text/erb" while the files like "customers.html.erb" was sent back as "text/html".
Once i renamed all the file to "html.erb" everything works ok

ie8 doesn't know what to do with xml or json but does know what to do with html. If you really want the browser to display the xml you could add :content_type => 'text/plain' to the block so it knows how to respond to it.

Related

Rabl/JSON view is not being rendered with ActionController::Live

Simply when I include ActionController::live in my Appointments Controller my apptbook view which i request as appointments/apptbook.json is loaded (i can see the database loads on the terminal attached to my server) but nothing is returned to the browser, the browser hangs waiting for a response. Can't seem to work this out none of my usual html views in this same controller stall out like this, however i just tried my index json which I can confirm stalls out, so when i request /appointments I get a normal html response, however when i request /appointments.json or /appointments/apptbook.json I never see anything returned to the browser.
Is there an incompatibility with ActionController::Live??
I have tried only including it in my events method but it seems to need to be loaded at the top.
Is there a way of unloading it for a few of my methods??
Controller:
class AppointmentsController < ApplicationController
include ActionController::Live
def index
#appointments = Appointment.all
ActiveRecord::Base.include_root_in_json = false
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #appointments.to_json(:only => [:id, :start, :end], :methods => :title) }
end
end
def apptbook
#appointments = Appointment.last(2)
end
end
View:
collection #appointments
attributes :id
node(:start) { |appointment| appointment.start.iso8601}
node(:end) { |appointment| appointment.finish.iso8601}
node(:title) { |appointment| '<a class="patient-select-link" data-id="'+appointment.patient.id.to_s+'">'+appointment.patient.full_name+'</a>' }

Rails respond_to in the controller

This is the code from internet and I am having trouble understanding what does respond_to, format.html and format.js do in the controller.
def create
#review = Review.create!(params[:review])
flash[:notice] = "Thank you for reviewing this product"
respond_to do |format|
format.html { redirect_to #review.product }
format.js
end
end
Thank you for your time.
Ramya
respond_to(*types, &block) public
Without web-service support, an action which collects the data for displaying a list of people might look something like this:
def index
#people = Person.find(:all)
end
Here’s the same action, with web-service support baked in:
def index
#people = Person.find(:all)
respond_to do |format|
format.html
format.xml { render :xml => #people.to_xml }
end
end
What that says is, "if the client wants HTML in response to this action, just respond as we would have before, but if the client wants XML, return them the list of people in XML format." (Rails determines the desired response format from the HTTP Accept header submitted by the client.)
Supposing you have an action that adds a new person, optionally creating their company (by name) if it does not already exist, without web-services, it might look like this:
def create
#company = Company.find_or_create_by_name(params[:company][:name])
#person = #company.people.create(params[:person])
redirect_to(person_list_url)
end
Here’s the same action, with web-service support baked in:
def create
company = params[:person].delete(:company)
#company = Company.find_or_create_by_name(company[:name])
#person = #company.people.create(params[:person])
respond_to do |format|
format.html { redirect_to(person_list_url) }
format.js
format.xml { render :xml => #person.to_xml(:include => #company) }
end
end
If the client wants HTML, we just redirect them back to the person list. If they want Javascript (format.js), then it is an RJS request and we render the RJS template associated with this action. Lastly, if the client wants XML, we render the created person as XML, but with a twist: we also include the person’s company in the rendered XML, so you get something like this:
http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to

How to get my rails pages to return as html?

I've written a rails site in the latest version of rails, based on knowledge of rails from a couple of years ago, and I've hit a horrible snag.
I foolishly decided to ignore the new RESTful routing system and hope for the best.
So all of my views are plain .erb, NOT html.erb
my routes file looks like this
map.connect '/crm/:action/:id', :controller => "contacts", :format => 'html'
here is an example of a method:
def update_emails
Com.update_emails
respond_to do |format|
format.html {redirect_to(:action => 'list')}
end
end
when it redirects to the 'list' action, I get a plain text file that my browser tries to download, instead of the html version of the page that I want.
Is there a simple way for me to tell rails to only send html format files?
Thank you!
EDIT:
list action
def list
if params[:search]
#contacts = Contact.search(params)
else
#contacts = Contact.find(:all, :order => "updated_at desc")
end
end
and the view is a plain .erb file (problem is the same when I make it a .html.erb file)
Also, the same thing happens when I redirect_to other actions
You should use respond_to.
def update_emails
Com.update_emails
redirect_to(:action => 'list')
end
and then in the 'list' action
def list
#some code here
respond_to |format| do
format.html {render :list}
end
end

How can I explicitly declare a view from a Rails controller?

I want to explicitly call a view from my controller.
Right now I have:
def some_action
.. do something ...
respond_to do |format|
format.xml
end
end
... then it calls my some_action.xml.builder view. How can I call some other view? Is there a parameter in respond_to I'm missing?
Thanks,
JP
You could do something like the following using render:
respond_to do |format|
format.html { render :template => "weblog/show" }
end
See the Rendering section of the ActionController::Base documentation for the different ways you can control what to render.
You can tell Rails to render a specific view (template) like this:
# Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)
render :template => "weblog/show"
# Renders the template with a local variable
render :template => "weblog/show", :locals => {:customer => Customer.new}
Or even simpler since Rails > 3.0:
render "edit"
You can also pass :action, or :controller if that's more convenient.
respond_to do |format|
format.html { render :action => 'show' }
end
You can modify the internal lookup_context of the controller by doing this in your controller
before_filter do
lookup_context.prefixes << 'view_prefix'
end
and the controller will try to load view/view_prefix/show.html when responding to an show request after looking for all the other view prefixes in the list. The default list is typically application and the name of the current controller.
class MagicController
before_filter do
lookup_context.prefixes << 'secondary'
end
def show
# ...
end
end
app.get '/magic/1`
This GET request will look for a view in the following order:
view/application/show.erb
view/magic/show.erb
view/secondary/show.erb
and use the first found view.
Use render
http://api.rubyonrails.com/classes/ActionController/Base.html#M000474

Excluding some ActiveRecord properties from xml rendering in rails

I have an ActiveRecord model that I would like to convert to xml, but I do not want all the properties rendered in xml. Is there a parameter I can pass into the render method to keep a property from being rendered in xml?
Below is an example of what I am talking about.
def show
#person = Person.find(params[:id])
respond_to do |format|
format.xml { render :xml => #person }
end
end
produces the following xml
<person>
<name>Paul</name>
<age>25</age>
<phone>555.555.5555</phone>
</person>
However, I do not want the phone property to be shown. Is there some parameter in the render method that excludes properties from being rendered in xml? Kind of like the following example
def show
#person = Person.find(params[:id])
respond_to do |format|
format.xml { render :xml => #person, :exclude_attribute => :phone }
end
end
which would render the following xml
<person>
<name>Paul</name>
<age>25</age>
</person>
You can pass an array of model attribute names to the :only and :except options, so for your example it would be:
def show
#person = Person.find(params[:id])
respond_to do |format|
format.xml { render :text => #person.to_xml, :except => [:phone] }
end
end
to_xml documentation
I just was wondering this same thing, I made the change at the model level so I wouldn't have to do it in the controller, just another option if you are interested.
model
class Person < ActiveRecord::Base
def to_xml
super(:except => [:phone])
end
def to_json
super(:except => [:phone])
end
end
controller
class PeopleController < ApplicationController
# GET /people
# GET /people.xml
def index
#people = Person.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #people }
format.json { render :json => #people }
end
end
end
I set one of them up for json and xml on every object, kinda convenient when I want to filter things out of every alternative formatted response. The cool thing about this method is that even when you get a collection back, it will call this method and return the filtered results.
The "render :xml" did not work, but the to_xml did work. Below is an example
def show
#person = Person.find(params[:id])
respond_to do |format|
format.xml { render :text => #person.to_xml(:except => [:phone]) }
end
end
The except is good, but you have to remember to put it everywhere. If you're putting this in a controller, every method needs to have an except clause. I overwrite the serializable_hash method in my models to exclude what I don't want to show up. This has the benefits of not having t put it every place you're going to return as well as also applying to JSON responses.

Resources