Tag: ruby on rails 4

Ruby on Rails实时搜索(过滤)

我正在关注railscasts rails ajax教程并陷入困境。 实时搜索不起作用,我必须单击搜索按钮才能获得结果。 我有两个searchfilter。 第一个是select_tag。 第二个是复选框。 这是我的代码: results.html.erb Filter “artists_search” do %> 的application.js # Ajax search on keyup $(‘#artists_search input’).keyup( -> $.get($(“#artists_search”).attr(“action”), $(“#artists_search”).serialize(), null, ‘script’) false ) user.html.erb def result if params[:titel_id].present? @users = User.where(‘titel_id = ?’, titel_id) if params[:option].present? @users = User.where(..) else @users = User.all end respond_to do |format| format.html # result.html.erb […]

rails 4:在第2行第11列(Psych :: SyntaxError)的上下文中不允许映射值?

我已经成功安装了rails 4并使用rails 4.0.0创建了项目 rails s 这是第一次工作正常。 但在gemfile中添加’devise’gem后 gem ‘devise’, ‘~> 3.0.0.rc’ 并运行bundle install,我收到以下错误。 这个错误的原因是什么,它的解决方案是什么? 我也试过评论gem’devise’部分,但现在我得到的每个rails命令都有相同的错误。 /home/siv/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/psych.rb:205:in `parse’: (): mapping values are not allowed in this context at line 2 column 11 (Psych::SyntaxError) from /home/siv/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/psych.rb:205:in `parse_stream’ from /home/siv/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/psych.rb:153:in `parse’ from /home/siv/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/psych.rb:129:in `load’ from /home/siv/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/rubygems/config_file.rb:318:in `load_file’ from /home/siv/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/rubygems/config_file.rb:191:in `initialize’ from /home/siv/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/rubygems/gem_runner.rb:66:in `new’ from /home/siv/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/rubygems/gem_runner.rb:66:in `do_configuration’ from /home/siv/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/rubygems/gem_runner.rb:46:in […]

Rails嵌套表单,其中预定义字段为复选框

我正在制作一个包含Room和RoomAttribute模型的酒店应用程序。 这两个模型通过连接表在彼此之间具有many_to_many关系。 每个模型的属性如下: Room – room_number , room_type (例如“豪华”或“套房”)和price 。 RoomAttributes – name (例如“无线互联网”,“有线电视”,“浴缸”)。 用户将首先创建一组房间属性,以便每次创建新房间时都可以选择这些房间属性。 例如,有些房间可能有无线互联网,有些则没有。 app/views/rooms/new.html.erb的代码是(我对使用原始html表示歉意)。 <form action="” method=”post”> <input type="hidden" name="authenticity_token" value="”> Room Number: Type: Price: Attributes: <input type="checkbox" name="room[room_attributes_ids][]" value="”> 我正在使用Rails 4,我想就以下内容征求意见: 使用表单助手或formtastic设置表单,以便它已经输出所有预定义的房间属性,用户只需通过复选框选择要包含在房间中的属性。 编写RoomController#create方法,以便将嵌套的RoomAttribute模型设置为房间属性。 我的app/models/room.rb是否需要accepts_nested_attributes_for :room_attributes app/models/room.rb ? 如何在此方案中合并强参数。 我读过我应该用的 params.require(:room).permit(:room_number, :room_type, :price, room_attributes_attributes: [:id]) 但这不适合我。 谢谢! 🙂

使用MiniTest测试用户模型(设计认证)

我正在尝试测试用户模型,为此我设计了身份validation。 我面临的问题是,1。在fixtures中输入’password’/’password_confirmation’字段会给我无效的列’password’/’password_confirmation’错误。 如果我从fixture中删除这些列并添加user_test.rb require ‘test_helper’ class UserTest < ActiveSupport::TestCase def setup @user = User.new(name: "example user", email: "example@example.com", password: "Test123", work_number: '1234567890', cell_number: '1234567890') end test "should be valid" do assert @user.valid? end test "name should be present" do @user.name = "Example Name " assert @user.valid? end end 我得到的错误是: test_0001_should be valid FAIL (0.74s) Minitest::Assertion: […]

如何在rails中添加options_from_collection_for_select的数据属性

将此视为我的选择标记 这工作正常,但我需要一个数据属性 @product.id无效。 我怎样才能获得ID

下拉铁路4国家列表的最佳方式

我有一个表单,用户输入一些信息,我想添加到这个表单一个国家,我正在徘徊是什么是最好的方式来做这个(种子数据,Csv,…)我不想使用gem

在ruby on rails上使用jQueryvalidation提交按钮不起作用

我正在使用ruby2和rails4。 我使用jQuery进行validation。 validation错误是正确显示的,但是当我点击提交按钮时,虽然我已经为字段指定了正确的值,但它什么也没做。 有人可以帮我解决问题吗? 我的代码如下所示。 谢谢。 jQuery插件: http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js 在download.html.erb中 :post, :id => ‘contact-form’ do |f| %> Download Form $(document).ready(function () { $(“#contact-form”).validate({ debug: true, rules: { “contact[name]”: {required: true, max: false, min: false}, “contact[email]”: {required: true, email: true}, “contact[phone]”: {required: true, digits: true, minlength: 10, maxlength: 10} } }); });

如何在枚举字段中使用CanCanCan?

我得到了枚举字段enum status: [:pending, :done] 文章模型enum status: [:pending, :done] 。 这是我的能力档案 class Ability include CanCan::Ability def initialize(user) user ||= User.new if user.member? can :read, Article.done end end end 在视图中我试图为成员渲染Article.done集合但不是渲染渲染。 因此我有一个问题:在CanCanCan中有没有可能使用enum的方法?

按共同关联数排序(Rails)

背景:我有post和用户,都有很多社区。 目标:对于任何给定的用户,我想返回一组post,按照post与用户共有的社区数量排序(更多社区的post更高) 我当前的尝试(使用排序方法)有效: Post.includes(:community_posts).where(community_posts: { community_id: current_user.community_ids }).sort{ |x,y| (y.community_ids & current_user.community_ids).length (x.community_ids & current_user.community_ids).length } 但有没有更好/更有效的方法来做到这一点?

Rails 4 Devise / Omniauth电子邮件存在于数据库错误中

我正在“撞击”我的头撞墙,试图解决这个问题。 我决定允许用户通过Twitter,Google,我的网站或Facebook登录。 问题是twitter不提供电子邮件,所以我试图通过用户名添加用户。 问题是设计不断检查电子邮件,当我不需要它时,我得到PG ::错误:错误:重复键值违反唯一约束“index_users_on_email”DETAIL:Key(email)=()已经存在。 :INSERT INTO“users”(“created_at”,“name”,“provider”,“uid”,“updated_at”,“username”)VALUES($ 1,$ 2,$ 3,$ 4,$ 5,$ 6)返回“id”。 我不知道该怎么做。 我在我的数据库中创建了一个用户名列,我确保将它添加到我的devise.rb intializer中, config.authentication_keys = [ :username ]