Tag: ruby on rails 4

如何为控制器方法运行minitest?

post_controller文件 class PostsController < ActionController::Base before_action :authenticate_user! def index @post = current_user.posts.paginate(page: params[:page], per_page: 5) respond_to do |format| format.html format.json { render json: @post } end end def new @post = Post.new end def create @post = current_user.posts.build(post_param) if @post.save redirect_to action: 'index' else render 'new' end post_controller_test require ‘test_helper’ class PostsControllerTest < ActionController::TestCase include […]

很多:`require’:无法加载这样的文件 – 升级ruby / rails后的“gem_name”(LoadError)

在我刚刚从4.1升级rails并将ruby从2.0升级到2.1之后,当我尝试启动rails时,我遇到了几个加载问题。 我的很多gem都抛出这样的错误: /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require’: cannot load such file — jwt (LoadError) from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/oauth2-1.0.0/lib/oauth2/strategy/assertion.rb:1:in `’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/oauth2-1.0.0/lib/oauth2.rb:8:in `’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency’ from /Users/nk/.rvm/gems/ruby-2.1.3@au_rails4/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in […]

列出重复值的数量

我有以下一个Rails ActiveRecord协会,其中一个课程有很多书,书籍属于一个课程。 我想运行一个SQL命令,它会告诉我有多少课程有多本属于该课程的书籍。 例如:第1课有第1册和第2册,我想知道发生了多少次。

Rails 4 – acts_as_votable,更改默认排序顺序

我有一个Rails应用程序,允许在某些模型上投票。 在我的一个模型中,我想更改default_scope以便首先显示投票最多的对象。 我在我的模型文件中尝试了以下内容: def self.default_scope order(‘votes_for.size DESC’) end 这给了我以下错误: SQLite3::SQLException: no such column: votes_for.size: SELECT “solutions”.* FROM “solutions” WHERE “solutions”.”competition_id” = ? ORDER BY votes_for.size DESC 我想知道是否有一种方法可以更改默认的默认排序顺序,我正在做的事情显然不起作用。 如果可能的话,使它在控制器级别(非默认顺序)工作的解决方案也会很好。

如何在rails 4中使用params.require

我有一个这样的私人方法用于注册表单,其中包含四个字段, firstname , email , password和confirm password 。 我不知道如何检查password confirmation 。 def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end Previosuly,我使​​用下面的代码。如何转换下面的代码使用params.require User.new(name: params[:name], email: params[:email], password: params[:password], confirmpassword: params[:password])

评论多个模型

在我的rails应用程序中,我目前有评论设置可以使用我的post模型,它正常运行。 如何在我的图书模型中添加评论? 这是我到目前为止: 以下是我的架构中的评论内容: create_table “comments”, force: true do |t| t.text “body” t.datetime “created_at” t.datetime “updated_at” t.integer “user_id” t.integer “post_id” t.integer “book_id” end 在我的用户模型中: class User < ActiveRecord::Base has_many :comments acts_as_voter end 在我的post模型中: class Post < ActiveRecord::Base has_many :comments end 在我的书模型中: class Book < ActiveRecord::Base has_many :comments end 在我的评论模型中: class Comment < ActiveRecord::Base belongs_to :post […]

Rails,respond_to块和|格式|

Rails scaffold生成以下内容: respond_to do |format| if @student.save format.html { redirect_to @student, notice => ‘Student was successfully created.’ } format.json { render :show, status: :created, location: @student } else format.html { render :new } format.json { render json: @student.errors, status: :unprocessable_entity } end end 阅读本文之后,我理解了respond_to是如何工作的(有点),但我不知道format正在做什么。 不应该是 format.json或format.json而不是两者兼而有之 ? 这两条线实际上在做什么? format.html { render :new } format.json { […]

RSpec不加载我的类,即使它在Rails控制台中加载

我在app/models/parser有一个名为data.rb ,内容如下: class Parser::Data def method1 end end 在这一点上没什么好看的。 我试图在实现太多之前为它编写测试,只是为Rails做了默认的RSpec安装。 我的RSpec文件在spec/models/parser/data_spec.rb ,到目前为止非常基础: require ‘spec_helper.rb’ describe Parser::Data do let(:parser) { Parser::Data.new } end 当我运行测试时,我收到此错误: spec/models/parser/data_spec.rb:3:in `’: uninitialized constant Parser (NameError) 我尝试将module Parser放在Data类的同一目录app/models/parser ,我也尝试将它移动到lib/parser做同样的模块包装类,并在application.rb添加了lib/parser到autoload但是到目前为止没有任何工作。 我究竟做错了什么?

关联模型和嵌套表单,validation不起作用

Update2:我已经清理了代码,这似乎已经解决了一些问题。 我在这里发布了新代码作为一个新问题。 更新:组织和用户有1:多关系。 我的问题涉及一个联合注册表单,其中需要组织和用户。 在maxcal对原始post的帮助之后,我为我的嵌套表单编写了一个新的create方法(“组织有很多用户”),如下所示。 我还添加了begin…rescue…end create方法。 现在的情况/问题: 提交所有有效信息,它正常工作。 提交组织的无效信息(如果用户也无效,则无关紧要),它会按照我们的意愿呈现包含错误消息的页面,但它仅显示组织详细信息的错误。 此外,对于用户详细信息,它已经清空了所有字段,它不应该。 仅为用户提交无效信息,它再次呈现表单但没有任何错误消息,并且用户的所有字段都已清空。 任何人都知道代码有什么问题? 对于嵌套用户而言,问题似乎与组织(父组件)相关。 users_attributes.empty? 不起作用,因为根据日志,空提交的表单仍然包含这些属性: Parameters: {“utf8″=>”✓”, “authenticity_token”=>”***”, “organization”=>{“name”=>””, “bag”=>””, “users_attributes”=>{“0″=>{“email”=>””, “username”=>””, “password”=>”[FILTERED]”, “password_confirmation”=>”[FILTERED]”, “usertype”=>”2”, “admin”=>”true”}}}, “commit”=>”Register”} 。 def create @organization = Organization.new(new_params.except(:users_attributes)) begin if users_attributes.empty? @organisation.errors.add(:users, ‘No user provided’) end @organization.transaction do @organization.save! if users_attributes.any? @organization.users.create!(users_attributes) end end rescue ActiveRecord::RecordInvalid => invalid if […]

如何在Rails中创建嵌套表单4

我试图通过各自的连接表(exercise_equipment,exercise_muscles)让用户在多对多关系中创建设备和肌肉练习。 我已经得到了每个练习添加一个设备/肌肉的表单,但无法弄清楚如何添加链接以便在动态添加另一个字段到表单。 我已经检查过RailsCasts , 这篇文章 ,已经将它作为我自己以前post的一个侧面问题,但根本无法使这个function起作用。 我是Rails 4的新手,我还在尝试学习Javascript,但是我想深入了解如何设置Rails 4方式! 我的模特: # id :integer # name :string # is_public :boolean Exercise has_many :exercise_equipment has_many :equipment, :through => :exercise_equipment accepts_nested_attributes_for :exercise_equipment # id :integer # exercise_id :integer # equipment_id :integer # optional :boolean ExerciseEquipment belongs_to :exercise belongs_to :equipment accepts_nested_attributes_for :equipment # id :integer # name :string Equipment […]