如何围绕视图代码包装链接?我无法弄清楚如何将多行使用ruby代码传递给单个link_to方法.我要找的结果是你单击列并获得显示页面:
<div class="subcolumns">
<div class="c25l">
<div class="subcl">
<%= image_tag album.photo.media.url(:thumb), :class => "image" rescue nil %>
</div>
</div>
<div class="c75r">
<div class="subcr">
<p><%= album.created_at %></p>
<%= link_to h(album.title), album %>
<p><%= album.created_at %></p>
<p><%= album.photo_count %></p>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我无法弄清楚为什么我的flash消息在redirect_to之后消失了.在我的视图中启动了调试器,flash变量完全为空.
flash
=> {}
Run Code Online (Sandbox Code Playgroud)
结果与flash.now相同...如果我编辑一些东西并调用渲染,它的工作正常.
控制器:
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Logged in"
redirect_to root_url
else
render :action => 'new'
end
end
Run Code Online (Sandbox Code Playgroud)
应用布局:
- flash.each do |name, msg|
=content_tag :div, msg, :class => "flash_#{name}"
Run Code Online (Sandbox Code Playgroud)
root_url是另一个控制器和动作.
我想验证我的用户,因此他们只能在用户名中使用az和 - .
validates_format_of :username, :with => /[a-z]/
Run Code Online (Sandbox Code Playgroud)
但是这个规则也允许空格._ @
Username should use only letters, numbers, spaces, and .-_@ please.
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
最好的祝福.AsbjørnMorell
我看到我的全新Rails 3.1 rc5应用程序在生产模式下运行时随机崩溃.后端是nginx,有3个瘦实例.
[Exception] users#show (NoMethodError) "undefined method `text?' for nil:NilClass"
undefined method `text?' for nil:NilClass
activerecord (3.1.0.rc5) lib/active_record/validations/uniqueness.rb:57:in `build_relation'
Run Code Online (Sandbox Code Playgroud)
我打开了log:debug.但是唯一的信息是"内部服务器错误"以前有人见过这个吗?身份地图被转为...
如果我重新启动瘦,Rails几个小时就会运行没问题.
Update1:看起来几乎所有我的控制器动作都会随机生成此错误.今天早上我收到了一封上面有错误的电子邮件.如果我按照链接,生成页面没有问题.
我想知道这个问题是否与从cookie加载用户的问题有关?
class ApplicationController < ActionController::Base
protect_from_forgery
layout "content_with_sidebar"
before_filter :system
rescue_from CanCan::AccessDenied do |exception|
flash[:warning] = "Ingen adgang!"
redirect_to root_url
end
def system
if current_user
unless current_user.email_verified == "t"
flash[:warning] = 'Please activate your account.'
end
current_user.last_seen = Time.now
current_user.save # <--- line 18
@new_messages = Message.new_messages(current_user).count
end
end
helper_method :current_user
private
def current_user
if cookies[:auth_token] …Run Code Online (Sandbox Code Playgroud) 我已经设置了我的模型以使用多态Image模型.这工作正常,但我想知道是否可以更改每个模型的:样式设置.找到一些使用STI的示例(模型<图像)但是这对我来说不是一个选项,因为我使用的是has_many关系.
艺术
has_many :images, :as => :imageable
Run Code Online (Sandbox Code Playgroud)
图片
belongs_to :imageable, :polymorphic => true
has_attached_file :file, :styles => { :thumb => "150x150>", :normal => "492x600>"}
#Change this setting depending on model
Run Code Online (Sandbox Code Playgroud)
我尝试在Proc方法中启动调试器.仅填充与附加文件相关的字段:
run'irb(Image):006:0> a.instance => #<Image id: nil, created_at: nil, updated_at: nil, imageable_id: nil, imageable_type: nil, file_file_name: "IMG_9834.JPG", file_content_type: "image/jpeg", file_file_size: 151326, file_updated_at: "2010-10-30 08:40:23">
Run Code Online (Sandbox Code Playgroud)
这是ImageController #create的对象
ImageController#create
@image => #<Image id: nil, created_at: nil, updated_at: nil, imageable_id: 83, imageable_type: "Art", file_file_name: "IMG_9834.JPG", file_content_type: "image/jpeg", file_file_size: 151326, file_updated_at: "2010-10-30 08:32:49"> …Run Code Online (Sandbox Code Playgroud) 我的大多数js.erb文件底部都包含这样的内容:
$("#flash_message").html("<%= escape_javascript(content_tag(:p, flash[:note], :class => "note")) %>");
$("#flash_message").fadeOut(2000);
$("#loading").remove();
Run Code Online (Sandbox Code Playgroud)
我想将这些行移动到一个单独的文件中,然后从我的每个js.erb文件中调用该文件.有可能吗?
最好的祝福.AsbørnMorell
我正在尝试加载由user_id分组并由created_at排序的最新10种艺术.这适用于SqlLite和MySQL,但在我的新PostgreSQL数据库上出错.
Art.all(:order => "created_at desc", :limit => 10, :group => "user_id")
Run Code Online (Sandbox Code Playgroud)
ActiveRecord错误:
Art Load (18.4ms) SELECT "arts".* FROM "arts" GROUP BY user_id ORDER BY created_at desc LIMIT 10
ActiveRecord::StatementInvalid: PGError: ERROR: column "arts.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "arts".* FROM "arts" GROUP BY user_id ORDER BY crea...
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我目前正在为Rails 3的模型设置Paperclip.当其中一个字段未通过验证(标题)时,用户必须再次上传文件.这不是非常用户友好:/
Paperclip论坛的建议是将Paperclip内容转移到相关模型中.我的模型很简单,只有几个字段,所以我想避免有两个页面/步骤来创建一个记录.
arts/create(有效时) - > arts_image/create
有什么建议?
如何选择按created_at desc排序的列分组的所有记录.
我可能想要检索每篇文章的所有最新评论.无论我做什么组(:article_id),总会返回最早的评论.
最好的祝福.AsbjørnMorell
我的消息模型属于作者和收件人。
belongs_to :recipient, :class_name => "User", :foreign_key => "recipient_id"
belongs_to :author, :class_name => "User", :foreign_key => "author_id"
Run Code Online (Sandbox Code Playgroud)
现在,我想做的是在User模型中建立has_many关系,该关系在单个查询中获取用户是以太作者或收件人的所有消息。我该怎么做?
has_many :messages, :finder_sql => ['author_id = #{self.id} or recipient_id = #{self.id}']
Run Code Online (Sandbox Code Playgroud)
但是,这打破了。
User.first.messages
User Load (0.6ms) SELECT "users".* FROM "users" LIMIT 1
Message Load (0.5ms) author_id = #{self.id} or recipient_id = #{self.id}
PGError: ERROR: syntax error at or near "author_id"
LINE 1: author_id = #{self.id} or recipient_id = #{self.id}
^
:author_id = #{self.id} or recipient_id = #{self.id} ActiveRecord::StatementInvalid: PGError: ERROR: syntax error …Run Code Online (Sandbox Code Playgroud)