Tag: ruby on rails 4

更新记录栏4没有路由匹配“/ admin / usersupdate”

我有这个控制器 def usersedit @user = User.find_by id: params[:id] end def usersupdate @user = User.find_by id: params[:id] if @user.update(post_params) redirect_to action: :users else render ‘usersedit’ end end 这个表单在usersedit.html.erb的视图中 但是当我提交表单时,会出现此错误 No route matches [PATCH] “/admin/usersupdate” 我的路线代码是: Prefix Verb URI Pattern Controller#Action root GET / pages#home admin_index GET /admin/index(.:format) admin#index admin_grades GET /admin/grades(.:format) admin#grades pages_home GET /pages/home(.:format) pages#home […]

Rspec测试失败,当用户被授权访问他拥有的交易时,用户被重定向

在我的应用程序中,感谢Cancan,我可以让客户访问他自己的交易。 当我使用浏览器“手动”尝试它时,它可以正常工作, 但我在实现rspec测试时失败了 。 客户无法访问其他客户的交易,只能访问自己的交易(管理员通过Active Admin界面授予他访问权限)。 这就像我没有让rspec明白我为测试创建的客户(通过FactoryGirl)应该被允许/与我为测试创建的交易相关联(再次通过FactoryGirl)。 测试:/spec/controllers/deals_controller_spec.rb require ‘spec_helper’ require “cancan/matchers” describe DealsController do context “As signed-in CUSTOMER” do before do @customer = FactoryGirl.create(:customer) #the factory builds a basic customer ie with ‘prospect role’ attributed by default @deal = FactoryGirl.create(:deal, :customers => [@customer]) # in array as a deal has_many customers sign_in_customer @customer end […]

Rails路由:向root添加(浅)关注

在我的routes.rb中,我定义了如下问题: concern :namespaceable do resources :comments do resources :replies … 现在,我可以将这个问题添加到任何资源: resources :groups, concerns: :namespaceable, shallow: true 但是我怎样才能将这个问题浅浅地添加到根目录中,以便我可以拥有以下路由 /评论 /评论/ {} COMMENT_ID /评论/ {} COMMENT_ID /回复 /回复/ {reply_id} 非常感谢!

如何在保存新记录之前检查数据库状态

是否有一种更为ruby的惯用方法来处理检查数据库是否有5条记录,然后使用if语句,是否应该在filter或任何类型的validation中完成? saved_count = Model.where(is_active: true).count if saved_count == MAX_SAVED return {error: ‘Cannot save more than 5 records’} end

rails中的更新路由对after_action反应不佳?

class FrogsController < ApplicationController before_action :find_frog, only: [:edit, :update, :show, :destroy] after_action :redirect_home, only: [:update, :create, :destroy] def index @frogs = Frog.all end def new @ponds = Pond.all @frog = Frog.new end def create @frog = Frog.create(frog_params) end def edit @ponds = Pond.all end def update @frog.update_attributes(frog_params) end def show end def destroy @frog.destroy end […]

Rails,Devise – 管理员试图编辑另一个用户配置文件,而不是加载自己的配置文件

在使用Rails进行开发时相当新,我正试图解决以下问题:我正在使用Devise + Cancan + olify应用程序来尝试创建身份validation和用户管理。 我已经对一般用户行为进行了排序,并且我正在努力实现管理员用户能够编辑另一个用户配置文件。 目前,管理员用户可以列出用户并尝试编辑其他用户的个人资料。 但是,当进入编辑页面时,尽管URL /路由对我来说是正确的,我仍然会在表单中看到“currentuser”/ admin信息。 简而言之,场景:UserId1是管理员UserId1正在尝试编辑UserId2的配置文件 记录为UserId1,以下路由为UserId1而不是UserId2带来详细信息: http://localhost:3000/d/users/edit.2 这是routes.rb: devise_for :users, :path_prefix => ‘d’, :controllers => { :registrations => ‘registrations’ } namespace :admin do get ”, to: ‘dashboard#index’, as: ‘/’ resources :users end 这是用户#index视图: { :confirm => “Are you sure?” }, :method => :delete %> 这是users_controller.rb: class Admin::UsersController < ApplicationController […]

rails 4 collection_select多个嵌套属性未正确保存

我是rails的新手,我在使用collection_select保存连接表的嵌套属性时遇到问题。 我有模型post,tag和post_tagging。 post_tagging是一个连接表。 我想为每个post设置多个标签,所以我尝试通过collection_select使用多选,但是当我保存时,只将post_id插入到数据库中。 下面是我的代码和日志。 Post.rb class Post < ActiveRecord::Base has_many :post_taggings, foreign_key: :post_id, dependent: :destroy has_many :tags, through: :post_taggings, source: :tag accepts_nested_attributes_for :post_taggings, reject_if: :all_blank, allow_destroy: true end Tag.rb class Tag < ActiveRecord::Base has_many :post_taggings, foreign_key: :tag_id, dependent: :destroy has_many :posts, through: :post_taggings, source: :post end post_tagging.rb(我在post_tagging模型中关闭了tag_id和post_id的状态validation,因此我可以获得POST的日志。) class PostTagging < ActiveRecord::Base belongs_to :post belongs_to […]

无法在postgresql(rails)中的json字段中存储数组,无法将数组转换为json

这是我运行db:migrate时遇到的错误 rake aborted! can’t cast Array to json 这是我的桌子 class CreateTrips < ActiveRecord::Migration def change create_table :trips do |t| t.json :flights t.timestamps end end end 这是在我的seeds.rb文件中 flights = [{ depart_time_hour: 600, arrive_time_hour: 700, passengers: [ { user_id: 1, request: true } ] }] trip = Trip.create( { name: ‘Flight’, flights: flights.to_json } ) 出于某种原因,我不能这样做。 如果我这样做 […]

accept_nested_attributes_for:allow_destroy,:_ destroy无效

我有一个Rails 4.1应用程序,它使用了一些值得注意的技术。 简单的forms,茧 我无法销毁嵌套属性的记录。 基于一些冗长的研究,我相信我的代码是正确的,但我可能会遗漏一些愚蠢的东西。 模型 has_many :staff_services_joins, :dependent => :destroy has_many :services, :through => :staff_services_joins accepts_nested_attributes_for :staff_services_joins, :allow_destroy => true 这有点不正统,但我在连接模型上有两个组件,它们不是外键,需要设置。 这就是为什么我接受连接模型的嵌套属性而不是服务模型。 控制器方法 def update ap staff_params # if @staff_member.update_with_account staff_params, params[:password] if @staff_member.update_attributes staff_params flash[:notice] = ‘Successfully updated staff member! :)’ redirect_to vendor_store_staff_path current_store, @staff_member else flash[:error] = ‘Failed to update staff member […]

Fresh rails app默认为Postgres而不是SQLite3

我刚刚在这台机器上安装了rbenv,ruby 2.2.3和rails 4.2.4。 我已经启动了我的rails应用程序而没有更改任何代码,只使用rails new .的默认生成文档rails new . 然后我用rails server启动了rails server 。 当命中http://localhost:3000我收到以下错误: “为数据库适配器指定’postgresql’,但未加载gem ‘pg’ 。将gem’pg gem ‘pg’添加到您的Gemfile中(并确保其版本达到ActiveRecord所需的最小值)。” 我从之前的Node项目中安装了postgres,但是我的database.yml仍然按照你对新应用程序的期望进行读取: # SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem ‘sqlite3’ # default: &default adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default database: […]