Rendering json but encoded characters in rails 3.0.1 - ruby-on-rails-3

i'm using rails3.0.10
def districts
#names = Node.where("name like ?", "%#{params[:term]}%").limit(5).map(&:name)
respond_to do |format|
format.json {render :json => #names}
format.xml {render :xml => #names}
end
end
render xml get right names
but render json make the names is encoded like => ["name1", "name2", "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"]
i want to get right names in their right language
any help?
Thanks in advance

before sending the response you must force the strings to be UTF-8
add this line for the above code
#names.map{|name| name.force_encoding('UTF-8')}
this will loop the array of names and force them to be encoded from ASCII-8BIT to UTF-8
so my method will be
def districts
#names = Node.where("name like ?", "%#{params[:term]}%").limit(5).map(&:name)
#names.map{|name| name.force_encoding('UTF-8')}
respond_to do |format|
format.json {render :json => #names}
format.xml {render :xml => #names}
end
end
you can also modify tour model Node like
class Node < ActiveRecord::Base
def name
super().force_encoding('UTF-8')
end
end
i'm sure from this code , it works correctly
Thanks
M.SH

Related

Mapping REST response in Rails 5

I have the following #list received via MailChimp's API:
{"members"=>[{"email_address"=>"a#gmail.com"}, {"email_address"=>"b#gmail.com"}]}
And I'm trying to map it so that my #list is in the following format:
a#gmail.com, b#gmail.com, ...
However my attempts are unsuccessful with the following code in the controller:
respond_to do |format|
format.json {render json: #list{|email| {:email_address => email.email_address} }}
format.html
end
list = {"members"=>[{"email_address"=>"a#gmail.com"}, {"email_address"=>"b#gmail.com"}]}
list['members'].map{|m| m['email_address']}
# => ["a#gmail.com", "b#gmail.com"]
so render json: #list['members'].map{|m| m['email_address']} will get what you want

Rails: code in model or controller

What is the best approach here? I'm trying to clean up some code and I'm wondering if the controller is the best place for this variety of logic:
if user_signed_in?
if current_user.try(:admin?)
#docs = Doc.chronologic.page(params[:page]).per(5)
#orders = Order.chronologic.page(params[:page]).per(5)
else
#docs = Doc.chronologic.where(:user_id => current_user.ftp, :retired => "active").page(params[:page]).per(5)
#orders = Order.chronologic.where(:user => current_user.ftp).page(params[:page]).per(5)
end
respond_to do |format|
format.html
format.json { render json: #docs }
end
else
redirect_to new_user_session_path
end
If there's a better location for it, where would it be?
Thanks!
Edit: it's far worse for methods like pdf which has line after line of instructions for Prawn, but I can't seem to get send_data to work from the model.
This is basically what mu said, but here's my take.
In your app controller:
def require_logged_in
redirect_to new_user_session_path unless user_signed_in?
end
In your controller
before_filter :require_logged_in
def some_action
#docs = Doc.chronologic.for_user(current_user).page(params[:page]).per(5)
#orders = Order.chronologic.for_user(current_user).page(params[:page]).per(5)
respond_to do |format|
format.html
format.json { render json: #docs }
end
end
In your Doc model
scope :for_user, lambda do |user|
where(:user_id => user.ftp, :retired => "active") unless user.admin?
end
And something similar in your Order model.
Per your edit, definitely don't do send_data from your model.

Unexpected dot from helper method

I have the following routings
PosTracker::Application.routes.draw do
get "home/index"
resources :pos
resources :apis
match 'update_data' => 'home#update', :as => :update, :via => :get
root :to => "home#index"
end
Now, when using the link_to helper method:
link_to "text", pos_path(starbase)
I get the following route /pos.13 instead of /pos/13. Obviously, this won't produce valid output. How can I fix this?
Edit: Relevant controller:
class PosController < ApplicationController
# GET /pos
# GET /pos.xml
def index
#do stuff
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #pos }
end
end
# GET /pos/1
# GET /pos/1.xml
def show
#pos = Pos.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #pos }
end
end
end
It seems to me like Rails is recognizing pos_path as your #index action url helper. Generally it will take the symbol you pass to resources and singularize it for a #show action.
The url helper you want to use would be
link_to "text", po_path(starbase)
You can generally find the name of the helper methods by running
rake routes
Or to get the helper for a specific controller
rake routes CONTROLLER=pos

ROR + Refactor default line of scaffolding in Single Line of Code

In Rails Controller, Using Scaffolding we generate default methods. Here One of the method is present. I wanna customized the last 4 lines into a single line. In my last project, I have done that today I am not able to memorize that single line of code.
# Index
def index
#audits = Audit.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #audits }
end
end
Here I have to refactor the below code in single line.
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #audits }
end
Thanks
you can use respond_with
respond_with #audits
and at the top of your controller define the formats you want with
respond_to :html, :json, :xml
respond_to { |format| format.html; format.xml { render :xml => #audits }; }

Render YAML in Rails 3

Is there anyway to respond_to a .yml file extension?
I have tried, but can't get it to work.
respond_to do |format|
format.xml # index.xml.builder
format.yml {render :text => #labels.to_yaml, :content_type => 'text/yaml'}
end
The above code spits out the following error uninitialized constant Mime::YML
no need to add that stuff to environment.rb, just change format.yml to format.yaml and it will work.
Try to add this into your environment.rb file :
Mime::Type.register 'text/yaml', :yaml