我正在处理有关处理嵌套表单数据的一些建议,我会非常感谢任何见解.
问题是我不能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)
任何帮助深表感谢。
我已经尝试运行以下命令将我的Rails版本从版本3.2.6更新到版本3.2.7但没有成功.
gem update rails
Updating installed gems
Nothing to update
Run Code Online (Sandbox Code Playgroud)
-
gem install rails
# Output
# 1 gem installed
# Installing ri documentation for rails-3.2.6...
# Installing RDoc documentation for rails-3.2.6...
Run Code Online (Sandbox Code Playgroud)
-
gem install rails -v 3.2.7
ERROR: Could not find a valid gem 'rails' (= 3.2.7) in any repository
ERROR: Possible alternatives: rails
Run Code Online (Sandbox Code Playgroud)
-
我已经尝试清除我的Rubygem缓存,我的源列表如下
*** CURRENT SOURCES ***
http://rubygems.org/
http://gems.github.com/
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢.
谢谢,WD
我将自定义部署_variables.scss到我的生产服务器作为编译资产时出现问题.
在我的开发环境中,一切都很好,生产中我的变量被覆盖了.
我正在使用Rails 4.2.1和Spree 3.0 Stable分支.
我有以下结构:
创建的文件 vendor/assets/stylesheets/frontend
将_variables.scss包含以下内容:
// Place all Sass variables here.
// Colors
$brand-primary: green;
$gray: #aaa;
// Navbar
$navbar-default-bg: #fff;
$navbar-height: 100px;
$navbar-border-radius: 0;
$navbar-default-border: none;
$navbar-default-toggle-hover-bg: $navbar-default-bg;
$navbar-default-toggle-icon-bar-bg: lighten($gray, 60%);
$navbar-default-toggle-border-color: $navbar-default-bg;
$navbar-default-link-active-bg: $brand-primary;
Run Code Online (Sandbox Code Playgroud)
将frontend_boostrap.css.scss包含以下内容:
// Spree Bootstrap Override
// Core
@import "variables";
@import "bootstrap-sprockets";
@import "bootstrap";
// Custom Overrides
@import "navbar";
Run Code Online (Sandbox Code Playgroud)
将navbar.scss包含以下内容:
// Navbar Customization
.navbar-myapp {
margin-bottom: 40px;
border-top: …Run Code Online (Sandbox Code Playgroud) 我一直在使用破折号作为 css 类和 ID 名称的分隔符:
.about-us
.car-pricing-guide
Run Code Online (Sandbox Code Playgroud)
我见过网站使用多个下划线 __
.home__content
Run Code Online (Sandbox Code Playgroud)
另外,我看到了2个破折号
.hero--title
Run Code Online (Sandbox Code Playgroud)
我知道这是一个偏好问题,但有人可以指出这些模式的文章或流程吗?是否有朝着特定惯例的方向发展,或者是否存在人们似乎同意/遵循的一组流行惯例?
我想展示Posts哪belongs_to :categories一个接一个.
帖子控制器:
def index
@posts = posts.order('created_at DESC').all
end
Run Code Online (Sandbox Code Playgroud)
发布#index视图:
<% @posts.each do |post| %>
<article>
<h1><%= post.title %></h1>
<p><%= post.content %></p>
<span><%= post.category %><span>
</article>
<% end %>
Run Code Online (Sandbox Code Playgroud)
通过上面的示例,它将显示所有posts基于创建日期时间.
更新
如果我有两个类别,如burgers和sandwiches,我希望它首先显示一个汉堡邮政,然后一个三明治邮政,然后汉堡邮政然后三明治邮政.因此,它在两个类别之间交替,例如:
<article id="1">
<h1>Cheese Burger<h1>
<p>Content...</p>
<span>Burgers category<span>
</article>
<article id="2">
<h1>Ham Sandwich<h1>
<p>Content...</p>
<span>Sandwiches category<span>
</article>
<article id="3">
<h1>Chicken Burger<h1>
<p>Content...</p>
<span>Burgers category<span>
</article>
<article id="4">
<h1>Tomato Sandwich<h1>
<p>Content...</p>
<span>Sandwiches category<span>
</article>
Run Code Online (Sandbox Code Playgroud) 我正在使用Devise,用户既可以是管理员也可以不是(真,假).
有人可以帮我弄清楚如何保护控制器动作,例如:
def new
@post = Post.new
end
Run Code Online (Sandbox Code Playgroud)
我知道Devise允许你通过在视图中使用来调用管理员,但是这可以在控制器中使用吗?:
if current_user.admin?
Run Code Online (Sandbox Code Playgroud)
提前致谢.
有人可以指出我正确的方向.
我正在使用after_validation来竞争地理编码,我想知道如何在一个或者||上进行操作 if子句的选项.
这按预期工作
class Address < ActiveRecord::Base
after_validation :geocode, if: :address_changed?
Run Code Online (Sandbox Code Playgroud)
但事实并非如此
class Address < ActiveRecord::Base
after_validation :geocode, if: :address_changed? || :zipcode_changed?
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我对如何在URL中处理多个param输入以在Rails 3.2中构建过滤器感到有点迷茫
基本上,我想将http://example.com/products/?category=24和http://example.com/products/?category=24&country=24一起工作.
我有以下型号Product,Country,Category,Categorization
我可以在我的身上建立一个简单的条件 ProductController
def index
@products = product.all
if params[:country]
@products = Country.find(params[:country]).products
end
if params[:category]
@products = Category.find(params[:category]).products
end
end
Run Code Online (Sandbox Code Playgroud)
我想知道解决这个问题的最佳方法,因为我Category和Product模型通过Categorizationhas_many关联模型.
class Product < ActiveRecord::Base
has_many :categorization
belongs_to :country
end
class Category < ActiveRecord::Base
has_many :categorization
has_many :products, through: :categorization
attr_accessible :name
end
class Categorization < ActiveRecord::Base
belongs_to :category
belongs_to :product
attr_accessible :category, :product
end
Run Code Online (Sandbox Code Playgroud)
我的视图布局模板是一个简单的类别和国家/地区链接列表.因此,如果有人点击了名为"玩具"的类别以及国家/地区"英格兰"的点击,则应该建立一个类似的链接:http://www.example.com/products/category=12&country_id=1 …
我通过rbenv(ruby-build)安装了Ruby 2.0.0-p247 ,不幸的是我的Pow.cx无效.
有没有人有过这样的经历?
我得到以下战俘
Error starting application
Your Rack app raised an exception when Pow tried to run it.
~/.rbenv/versions/1.9.3-p429/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
~/.rbenv/versions/1.9.3-p429/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
~/Code/sandwiches_app/config/boot.rb:6:in `<top (required)>'
~/.rbenv/versions/1.9.3-p429/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
~/.rbenv/versions/1.9.3-p429/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
Run Code Online (Sandbox Code Playgroud)
非常感谢任何见解.它看起来像是在查看1.9.3版本的Ruby,但我已经设置了rbnenv全局,shell和本地(在应用程序.ruby-version中)以使用Ruby 2.0.0-p247.我正在使用ZSH并尝试更新自制程序,rbenv以及冷启动.
有没有办法使用Ruby或可能的rails帮助方法将HTML .class添加到无序列表项?
基本上我只是通过todo模型迭代
<ul>
<% @todos.each do |todo| %>
<li><%= todo.task %></li>
<% end %>
</ul>
Run Code Online (Sandbox Code Playgroud)
我想在第三个列表项中添加一个class ="third",以便HTML输出看起来像
<ul>
<li>Task 1</li>
<li>Task 2</li>
<li class="third">Task 3</li>
<ul>
Run Code Online (Sandbox Code Playgroud)
我使用jQuery来添加类,但我想要一个更好的方法来做这个而不是在javascript上转发.
谢谢大家.