我想补充time_select用include_blank.我这样做:
<%= f.time_select :start_at, include_blank: true, ampm: true %><br>
Run Code Online (Sandbox Code Playgroud)
我想要做的是删除值(保存为零?)如果在视图中选择了空白.
虽然我尝试了以下帖子,但它对我不起作用.
带allow_blank的可选time_select默认为00:00
1)当我尝试如下时,没有出现错误,但00:00:00保存.
调节器
def update
@event = Event.find(params[:id])
if event_params["start_at(4i)"].blank? or event_params["start_at(5i)"].blank?
@event.start_at = nil
end
if @event.update(event_params)
flash[:success] = "event updated!"
redirect_to root_url
else
render 'edit'
end
end
Run Code Online (Sandbox Code Playgroud)
2)当我尝试如下(更改if子句)时,不会出现错误,但00:00:00会保存.
调节器
def update
@event = Event.find(params[:id])
if params[:id]["start_at(4i)"].blank? or params[:id]["start_at(5i)"].blank?
@event.start_at = nil
end
if @event.update(event_params)
flash[:success] = "event updated!"
redirect_to root_url
else
render 'edit'
end
end
Run Code Online (Sandbox Code Playgroud)
3)当我尝试如下(添加before_action)时,没有出现错误,但00:00:00保存. …
Your connection is not private当我尝试访问我注册的网址时,会显示隐私错误(使用Chrome)CNAME.
我开发了Rails应用程序,这是第一次使用Heroku.
虽然当我使用原始URL时没有显示https://floating-fortress-99999.herokuapp.com/错误,但是当我使用时会显示错误www.my_app.com.
浏览器上的所有错误消息如下:
Your connection is not private
Attackers might be trying to steal your information from www.my_app.com (for example, passwords, messages, or credit cards).
NET::ERR_CERT_COMMON_NAME_INVALID
This server could not prove that it is www.my_app.com; its security certificate is from *.herokuapp.com. This may be caused by a misconfiguration or an attacker intercepting your connection.
Run Code Online (Sandbox Code Playgroud)
www.my_app.com当我点击Proceed to www.my_app.com (unsafe)浏览器上的链接时,我可以显示.
是否可以避免显示"隐私错误"?
如果你能告诉我如何避免这个错误,将不胜感激.
我正在使用Rails4和Carrierwave上传文件,我有它的工作.我来用这个aritcle检查图像大小,2.模型级验证.
我validate :image_size_validation在models\article.rb中添加了,但是NoMethodError in ArticlesController#create出现了.
如何查看图像尺寸?
我应该使用file_size_validator.rb(参考如何:验证附件文件大小)而不是上面的文章中的解决方案?
有任何想法吗?提前致谢.
\型号\ article.rb
class Article < ActiveRecord::Base
...
has_many :photos, dependent: :destroy
validate :image_size_validation
...
private
def image_size_validation
errors[:image] << "should be less than 1MB" if photos.image.size > 1.megabytes
end
end
Run Code Online (Sandbox Code Playgroud)
\型号\ photo.rb
class Photo < ActiveRecord::Base
belongs_to :article
mount_uploader :image, ImageUploader
end
Run Code Online (Sandbox Code Playgroud)
\控制器\ article_controller.rb
...
def create
@article = current_user.articles.build(article_params)
if @article.save
flash[:success] = "article created!"
redirect_to current_user #root_url
else
@article.build_images
render …Run Code Online (Sandbox Code Playgroud) 我有一个Event model有form时间和to时间在我的日程安排应用程序,我要在保存之前验证重叠的时间。
我的视图图像如下;
Departure date: Dec 31, 2016
Day1
07:00 - 07:20 event1
10:30 - 11:30 event2
15:40 - 16:10 event3
[add event button]
Day2
08:15 - 09:05 event4
12:08 - 13:04 event5
14:00 - 14:25 event6
[add event button]
[save schedule button]
Run Code Online (Sandbox Code Playgroud)
from时间和to时间可以同时更改和添加。
如果我尝试添加(或更改为)07:05 - 07:30for Day1,例如13:50 - 14:30forDay2等等,我想要做的是显示错误。
虽然我尝试了一些代码overlap,between,cover与参照 这篇文章或者这个职位等等,我还没有能够将其应用到我的代码。
模式文件
create_table "events", …Run Code Online (Sandbox Code Playgroud) 我可以上传文件并将文件名保存在数据库中.但是,当我编辑时,文件名不会出现.
我想:
article.rb
class Article < ActiveRecord::Base
.
.
has_many :photos, dependent: :destroy
accepts_nested_attributes_for :photos
.
.
end
Run Code Online (Sandbox Code Playgroud)
photo.rb
class Photo < ActiveRecord::Base
belongs_to :article
mount_uploader :image, ImageUploader
validates :image, presence: true
#validates :article_id, presence: true
end
Run Code Online (Sandbox Code Playgroud)
.schema文章
CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"content" varchar(255),
"user_id" integer,
"created_at" datetime,
"updated_at" datetime,);
Run Code Online (Sandbox Code Playgroud)
.schema照片
CREATE TABLE "photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"article_id" integer,
"image" varchar(255),
"created_at" datetime,
"updated_at" datetime);
Run Code Online (Sandbox Code Playgroud)
articles_controller.rb
class ArticlesController …Run Code Online (Sandbox Code Playgroud) 我的时间表应用中有一个Event model具有start_at时间和end_at时间的,并且我想在保存之前验证重叠时间。
我在Cloud9上创建我的rails应用程序。
我的观看图片如下:
Day1
07:00 - 07:20 event1
10:30 - 11:30 event2
15:40 - 16:10 event3
[add event button]
Day2
08:15 - 09:05 event4
12:08 - 13:04 event5
14:00 - 14:25 event6
[add event button]
[save schedule button]
Run Code Online (Sandbox Code Playgroud)
start_at时间和end_at时间可以同时更改和添加。
我想做的是显示错误,如果我尝试添加(或更改为)07:05 - 07:30for Day1,例如13:50 - 14:30for Day2等。
例如;
app_development =#从事件中选择*;
id | start_at | end_at | title | detail | schedule_id | created_at …Run Code Online (Sandbox Code Playgroud) 我想在“_article.html.erb”的评论模型中显示“代码”。我应该如何修改以下来源?
.schema 文章
CREATE TABLE "articles" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"content" varchar(255),
"user_id" integer,
"comment_id" integer,
"created_at" datetime,
"updated_at" datetime);
Run Code Online (Sandbox Code Playgroud)
.schema 注释
CREATE TABLE "comments" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"code" integer,
"created_at" datetime,
"updated_at" datetime);
Run Code Online (Sandbox Code Playgroud)
文章.rb
class Article < ActiveRecord::Base
belongs_to :user
belongs_to :comment
end
Run Code Online (Sandbox Code Playgroud)
评论.rb
class Comment < ActiveRecord::Base
has_many :articles
end
Run Code Online (Sandbox Code Playgroud)
用户/show.html.erb
<%= render @articles %>
Run Code Online (Sandbox Code Playgroud)
文章/_article.html.erb
<li>
<%= article.comment.code %>
<span class="content"><%= article.content %></span>
by <%= article.user.name %>
</li> …Run Code Online (Sandbox Code Playgroud)