我在这里看到了一些关于向现有列添加默认布尔值的问题(即这一个).所以我尝试了这个change_column建议,但我一定不能做得对.
我试过了:
$ change_column :profiles, :show_attribute, :boolean, :default => true
Run Code Online (Sandbox Code Playgroud)
哪个回报 -bash: change_column: command not found
然后我跑了:
$ rails g change_column :profiles, :show_attribute, :boolean, :default => true
Run Code Online (Sandbox Code Playgroud)
...和
$ rails change_column :profiles, :show_attribute, :boolean, :default => true
Run Code Online (Sandbox Code Playgroud)
然后跑了rake db:migrate,但价值:show_attribute仍然存在nil.在我上面提到的问题中,它在PostgreSQL中说你需要手动更新它.由于我正在使用PostgreSQL,因此我在create_profiles迁移中添加了以下内容:
t.boolean :show_attribute, :default => true
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这里我做错了什么?
我已经看过如何添加<span>标签但是我没有看到<span>使用Rails 3 放置我想要的位置的示例link_to:
<a href="#" class="button white"><span id="span">My span </span>My data</a>
Run Code Online (Sandbox Code Playgroud)
我尝试过类似的东西:
<%= link_to(content_tag{:span => "My span ", :id => "span"} @user.profile.my_data, "#", {:class => "button white"}) %>
Run Code Online (Sandbox Code Playgroud)
但那没用.
我知道我可以request.referrer用来获取Rails中的完整请求URL,但有没有办法从请求中获取控制器名称?
我想看看http://myurl.com/profiles/2的网址是否包含"个人资料"
我知道我可以使用正则表达式来做,但我想知道是否有更好的方法.
在我的CSS中,我为一个类定义了一个过渡.出于某种原因,当我将鼠标悬停在具有转换的类上时,由于transition-duration某种原因会改变其他地方的字体颜色(表单占位符和某些链接).(就我所知,这只发生在Safari中.)
这是一个jsFiddle,显示我在说什么:
有谁知道为什么会这样,我怎么能阻止它?
我试图改变nil现有的列上一个布尔属性的可能性:access_titles上:profiles表.由于此迁移,该列存在:
class AddAccessItemsToProfiles < ActiveRecord::Migration
def self.up
add_column :profiles, :access_items, :boolean, :default => true
end
def self.down
remove_column :profiles, :access_items, :boolean, :default => nil
end
end
Run Code Online (Sandbox Code Playgroud)
为了改变nil,我试着像往常一样生成迁移:
rails g migration ChangeColumnNull :profiles :access_items :null => false
Run Code Online (Sandbox Code Playgroud)
但这没有做任何事情,所以我做了一个独立的迁移:
rails g migration AddChangeColumnNullToAccessItems
Run Code Online (Sandbox Code Playgroud)
在那之内我补充说:
class AddChangeColumnNullToAccessItems < ActiveRecord::Migration
def self.up
change_column_null :profiles, :access_items, :boolean, false
end
def self.down
change_column_null :profiles, :access_items, :boolean, true
end
end
Run Code Online (Sandbox Code Playgroud)
然后我跑了rake db:migrate,重新启动了我的服务器,没有看到任何变化.所以我尝试过:
class AddChangeColumnNullToAccessItems < ActiveRecord::Migration
def …Run Code Online (Sandbox Code Playgroud) 我正在使用Paperclip处理我的应用中的个人资料照片上传.他们上传得很好,并调整到我的模型中的规格.但是,如果用户的个人资料:照片为零,无论我尝试什么,我都无法更改默认值.这是我想要使用的代码:
<% if @profile.photo.nil? %>
<%= image_tag "public/images/example.jpg", :html => { :id => "noUserProfile" } %>
<% else %>
<%= image_tag @profile.photo.url(:normal) %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我已经尝试了"../public/images/example.jpg",即使我的公共图片文件夹中有"example.jpg",也无法正常工作.当我在视图中复制图像地址时,我得到:
http://localhost:3000/photos/normal/missing.png
Run Code Online (Sandbox Code Playgroud)
我将这些文件夹添加到我的应用程序并在其中放入missing.png文件,没有任何内容.如果我转到上面的URL我得到No route matches "/photos/normal/missing.png"
有没有人对于发生了什么有任何想法?
配置文件模型中的has_attached_file:
has_attached_file :photo,
:styles => {
:normal => "153x220#",
:small => "75x108#" }
Run Code Online (Sandbox Code Playgroud)
add_attachment_photo_to_profile migration:
class AddAttachmentPhotoToProfile < ActiveRecord::Migration
def self.up
add_column :profiles, :photo_file_name, :string
add_column :profiles, :photo_content_type, :string
add_column :profiles, :photo_file_size, :integer
add_column :profiles, :photo_updated_at, :datetime
end
def self.down
remove_column :profiles, :photo_file_name
remove_column …Run Code Online (Sandbox Code Playgroud) 在Rails 3中设置性别字段的最佳方法是什么?我正在使用PostgreSQL.
我现在把它作为一个字符串,但我想知道它是否更容易(更好?)将其设置为整数.如果是这样,怎么办呢?我想为它制作一个下拉列表,其中包括三个值:"男性","女性","没有您的业务".
对不起基本问题,但我很好奇,简单,最好的做法.
我在我的Rails 3应用程序中有一个表单,我想在方法上创建select一年的标签:grad_year.我有一切工作 - 和存储 - 正确使用date_select和添加:discard_month和:discard_day.但是,当我渲染时,@profile.grad_year我得到了月和日值.所以,我不知道如何来存储和渲染 仅在一年@profile.grad_year?
这是表格:
<%= f.date_select :grad_year, {:start_year => Time.now.year, :end_year => Time.now.year - 95, :discard_day => true, :discard_month => true}, :selected => @profile.grad_year %>
Run Code Online (Sandbox Code Playgroud)
在我的迁移中:
t.date :grad_year
Run Code Online (Sandbox Code Playgroud) 我想在我的网站上实现一个简单的通知系统,向用户显示一个看不见/未读的项目.类似于Stack Exchange中用于用户收件箱的内容,其中显示了对问题的未读评论等.
我遇到了这个问题,概述了我是如何做到这一点的.我很困惑的是如何弄清楚是否有东西被读过.我可以添加一个read_at列但是如何实际填充它?如果有人能帮我提供一些基本的指导,我会很感激!
更新#1:如果我在我的项目#show action中添加条件,我会检查user_id(创建项目的用户的ID)current_user.id.像下面这样的东西:
unless @item.user_id == current_user.id
@item.read_at = Time.now
end
Run Code Online (Sandbox Code Playgroud)
更新#2:使用下面的代码,我试图更新消息,read_at如果它recipient_id与current_userID 匹配.然而,它不起作用.
def show
@message = Message.find(params[:id])
if @message.recipient_id == current_user.id
@message.read_at == Time.now
@message.save
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @message }
end
end
Run Code Online (Sandbox Code Playgroud)
最后更新:感谢@prasvin,这是我的解决方案.我read_at在对象中添加了一列.该对象还具有现有recipient_id列.所以在我的Controller的show动作中,我提出以下内容:
def show
@message = Message.find(params[:id])
if @message.recipient_id == current_user.id
@message.update_attributes(:read_at => Time.now)
end
respond_to …Run Code Online (Sandbox Code Playgroud) 我有一个表单在我的用户设置中工作,用于显示或隐藏用户个人资料的一部分.表单是a check_box_tag,当单击它时,我使用jQuery提交表单.这一切都运作良好.单击该复选框可将布尔值从中切换为true,false反之亦然.但是,如果我的布尔值是,我希望复选框显示为已选中false.考虑到下面的代码,我该怎么做?
我的控制器update_attribute对配置文件的操作:
def edit_show_hometown_settings
@profile = current_user.profile
if @profile.show_hometown == true
if @profile.update_attributes(:show_hometown => false)
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
elsif @profile.show_hometown == false
if @profile.update_attributes(:show_hometown => true)
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的表格:
<%= form_tag({:action => "edit_show_hometown_settings", :controller => "profiles"}, :html => {:multipart => true }) do …Run Code Online (Sandbox Code Playgroud)