我一直在尝试将自定义属性添加到jbuilder,就像我在显示页面中一样添加到我的索引页面,用于分页将paginate并且它不显示自定义属性.
例如,我在控制器操作中拥有的是
def index
#respond_with
@publishers = Publisher.paginate(:page => params[:page], :per_page => 30)
respond_to do |format|
format.json
end
end
Run Code Online (Sandbox Code Playgroud)
我的index.json.jbuilder是
json.array!(@publishers) do |publisher|
json.extract! publisher, :id, :name, :url
json.categories do
publisher.categories.each do |category|
json.name category.name
json.id category.id
json.url url_for(category)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我想拥有的是什么
json.current_page @publishers.current_page
json.total_pages @publishers.totla_entries
json.array!(@publishers) do |publisher|
json.extract! publisher, :id, :name, :url
json.categories do
publisher.categories.each do |category|
json.name category.name
json.id category.id
json.url url_for(category)
end
end
end
Run Code Online (Sandbox Code Playgroud)
这样我就可以在索引页面的json输出中显示current_page和总页数.
目前我所拥有的是
[{"id":1,"name":"facebook","url":"http://www.facebook.com","categories":{"name":"Art and Crafts","id":1,"url":"/categories/1-art-and-crafts"}}]
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点.我也在使用willpaginate
当我在开发模式下运行我的应用程序时,我收到此警告消息
在2012-01-07 03:30:53 -0500开始获取127.0.0.1的GET"/assets/playerbg.png"
服务资产/playerbg.png - 304未修改(0ms)
[2012-01-07 03:30:53]警告无法确定响应正文的内容长度.设置响应的内容长度或设置Response#chunked = true
请问我该如何处理此消息.
我已经能够使用nginx,unicorn和capistrano将我的rials应用程序部署到vps系统中,没有任何错误.现在,我想在同一个vps服务器内使用相同的nginx配置(下面的两个脚本)部署另一个rails应用程序,并在运行cap deploy之后:setup和cap deploy:cold它设置正确并且rails应用程序被发送到服务器.我得到的问题是这个.当我输入service nginx restart我得到以下错误
nginx: [emerg] duplicate upstream "unicorn" in /etc/nginx/sites-enabled/cf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
Run Code Online (Sandbox Code Playgroud)
我正在运行的第一个应用程序的nginx脚本是
upstream unicorn {
server unix:/tmp/unicorn.cf.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name cfmagazineonline.com;
root /home/deployer/apps/cf/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Run Code Online (Sandbox Code Playgroud)
我的nginx配置为第二个rails应用程序无法运行,但改为第一个rails应用程序的错误并使其崩溃是 …
capistrano ruby-on-rails nginx ruby-on-rails-3 ruby-on-rails-3.2
如何定义一个全局变量,以便我的current_user方法可以工作,我想要它,我只需要检查是否有当前用户,我的示例代码如下
if (isset($_SESSION['company_id'])) {
$current_user = Companies::find($_SESSION['company_id']);
}
else
{
$current_company = null;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使用当前的用户方法,无需将其传递给我的app->render()方法,就像在我的header.html中一样
{% if current_user %}
hi {{current_user.name}}
{% endif %}
Run Code Online (Sandbox Code Playgroud) 我开始使用设计,我希望能够将用户属性更新为在线或离线.我有这个
class ApplicationController < ActionController::Base
private
def after_sign_in_path_for(resource_or_scope)
#update user status to online
root_path
end
def after_sign_out_path_for(resource_or_scope)
#update user status to offline
root_path
end
end
Run Code Online (Sandbox Code Playgroud)
但我不知道如何编写更新方法
我正在使用rails 3应用程序,它在我的locales文件夹中有不同的语言.这些文件是en.yml,pu.yml,sp.yml.所有语言都必须转换为各种格式,我需要帮助,让用户选择任何语言,如链接
<%= link_to "English language", ...%> <%= link_to "spanish", ...%>
Run Code Online (Sandbox Code Playgroud)
当用户选择语言时,该语言被设置为用户的首选语言,以便用户在每次登录后不必继续选择语言.
ruby-on-rails internationalization ruby-on-rails-3 ruby-on-rails-3.1
我正在尝试使用kaminari和jquery创建一个不错的无尽分页,我已经看过关于这种主题的教程,但它是用will_paginate和jquery完成的.这次我希望能够用kaminari宝石做到这一点.这里有任何帮助.谢谢
我有一个简单的rails应用程序,就像一个具有start_at字段和end_at字段的事件系统.我希望能够显示start_at当月的月份事件.我为此定义了一个范围,如下所示:
scope :this_months_event, lamda{where("start_at = ?" Time.zone.now.month)}
Run Code Online (Sandbox Code Playgroud)
每当我在控制台窗口中尝试这个时,我得到一个空数组.我观察到该start_at字段的月份未被提取,因此无法找到start_at月份,因此它返回空.
有没有人知道一个很好的方法来做这个或如何通过这个start_at月,以便当我打电话时Event.this_months_event,我得到本月的所有事件,如果可能的话,范围来获取过去的事件和另一个来获取当前事件.
我有一个简单的Rails应用程序向外部网站发出请求并返回一个带有回调的json url响应到我的站点,以通知我的Rails应用程序有关响应.json url示例是https://voguepay.com/?v_transaction_id=demo-1345109950&type=json,响应正文如下:
{"merchant_id":"demo","transaction_id":"demo-1345109950","email":"testuser@buyerdomain.com","total":"10","total_paid_by_buyer":"10.00","total_credited_to_merchant":"9.90","extra_charges_by_merchant":"0.00","merchant_ref":"","memo":"Donation of N10 to Test User","status":"Approved","date":"2012-01-01 11:39:11","referrer":"http:\/\/sellerdomain.com\/buy_now.html","method":"VoguePay","fund_maturity":"2012-01-03"}
Run Code Online (Sandbox Code Playgroud)
我想将此响应转换为Rails方法,而不是简单地给出我需要进行查询的属性.例如,从响应主体我需要做出如下操作:
def notify
response = Json.parse('https://voguepay.com/?v_transaction_id=demo-1345109950&type=json').body
response.status
response.date
response.merchant_id
response.total
end
Run Code Online (Sandbox Code Playgroud)
上面的代码只是一个解释我想要实现的内容的示例.任何帮助都会很棒.
我已经尝试了typhoeus和yajl-ruby gems但是当请求进来时,我的所有身份验证方法都被删除了,我不断收到错误消息,无法验证csrf元令牌.即使我跳过它,当前用户也会自动注销(使用设计认证).使用的代码示例如下:
class NotificationsController < ApplicationController
skip_before_filter :verify_authenticity_token
def notify
@transaction_id = params[:transaction_id]
do_notify
end
private
def do_notify
hydra = Typhoeus::Hydra.new
request = Typhoeus::Request.new("https://voguepay.com/?v_transaction_id=#{@transaction_id}&type=json")
request.on_complete do |response|
logger.info("#{response.request.url} in #{response.time} seconds") #remove in production to avoid huge logs
transaction = Yajl::Parser.parse(response.body) #or Yajl::Parser.parse(response.body)
#Now we have the following keys in our transaction hash …Run Code Online (Sandbox Code Playgroud) devise ×2
json ×2
activerecord ×1
capistrano ×1
jbuilder ×1
jquery ×1
kaminari ×1
nginx ×1
pagination ×1
php ×1
slim ×1