Tag: ruby on rails 4

为什么我的Rails应用程序重定向到`data:,`?

因此,当我在Rails 4应用程序中保存记录时,会发生这种情况。 这是一些细节: 我正在使用Ace编辑器 。 data属性不在我的模型或应用程序中。 表单是标准的form_for (非远程)。 该记录确实成功保存,但随后它重定向到这个奇怪的屁股URL。 更新的代码是标准的脚手架样板。 # PATCH/PUT /pages/1 # PATCH/PUT /pages/1.json def update respond_to do |format| if @page.update(page_params) format.html { redirect_to @page, notice: ‘Page was successfully updated.’ } format.json { head :no_content } else format.html { render action: ‘edit’ } format.json { render json: @page.errors, status: :unprocessable_entity } end end […]

使现有列在Rails中唯一

我在users表上有users名列。 我可以像这样做一些事情: rails generate migration add_username_to_users username:string:uniq但我有username rails generate migration add_username_to_users username:string:uniq (我已经运行了迁移)。 那么如何才能使用username列独一无二?

Rails中的ActiveModel :: Serializer – JSON结果中忽略的序列化方法

我正在使用active_model_serializers为我的Rails模型创建JSON。 串行 class OptionSerializer < ActiveModel::Serializer self.root = false attributes :id def test_id object.id end end 但是,to_json选项似乎忽略了OptionSerializer中添加的方法: OptionSerializer.new(Option.find(13)).to_json.html_safe 预期产出 { “id”: 13, “test_id”: 13 } 实际产出 { “id”: 13 } 我已经查看了这篇SOpost ,但这是我能找到的唯一一个有人遇到这个问题的post。 我正在运行Ruby 1.9.3和Rails 4.0.0。 感谢您的时间。 非常感谢任何支持,意见或建议。

Fresh Rails 4应用程序无法识别`rails`命令,坚持使用`rails new`

我已经使用Rails主分支的克隆中的rails new命令成功创建了一个新的应用程序。 该应用程序看起来很好,并且肯定是Rails 4.但是当我尝试运行任何其他rails命令时,提示符给了我: Usage: rails new APP_PATH [options] Options: -r, [–ruby=PATH] # Path to the Ruby binary of your choice … etc etc 我试过rails , bundle exec rails和bin/rails ,都给出了相同的提示。 我创建新的Rails 4应用程序的步骤是: 将Rails主分支克隆到本地计算机 创建一个新的rvm gemset,然后使用它(1.9.3) 运行rails/railties/bin/rails new myapp –edge -T –skip-index-html cd myapp并运行bundle install和bundle update以确保 运行任何rails命令 我究竟做错了什么? rvm-prompt表明我正在使用正确的gemset。 我注意到的一件事是,即使我从我的Gemfile中删除jbuilder并运行bundle update ,它仍会出现在我的gem list 。 我的rvm在这里有问题吗? rails-v输出是’Rails […]

嵌套表单不创建嵌套的记录。

您好我有2个型号Product和ProductSize。 我在Product表单中创建了ProductSize。 问题是,它不能持久创建ProductSizes。 您应该能够访问product.product_sizes并显示ProductSizes列表。 product.rb class Product :destroy validates :title, presence: true, length: { maximum: 30 } validates :description, presence: true, length: { maximum: 2000 } validates :category, :user, :price, presence: true accepts_nested_attributes_for :product_images, :product_sizes, allow_destroy: true end product_size.rb class ProductSize < ActiveRecord::Base belongs_to :product belongs_to :size validates :quantity, presence: true end 这是我的表格。 它的工作方式是:用户可以上传图像,然后选择产品的类别。 让我们说他们选择衬衫,然后所有衬衫尺寸的列表将下降像XS,小,中,大。 […]

在创建费用之前validationCVC代码

我正在尝试检查用户输入的cvc代码。 我已经存储了条带customer_id和条带card_id。 我想在向用户收费之前validationCVC代码。 以下是创建费用的代码。 charge = Stripe::Charge.create( :amount => 1000, :currency => “usd”, :customer => “cus_3sAc**************”, :card => “card_18m*********************”, :description => “Testing multiple card payment”, :capture => false # default true, :cvc => 125 ) 但是这里的cvc参数无效。 如果我删除cvc,它工作正常。 但它不会检查CVCvalidation。 我怎么检查这个?

我无法使用accepts_nested_attributes_for创建模型对象。 它不会创建嵌套对象

我的模型结构如下所示: 董事会has_many主题。 主题has_manypost。 应用程序/模型/ board.rb class Board < ActiveRecord::Base has_many :topics end 应用程序/模型/ topic.rb class Topic < ActiveRecord::Base belongs_to :user belongs_to :board has_many :posts accepts_nested_attributes_for :posts validates :title, presence: true, length: { maximum: 255 } validates :user_id, presence: true validates :board_id, presence: true … end 应用程序/模型/ post.rb class Post < ActiveRecord::Base belongs_to :user belongs_to :topic […]

Rails 4如何在视图中使用渲染布局选项?

只是乱搞铁路布局。 完成指南 2.2.11.2:布局选项 创建了一个名为two_column_landing的简单布局,并将其放在app / views下的layouts目录中。 然后修改我的视图以呈现布局: Ca#tcl Find me in app/views/ca/tcl.html.erb 我收到这个错误: You invoked render but did not give any of :partial, :template, :inline, :file or :text option. 我知道在生产代码中做这个习惯可能不是一件好事,但我只想弄清楚如何做我想做的事。 任何想法为什么这不起作用? 它不是导轨4的特征吗?

路由错误没有路由匹配“/ locations”

尝试更新位置项后,我收到路径错误 我正在使用before_action来设置位置ID before_action :set_location, only: [:show, :edit, :update, :destroy] private def set_location @location = Location.find(params[:id]) end 这是我的控制器我只有更新new和删除工作的问题 def create @location = Location.new(location_params) respond_to do |format| if @location.save format.html { redirect_to @location, notice: ‘Location was successfully created.’ } format.json { render action: ‘show’, status: :created, location: @location } else format.html { render action: ‘new’ } format.json […]

将字段名称作为参数传递给自定义validation方法Rails 4

我有一个自定义validation方法: def my_custom_validation errors.add(specific_field, “error message”) if specific_field.delete_if { |i| i.blank? }.blank? end 目标是禁止包含[“”]参数通过validation,但我需要调用此方法,如: validate :my_custom_validation #and somehow pass here my field names 例如: validate :my_custom_validation(:industry)