小编Pra*_* KJ的帖子

Rails通过ajax提交表单并更新视图

我想通过ajax更新表单提交而不重新加载页面并根据它更新视图.我尝试了不同的方法,并失败了作为新的rails.

这是情况.

在模态中

def new
    @new_entry = current_user.notes.build
    @words = current_user.notes
    @notes_false_list = current_user.notes.where(known: false).order("title").group_by { |note| note.title[0] }
    @notes_true_list = current_user.notes.where(known: true).order("title").group_by { |note| note.title[0] }
  end
Run Code Online (Sandbox Code Playgroud)

在视图中的表单代码.

<%= form_for @new_entry, :html => {:class => 'home-form without-entry clearfix'}  do |f| %>
      <%= f.text_field :title, placeholder: 'Squirrel', autofocus: true, class: 'title-field pull-left' %>
      <%= f.submit '', class: 'btn home-add without-entry' %>
  <% end %>
Run Code Online (Sandbox Code Playgroud)

创建动作

def create
    @new_note = current_user.notes.build(new_note_params)
    if @new_note.save
      @notes_list = current_user.notes.count
      unless @new_note.user.any_entry
        @new_note.user.toggle!(:any_entry) if @notes_list …
Run Code Online (Sandbox Code Playgroud)

javascript forms ajax jquery ruby-on-rails

8
推荐指数
1
解决办法
2万
查看次数

跳过依赖于: :destroy 的验证

我有一个用户和一个公司模型,如下所示。公司模式已经得到了一些验证。

用户.rb

    after_create :create_tables!

    def create_tables!
      companies.create!(handle: "random_handle")
   end
Run Code Online (Sandbox Code Playgroud)

公司.rb

before_destroy :check_for_presence_of_a_company!

def check_for_presence_of_a_company!
  if user.companies.count <= 1
    errors.add(:base, 'You cannot delete all of your companies.')
    throw(:abort)
  end
end
Run Code Online (Sandbox Code Playgroud)

当用户第一次创建帐户时,我在用户模型中使用after_create为他创建一个公司,在删除公司之前,他应该至少有一个公司。

但问题是当用户尝试删除他的帐户时,上述公司验证会抛出错误。

当用户删除其帐户时,应忽略上述验证company.rb。我怎样才能做到这一点?谢谢。

编辑

user.rb中我更新了

has_many :companies,dependent: :destroy
Run Code Online (Sandbox Code Playgroud)

has_many :companies,dependent: :delete_all
Run Code Online (Sandbox Code Playgroud)

但是company.rb

has_many :categories, dependent: :destroy
Run Code Online (Sandbox Code Playgroud)

并显示违反外键约束错误。将其从 destroy 更新为 delete_all 也不起作用。

ruby activerecord ruby-on-rails

3
推荐指数
1
解决办法
4217
查看次数

测试模型的Rails说'预期错误是真的'

我有一个网站,用户可以在注册时创建产品,产品可以有清单.

我有一个清单表,其中包含以下列: -

id  |  product_id  | content  |  archived 
Run Code Online (Sandbox Code Playgroud)

我是铁轨测试的新手.我在下面写了测试

  test 'should have content' do
    checklist = Checklist.new
    assert checklist.save
  end
Run Code Online (Sandbox Code Playgroud)

并使用以下命令运行测试

ruby -I test test/models/checklist_test.rb
Run Code Online (Sandbox Code Playgroud)

并且测试失败,预期的错误是真正的 错误.

是否因为问题可以使用user.product.checklists访问清单我必须首先填充灯具中的数据并调用那些测试中的数据?

编辑1

我在清单模型中没有任何验证.

class Checklist < ApplicationRecord belongs_to :product end

我加了!在测试中保存如下

  test 'should have content' do
    checklist = Checklist.new
    assert checklist.save!
  end
Run Code Online (Sandbox Code Playgroud)

并得到了这个错误

ActiveRecord :: RecordInvalid:验证失败:产品必须存在

因为表中有product_id.我不知道如何向rails测试提供数据.有帮助吗?

编辑2

编辑后如下所示消除错误.

class Checklist < ApplicationRecord belongs_to :product, optional: true end

但是我想用product现在测试模型.我不知道如何使用灯具为测试提供数据,好像没有我可以Checklist.new在测试中使用的外键.

既然它有外键我怎么能提供Checklist属于 …

ruby testing ruby-on-rails

1
推荐指数
1
解决办法
2300
查看次数

未初始化的常量控制器名称

routes.rb,我用过resources nicknames.在nickname_controller,我做了:

def index
    @nick_name = current_user.nicknames.build
    @nick_names = current_user.nicknames.all
  end

def create
    @nick_name = current_user.nicknames.build(nn_create_param)
    if @nick_name.save
      flash[:success]= 'Name created'
      redirect_to nickname_path
    else
      flash[:danger]= "Name can't be created"
      redirect_to nickname_path
    end
  end
Run Code Online (Sandbox Code Playgroud)

在索引视图文件中

<%= form_for @nick_name do |f| %>
    <%= f.text_field :nickname %>
    <%= f.submit 'Submit' %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

当我提交表格时,它说uninitialized constant NicknamesController.

谁能告诉我问题出在哪里?

ruby model-view-controller controller ruby-on-rails

-1
推荐指数
1
解决办法
9931
查看次数