validates :name, uniqueness: true
以上验证name了区分大小写的唯一性。存在任何其他默认验证器/选项,以包括不区分大小写的检查。
请帮忙。提前致谢。
我正在尝试$bundle install将obfuscate_idgem放入我的Rails 4应用程序中,但是我遇到了一个错误:

它说它依赖于Rails> 3.2.1,但我有Rails 4.0.2.假设这应该有效,我错了吗?我错过了什么吗?
宝石:https://github.com/namick/obfuscate_id
谢谢,迈克尔.
我正在使用带有braintree沙箱的braintree gem并尝试设置web钩子.我有以下代码:
class PaymentsController < ApplicationController
def webhooks
challenge = request.params["bt_challenge"]
challenge_response = Braintree::WebhookNotification.verify(challenge)
return [200, challenge_response]
end
end
Run Code Online (Sandbox Code Playgroud)
但出于某种原因,当我在braintree的网站上按"创建网络钩子"时,我被告知:
Destination could not be verified.
Run Code Online (Sandbox Code Playgroud)
我检查了服务器日志并且它正在接收请求,但是由于某种原因返回http 500并说出以下内容:
2014-04-09T23:39:19.937280+00:00 app[web.1]: Completed 500 Internal Server Error in 71ms
2014-04-09T23:39:19.941486+00:00 app[web.1]: ActionView::MissingTemplate (Missing template payments/webhooks, application/webhooks with {:locale=>[:en], :formats=>[:xml, :html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :arb, :jbuilder, :haml]}. Searched in:
Run Code Online (Sandbox Code Playgroud)
这表明控制器正在寻找视图而无法找到它.我不知道该怎么做.Web挂钩操作中的代码直接来自braintree的网站:https://www.braintreepayments.com/docs/ruby/guide/webhook_notifications …
我有属于类别的子类别.我的应用程序在子类别下保存(构建)我的所有子类别,id = 1,即使我的代码似乎没问题,也不应该这样做:
子类别控制器:
def create
@category = Category.find_by(params[:id])
@subcategory = @category.subcategories.build(subcategory_params)
if @subcategory.save
flash[:success] = "added subcategory"
redirect_to admin_categories_url
else
render :new
end
end
...
private
def subcategory_params
params.require(:subcategory).permit(:name, :category_id)
end
Run Code Online (Sandbox Code Playgroud)
Subcategory.rb
class Subcategory < ActiveRecord::Base
belongs_to :category
has_many :products
validates :name, presence: true
validates :category_id, presence: true
end
Run Code Online (Sandbox Code Playgroud)
形成:
<h3>Add a subcategory</h3>
<%= form_for [@category, @subcategory] do |f| %>
<%= f.text_field :name, placeholder: "Name" %>
<%= f.submit "Add a subcategory" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
路由器:
resources :categories do …Run Code Online (Sandbox Code Playgroud) 我正在使用标准设置部署rails4应用程序,但获得:
此应用程序是Rails 4应用程序,但它被错误地检测为Rails 1或Rails 2应用程序.这可能是Phusion Passenger中的一个错误,所以请报告.
我已经创建了一个脚手架控制器,我可以在其中删除记录,更新记录并创建新记录.所有方法工作正常但更新方法不起作用.当我更新任何记录然后提交它只是告诉我闪存通知记录更新成功,但当我检查我的数据库什么都没有改变.在我的控制器下面我的更新方法.请告诉我我做错了什么.
def update
respond_to do |format|
if @member = Member.find(params[:id])
format.html { redirect_to @member, notice: 'Member was successfully updated.' }
format.js { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @member.errors, status: :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud) 我必须得到像这样的数组
[{"social"=>"linkedin", "count"=>1},
{"social"=>"twitter", "count"=>1},
{"social"=>"facebook", "count"=>0}]
Run Code Online (Sandbox Code Playgroud)
从这个数组
=> [{"twitter"=>1, "linkedin"=>1, "facebook"=>0}]
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?
我在我的Rails 4应用程序中有这个区域设置切换器:
class LocalesController < ApplicationController
def change_locale
if params[:set_locale]
session[:locale] = params[:set_locale]
redirect_to(:back, :locale => params[:set_locale])
end
end
end
Run Code Online (Sandbox Code Playgroud)
一切正常,但用户切换语言环境时,新的语言环境不会显示在URL中.
例如,当我在页面上/de/faq然后单击English下拉菜单时,页面的内容将切换为英语(我猜会话也会调整为英语),但URL仍然存在/de/faq.只有在此之后的下一次单击才能使URL中的区域设置调整为/en/....
但是,如果区域设置更改将立即反映在URL中,那将是很好的.
如何才能做到这一点?
我想解决这个Rails 4弃用警告:
关系#all已弃用.如果你想加载一个关系,你可以调用#load(例如Post.where(published:true).load).如果要从关系中获取记录数组,可以调用#to_a(例如Post.where(已发布:true).to_a).
因此,当我将Model.all转换为Model.to_a时,它会抛出未定义的方法to_a.
当我尝试使用Model.scoped.to_a时,它就像在where和scope链中一样.
我应该通过范围还是有更好的解决方案?
我的application_helper.rb文件包含以下代码
module ApplicationHelper
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
Run Code Online (Sandbox Code Playgroud)
我有一个有一个方法的帖子控制器
def create
@post = Post.new(params[:post])
@post.posted_by_uid=current_user.id
@post.posted_by=current_user.first_name+" "+current_user.last_name
@post.ups=0
@post.downs=0
@post.save
respond_to do |format|
if @post.save
format.html {redirect_to root_path}
format.json {render :json => @post, :status => :created, :location => @post}
else
format.html {redirect_to root_path}
format.json {render :json => @post.errors, :status => :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试通过以下视图提交表单时
<%= form_for(@post) do |f| %>
<div class="fields">
<%= f.label "Post" %><br />
<%= f.text_field :post %>
</div>
<div class="actions" id="refreshposts"> …Run Code Online (Sandbox Code Playgroud) ruby-on-rails-4 ×10
ruby ×5
arrays ×1
braintree ×1
gem ×1
localization ×1
nginx ×1
passenger ×1
view-helpers ×1
webhooks ×1