这是我的User
模特:
class User < ActiveRecord::Base
rolify
devise :database_authenticatable, :registerable, :token_authenticatable, :confirmable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable,
:email_regexp => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :confirmed_at, :confirmation_token
validates_uniqueness_of :email, :case_sensitive => false
validates_confirmation_of :password
end
Run Code Online (Sandbox Code Playgroud)
的Gemfile:
gem 'client_side_validations'
形成:
<%= form_for(resource, :as => resource_name, :class => "send-with-ajax", :url => user_registration_path(resource), :validate => true, :html => { :id => 'user_new' }) do |f| %>
<%= f.text_field :email,:placeholder => "your-email@address.com", :input_html => {:autofocus => true}, :validate => true %>
<%= …
Run Code Online (Sandbox Code Playgroud) 所以,我有3种型号:category
,product
,category_products
。
这是我的 category.rb
attr_accessible :name
has_many :category_products do
def with_products
includes(:product)
end
end
has_many :products, :through => :category_products
Run Code Online (Sandbox Code Playgroud)
这是我的 product.rb
attr_accessible :name, :description, :price, :vendor_id, :image, :category_ids
belongs_to :vendor
has_many :category_products do
def with_categories
includes(:category)
end
end
has_many :categories, :through => :category_products
Run Code Online (Sandbox Code Playgroud)
这是我的 category_product.rb
attr_accessible :product_id, :category_id, :purchases_count
belongs_to :product
belongs_to :category
validates_uniqueness_of :product_id, :scope => :category_id
Run Code Online (Sandbox Code Playgroud)
这是我的 routes.rb
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
resources :categories
resources :vendors do
resources :products
end …
Run Code Online (Sandbox Code Playgroud) 我正在关注导入CSV Railscast,它很直接.
问题是它只处理一个csv文件,该文件只包含1个文件中1个模型的信息.
说,我有一个CSV文件,我试图导入我的Listing
模型.在每行/列表上,它有一个名为列的列Building
,其中值实际上是该列表的构建属性的名称(即@listing.building.name
).
如何在导入中处理这些情况?
这是Ryan在Railscast中获得的壁橱,Product
在他的案例中就是模型:
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
product = find_by_id(row["id"]) || new
product.attributes = row.to_hash.slice(*accessible_attributes)
product.save!
end
end
Run Code Online (Sandbox Code Playgroud)
所有发生的事情是他正在检查产品是否存在以及是否存在然后更新属性.如果没有,则创建一个新的.
在这种情况下不太确定如何处理关联...特别是考虑到需要发生的事情是在不存在关联记录的情况下,需要在此过程中创建它.
所以回到我building.name
之前的例子,如果没有Building.find_by_name(name)
,那么它应该创建一个新的建筑记录.
思考?
我想使用simple_form生成一个取消按钮....但不太确定如何做到这一点.
<%= f.button :submit, :class => "btn btn-warning btn-small", :label => "Save Changes" %>
<%= f.button :cancel, :class => "btn btn-inverse btn-small", :label => "Cancel" %>
Run Code Online (Sandbox Code Playgroud)
但取消按钮不起作用.
我怎么做到的?
我想要发生的是,一旦用户点击确认链接 - 并且他们的帐户已成功确认 - 如果他们分配了特定角色(已成功分配给他们的after_create
回调,则应将他们重定向到某个指定路径)User
模型).
我创建了一个RegistrationsController
:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
if resource.has_role? :seller
new_item_path
else
root_path
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是,这总是重定向到root_path
....尽管我已经证实,用户确实有这个角色.
编辑1
似乎确认请求永远不会发送到RegistrationsController
:
Started GET "/users/confirmation?confirmation_token=KRwZ7MChtxxq4sxxkDLq" for 127.0.0.1 at 2013-05-07 03:52:56 -0500
Processing by Devise::ConfirmationsController#show as HTML
Parameters: {"confirmation_token"=>"KRwZ7MChtxxq4sxxkDLq"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'KRwZ7MChtxxq4sxxkDLq' LIMIT 1
(0.1ms) BEGIN
(0.5ms) UPDATE "users" SET "confirmation_token" = NULL, "confirmed_at" = '2013-05-07 08:52:56.846852', "updated_at" = '2013-05-07 …
Run Code Online (Sandbox Code Playgroud) 我有一个我一直在开发的应用程序,它曾经用于直到最近的一些更改.
我运行了一个包更新,并更新了一堆宝石.从那以后(或那段时间)我的结账过程不起作用,我无法弄清楚导致问题的原因.
我的development.log文件中没有任何错误,我的JS控制台没有错误.
对于登录凭据,请使用:abc@test.com/testing123(其中电子邮件是用户,后者是pw).
将该项目添加到购物车,然后完成结帐流程.
输入一些虚假凭证,并使用信用卡号码:4111111111111111(验证号码可以是任何3位数字,在今天之后的任何日期到期).
点击结帐后,您会看到表单未提交.
对于我的生活,我无法弄清楚为什么.
我很乐意听到一些关于如何进一步排除故障或者告诉我是什么导致这种情况的想法.
谢谢.
PS这不是Heroku问题 - 我在本地也有这个问题.我正在使用piggybak购物车gem,并且gem维护者无法在他们的结尾复制此错误.所以我进一步陷入困境:(
我正在使用public_activity和我的一个部分我有这个电话:
<%= link_to "#{activity.trackable.user.name} " + "commented on " + "#{activity.trackable.node.name}", node_path(activity.trackable.node.family_tree_id,activity.trackable.node) %>
Run Code Online (Sandbox Code Playgroud)
这是通过这个电话执行的:
<% @unread_act.each do |friend| %>
<li><%= render_activity friend %> </li>
<% end %>
Run Code Online (Sandbox Code Playgroud)
那个实例变量在我ApplicationController
这里分配:
before_filter :handle_nofications
def handle_nofications
if current_user.present?
@unread_act = Notification.where(owner_id: current_user).includes(:trackable => :user).unread_by(current_user)
end
end
Run Code Online (Sandbox Code Playgroud)
我正在使用gem bullet,找到N + 1个查询,我得到的反馈是这样的:
N+1 Query detected
Comment => [:node]
Add to your finder: :include => [:node]
N+1 Query method call stack
Run Code Online (Sandbox Code Playgroud)
我最初得到了该消息:trackable
,并为:user
.这两个我现在都渴望通过includes
该赋值语句加载.
但是,我不知道如何在热切的加载中达到3级 - 而不仅仅是两级.
鉴于节点被调用activity.trackable.node.name …
ruby-on-rails query-optimization eager-loading ruby-on-rails-4 public-activity
所以我有一个有形式的模态.当在该模态上按下"提交"按钮时,我想要执行另一个模态.我怎么做?
这是我的第一个模式 - views/shared/_upload_video_popup.html.erb
:
<div id="overlay"> </div>
<div class="popup" id="add-video-step-1">
<div class="titles clearfix">
<h2>Upload a Video</h2>
<p><i>Step 1 of 2 - TEST</i></p>
</div>
<div class="content">
<% if @family_tree %>
<%= simple_form_for([@family_tree, @video], :remote => true) do |f| %>
<div class="column">
<div class="f-row">
<%= f.input :title, label: "Title:" %>
</div>
<div class="f-row">
<%= f.input :description,label: "Description:" %>
</div>
<div class="f-row">
<%= f.input :circa, as: :datepicker, start_year: Date.today.year - 5, label: "Circa:" %>
</div>
<div class="f-row">
<label for="family">Family in this video:</label>
<%= …
Run Code Online (Sandbox Code Playgroud) javascript jquery ruby-on-rails twitter-bootstrap ruby-on-rails-4
所以我的表单部分加载到我的div id="secondary"
,在第一页加载时隐藏.
当用户点击带有被调用类的按钮时toggleSidebar
,_form.html.erb
会显示该按钮.
update
当用户没有像这样登录时,我已覆盖部分以显示新表单(即使被按下):
<%= simple_form_for(Post.new, html: {class: 'form-horizontal' }) do |f| %>
Run Code Online (Sandbox Code Playgroud)
与看起来像这样的常规版本相反,并且包含在if
同一部分的语句中:
<% if current_user and current_user.has_any_role? :editor, :admin %>
<%= simple_form_for(@post, html: {class: 'form-horizontal' }) do |f| %>
Run Code Online (Sandbox Code Playgroud)
真正的问题是在我看来,当有人去的时候Update
,这就是当用户注销时会发生的事情:
<%= link_to "Update", "#", class: "togglesidebar" %>
Run Code Online (Sandbox Code Playgroud)
这是完美的,它执行CSS并完美地显示空表单部分.
但是,当用户登录时,我希望它parent_id: @post
在执行侧边栏切换时发送参数.
这是它在普通new_post_path
视图中的外观(即非侧边栏新帖子视图):
<% if current_user %>
<%= link_to "Update", new_post_path(parent_id: @post) %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这就是我的PostController#New
样子:
def new
@post = Post.new(parent_id: params[:parent_id]) …
Run Code Online (Sandbox Code Playgroud) 我对如何正确配置CanCanCan感到有点困惑.
对于初学者,我是否必须添加load_and_authorize_resource
到我想限制访问的每个控制器资源?
这就是我想做的事情:
这就是我的ability.rb
样子:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
#Admin
if user.has_role? :admin
can :manage, :all
can :manage, :newsroom
# Editor
elsif user.has_role? :editor
can :read, :all
can :manage, :newsroom
can :manage, Post
#Member
elsif user.has_role? :member
can :read, :all
can :create, Post
can :status, Post
can :update, Post do |post|
post.try(:user) == user
end
#Guest
else
can :read, :all
can :create, …
Run Code Online (Sandbox Code Playgroud) devise ×2
javascript ×2
cancan ×1
cancancan ×1
csv ×1
heroku ×1
import ×1
jquery ×1
rails-admin ×1
simple-form ×1