我正在开发一个需要管理子域名的应用程序.
当我试图获取请求的子域名时,即使有一个域名,我也会得到nil并获得整个主机域名:
# GET patate.localhost:3000
request.subdomain # -> nil
request.subdomains # -> []
request.domain # -> patate.localhost
request.host # -> patate.localhost
Run Code Online (Sandbox Code Playgroud)
有谁知道我的方法有什么问题?
编辑
我的/ etc/hosts文件:
127.0.0.1 localhost
127.0.0.1 patate.localhost
Run Code Online (Sandbox Code Playgroud) 如何在Ruby on Rails 4.1.4中实现以下行为?
Purchase.where('purchase_date <= ?', 6.months.ago - 1.days.ago).each do |purchase|
# ...
end
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我正在使用friendly_id gem生成漂亮的URL。我有一个模型,Questionnaire。我的路线如下所示:
roi_summary_questionnaire GET /questionnaires/:id/roi_summary(.:format) questionnaires#roi_summary
lack_of_internal_controls_questionnaire GET /questionnaires/:id/lack_of_internal_controls(.:format) questionnaires#lack_of_internal_controls
questionnaires POST /questionnaires(.:format) questionnaires#create
new_questionnaire GET /questionnaires/new(.:format) questionnaires#new
Run Code Online (Sandbox Code Playgroud)
和链接如下所示:子弹(company_name)http://myapp/questionnaires/foo/roi_summary在哪里foo。
我的目标是摆脱生成的url中的控制器名称,即:http://myapp/foo/roi_summary。我的很独特。
最好的方法是什么?谢谢!
我为我的模型"Things"创建了一个编辑表单,但是当我提交表单时,它会返回错误"No route matches [PATCH]"/ things"".奇怪的是,错误的网址是http://localhost:3000/things,但我期待它http://localhost:3000/things/:id,因为我在create function下编写了redirect_to @thing.我刚刚用资源定义了所有我的Thing路由:事情,所以它应该定义更新路径.我究竟做错了什么?
things_controller:
class ThingsController < ApplicationController
def show
@thing = Thing.find(params[:id])
@category_things = CategoryThing.all
@thing.categories.build
@thing.things.build
@related_things = RelatedThing.all
end
def index
end
def new
@thing = Thing.new
@things = Thing.all
end
def create
@thing = Thing.new(thing_params)
if @thing.save
redirect_to @thing
else
render 'new'
end
end
def edit
@thing = Thing.find(params[:id])
end
def update
@thing = Thing.find(params[:id])
if @thing.update_attributes(thing_params)
flash[:success] = "Thing updated"
redirect_to @thing
else
render 'edit'
end
end …Run Code Online (Sandbox Code Playgroud) 我试图这样做,如果有人发一个没有标题的帖子,帖子将获得默认标题.
到目前为止我已经尝试过了
调节器
def create
@post = Post.create(post_params)
if @post.title.nil?
@post.update(:title => 'Unnamed')
else
# DO NOTHING
end
end
Run Code Online (Sandbox Code Playgroud)
调节器
def create
if params[:title].nil?
params[:title] == 'Unnamed'
@post = Post.create(post_params)
else
@post = Post.create(post_params)
end
end
Run Code Online (Sandbox Code Playgroud)
但它不起作用.什么想法我做错了什么?提前致谢.
我有FamilyTree, Node, Comment, & User模特.
这种关系是这样的:
FamilyTree
class FamilyTree < ActiveRecord::Base
belongs_to :user
has_many :memberships, dependent: :destroy
has_many :members, through: :memberships, source: :user, dependent: :destroy
has_many :nodes, dependent: :destroy
end
Run Code Online (Sandbox Code Playgroud)
Node
class Node < ActiveRecord::Base
belongs_to :family_tree
belongs_to :user
has_many :comments, dependent: :destroy
end
Run Code Online (Sandbox Code Playgroud)
Comment
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :node
end
Run Code Online (Sandbox Code Playgroud)
User
class User < ActiveRecord::Base
has_one :family_tree, dependent: :destroy
has_many :memberships, dependent: :destroy
has_many :nodes, dependent: :destroy
has_many :comments
end
Run Code Online (Sandbox Code Playgroud)
Membership ## This is …
Rails中有一种方法可以在提交表单时发送自动电子邮件,其中包含该表单中的信息吗?具体来说,我希望新用户在提交注册表单时收到包含他的新个人资料网址的电子邮件.
这是我的表单的样子:
<h1>Add Something!</h1>
<p>
<%= form_for @thing, :url => things_path, :html => { :multipart => true } do |f| %>
<%= f.text_field :name, :placeholder => "Name of the thing" %>
<%= f.label :display_picture %>
<%= f.file_field :avatar %>
<br>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</p>
Run Code Online (Sandbox Code Playgroud)
控制器:
class ThingsController < ApplicationController
def show
@thing = Thing.find(params[:id])
end
def new
@thing = Thing.new
@things = Thing.all
end
def create
@thing = Thing.new(thing_params)
if @thing.save
render :action …Run Code Online (Sandbox Code Playgroud) 所以我有一份时事通讯,但是我无法想出一种方法来跟踪已发送的报价.我想跟踪发送的报价以避免发送重复项.
应用程序非常简单,有一个属于类别模型的引用模型,以及属于类别模型的订阅者模型.类别has_many订阅者和引号.
下面的代码将第一个Quote发送给其订阅者.当然,您必须维护一个表或字段,以便跟踪之前发送给订户的报价,以便您始终可以设置每天发送新报价.
Category.all.each do |category|
category.subscribers.each do |subscriber|
SubscriptionMailer.sendmyemail(subscriber.email, category, category.quotes.first.body, category.subscribers).deliver
end
end
Run Code Online (Sandbox Code Playgroud)
这可以进一步更新为:
Category.all.each do |category|
category.subscribers.each do |subscriber|
SubscriptionMailer.sendmyemail(subscriber.email, category, subscriber.choose_quote(category), category.subscribers).deliver
end
end
Run Code Online (Sandbox Code Playgroud)
在我的subscriber.rb模型中,我准备好了这个方法,但不确定用什么代码来填充它以实现我想要的.如何每天向订阅者发送一个独特的报价,而不是过去发送的报价的副本?或者甚至更简单的方法是随机化订单,这可能吗?
def choose_quote(cat)
# send quote which is unique
# Meaning which was not sent
end
Run Code Online (Sandbox Code Playgroud) 我希望做到以下几点:
var = 'bank_id'
id = '100'
Bank.where(var: id) or
Bank.where("? = ?", var, id)
Run Code Online (Sandbox Code Playgroud)
银行是一种模式.bank_id是模型的一个属性,它将其作为字符串.
RoR中的类是否在创建该类的新对象时自动启动第一个方法?
class User
attr_accessor :name, :email
def initialize(attributes = {})
@name = attributes[:name]
@email = attributes[:email]
end
def formatted_email
"#{@name} <#{@email}>"
end
end
Run Code Online (Sandbox Code Playgroud)
假设我像这样创建一个新用户
connor = User.new(name: "Connor B", email: "CB@example.com")
Run Code Online (Sandbox Code Playgroud)
它如何知道自动启动第一个方法,但第二个方法只在被调用时才有效?
ruby-on-rails-4 ×10
ruby ×7
routes ×2
actionmailer ×1
automation ×1
class ×1
constraints ×1
controller ×1
email ×1
post ×1
subdomain ×1