要处理规范URL,最佳做法是进行301重定向或更好地为www和非www域设置相同的IP地址?
例如:
Canonical URL/domain wanted是http://mydomain.com
Domain | A Record
------------------------------------
mydomain.com | 58.162.62.34
www.mydomain.com | 58.162.62.34
Run Code Online (Sandbox Code Playgroud)
我知道人们可能有www的CNAME记录(别名,因为CNAME被称为Canonical Name); 与使用相同的IP地址相比,不确定这是否是最佳做法.
或者这更好.
Domain | A Record
------------------------------------
mydomain.com | 58.162.62.34
* www.mydomain.com 301 redirect to mydomain.com
Run Code Online (Sandbox Code Playgroud) 使用Twitter Bootstrap实现Modernizr是否可以?
我目前正在使用带有Google的html5shiv的Bootstrap,我想知道我是否可以使用Modernizr,或者只是为了让老版IE浏览器激活HTML5元素而过度杀戮?
提前致谢
我只是想知道如何在Rails控制台中使用Carrierwave上传远程文件URL.
我试了以下没有任何运气.我认为它不处理上传器?
user = User.first
user.remote_avatar_url = "http://www.image.com/file.jpg"
user.save
Run Code Online (Sandbox Code Playgroud)
非常感谢
我需要一些关于Rails 4如何与has_one和belongs_to关联一起工作的指针.
我的表格没有保存has_one
关系
发布模型
class Post < ActiveRecord::Base
validates: :body, presence: true
has_one :category, dependent: :destroy
accepts_nested_attributes_for :category
end
class Category < ActiveRecord::Base
validates :title, presence: true
belongs_to :post
end
Run Code Online (Sandbox Code Playgroud)
后控制器
class PostController < ApplicationController
def new
@post = Post.new
@post.build_category
end
def create
@post = Post.new(post_params)
end
private
def post_params
params.require(:post).permit(:body)
end
end
Run Code Online (Sandbox Code Playgroud)
表格在Post#new action中
<%= form_for @post do |form| %>
<%= form.label :body %>
<%= form.text_area :body %>
<%= fields_for :category do |category_fields| %>
<%= category_fields.label …
Run Code Online (Sandbox Code Playgroud) ruby-on-rails form-for actioncontroller strong-parameters ruby-on-rails-4
美好的一天,
有人可以帮助我使用嵌套资源及其最佳实践.
我想我的限制:events
路由只:show
和:index
,这是做它的正确方法是什么?
resources :events do
resources :registrations, :except => [:index]
end
resources :events, :only => [:index, :show]
Run Code Online (Sandbox Code Playgroud)
这是最好的方式还是更多Rubyist会处理这种事情的方式?我添加了两行resources :events
或有没有办法将它们组合在一个块中?
提前致谢.
routes ruby-on-rails nested-resources nested-routes ruby-on-rails-3
我正在阅读一些Rails 3和4教程并遇到一些我喜欢的一些见解:
关于Create操作,Model.new和Model.create之间有什么区别.我以为你确实使用create
控制器中的方法来保存例如.@post = Post.create(params[:post])
但看起来我错了.任何见解都非常感谢.
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
@post.save
redirect_to post_path(@post)
end
def post_params
params.require(:post).permit(:title, :body)
end
Run Code Online (Sandbox Code Playgroud)
def new
@post = Post.new
end
def create
@post = Post.create(post_params)
@post.save
redirect_to post_path(@post)
end
def post_params
params.require(:post).permit(:title, :body)
end
Run Code Online (Sandbox Code Playgroud)
我有两个问题
@post = Post.create(post_params)
吗?我正在学习Spree 3.0,我设置了一个卖短裤的测试店.
短裤有多种选择类型:尺寸,颜色,长度
我想改变它从前端复选框到下拉框显示前端变体选项的方式.
目前,Spree将选项类型显示为单选按钮:
我想更改此选项以使用每个选项类型的下拉菜单,如下所示:
我尝试过以下方法:
<%= select_tag "variant_id", options_for_select(@product.variants_and_option_values(current_currency).collect{ |v| ["#{variant_options(v)} #{variant_price(v)}", v.id] })%>
Run Code Online (Sandbox Code Playgroud)
但它只显示每个标记中所有选项类型的值:
我想知道将选项值拆分为单个下拉菜单的最佳方法吗?
任何帮助都非常感谢,谢谢.
我在我的Rails 3示例应用程序上使用CarrierWave.我想验证远程位置上传,因此当用户提交无效的URL空白或不是图像时,我不会收到标准错误异常:
CarrierWave::DownloadError in ImageController#create
trying to download a file which is not served over HTTP
Run Code Online (Sandbox Code Playgroud)
这是我的模特:
class Painting < ActiveRecord::Base
attr_accessible :gallery_id, :name, :image, :remote_image_url
belongs_to :gallery
mount_uploader :image, ImageUploader
validates :name, :presence => true,
:length => { :minimum => 5, :maximum => 100 }
validates :image, :presence => true
end
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
class PaintingsController < ApplicationController
def new
@painting = Painting.new(:gallery_id => params[:gallery_id])
end
def create
@painting = Painting.new(params[:painting])
if @painting.save
flash[:notice] = "Successfully created painting."
redirect_to @painting.gallery
else …
Run Code Online (Sandbox Code Playgroud) 我正在处理有关处理嵌套表单数据的一些建议,我会非常感谢任何见解.
问题是我不能100%确定为什么我需要在我的模型中使用以下代码
accepts_nested_attributes_for :holiday_image, allow_destroy: true, :reject_if => lambda { |a| a[:title].blank? }
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我需要在accept_nested_attributes_for关联上进行操作:
:reject_if => lambda { |a| a[:title].blank? }
Run Code Online (Sandbox Code Playgroud)
如果我删除它:reject_if lambda,它将在数据库中保存一个空白的假日照片对象.我推测是因为它将表单中的:title字段作为空字符串?
我想我的问题是,如果我想扩展我的HolidayImage模型以包含更多字符串(如描述,注释),我是否正确执行此操作或在嵌套表单中有更好的方法吗?
对不起如果我不能更简洁.
我简单的假期应用.
# holiday.rb
class Holiday < ActiveRecord::Base
has_many :holiday_image
accepts_nested_attributes_for :holiday_image, allow_destroy: true, :reject_if => lambda { |a| a[:title].blank? }
attr_accessible :name, :content, :holiday_image_attributes
end
Run Code Online (Sandbox Code Playgroud)
我正在使用CarrierWave进行图片上传.
# holiday_image.rb
class HolidayImage < ActiveRecord::Base
belongs_to :holiday
attr_accessible :holiday_id, :image, :title
mount_uploader :image, ImageUploader
end
Run Code Online (Sandbox Code Playgroud)
在我的_form部分里面有一个field_for块
<h3>Photo gallery</h3>
<%= f.fields_for :holiday_image do |holiday_image| %>
<% if holiday_image.object.new_record? %> …
Run Code Online (Sandbox Code Playgroud) 有没有办法设置config.middleware.use
排除/忽略/跳过特定的控制器操作?
例如,如果我想排除Post#show
控制器操作。
我正在使用https://github.com/railslove/rack-tracker来测试 Google Analytics/Tag Manager。
# Rack Tracker config
config.middleware.use(Rack::Tracker) do
handler :google_tag_manager, { container: ENV['GTM_CONTAINER_ID'] }
end
Run Code Online (Sandbox Code Playgroud)
我以为我可以使用以下条件:
# Rack Tracker config
config.middleware.use(Rack::Tracker) do
handler :google_tag_manager, { container: ENV['GTM_CONTAINER_ID'] } if app.route != ApplicationController::PostController.show
end
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢。