Tag: 形成

Ruby on Rails:使用一个表单和一个提交为多个模型创建记录

我有3个型号:报价,客户和项目。 每个报价都有一个客户和一个项目。 当我按下提交按钮时,我想在各自的表格中创建新报价,新客户和新项目。 我已经查看了其他问题和轨道广播,要么它们不适用于我的情况,要么我不知道如何实现它们。 quote.rb class Quote < ActiveRecord::Base attr_accessible :quote_number has_one :customer has_one :item end customer.rb class Customer < ActiveRecord::Base attr_accessible :firstname, :lastname #unsure of what to put here #a customer can have multiple quotes, so would i use has_many or belongs_to? belongs_to :quote end item.rb的 class Item < ActiveRecord::Base attr_accessible :name, :description #also […]

帮助中的link_to与块

我试图让这个工作: link_to(“#”, class: “add_fields btn btn-success”) do name content_tag(:i, “”, :class => “icon-plus icon-white”) end 但它只显示我指定的图标(twitter-bootstrap css)而不是name的文字,我做错了什么?

Rails 3 Form Helpers:UTF8和其他隐藏字段

风景: ‘text’, :id => ‘title’, :size => ”, :limit => ‘255’ %> LevihackwithCom Title 我正在搞乱表格助手。 上面的代码显示了我的视图文件以及它生成的HTML。 什么是可怕的div充满内联CSS填充隐藏字段我没有明确要求? 什么设置会导致生成这些字段? 有没有办法删除内联CSS?

Rails 3.2从子视图创建父模型

我很难理解如何做到这一点。 我有两个模型,一个项目和一个课程。 #project.rb belongs_to :course attr_accessible :course_id, :course accepts_nested_attributes_for :course, reject_if: lambda { |a| a[:course_id] == 0 } #course.rb has_many :projects 在Projects#new页面(子对象)上,我想输入新course的名称并让它创建父对象。 这是我在视图中的尝试,但它似乎没有正常工作。 = form_for [@user, @project] do |f| # Other fields = fields_for :course do |builder| = builder.label :name, ‘Course Name’ = builder.text_field :name = f.submit 我稍后将使用这个父对象来创建更多项目,但是现在,让我们假设它不存在。 更新1我已经修改了我的fields_for(根据Ryan的要求): = form_for [@user, @project] do |f| […]

Rails Restful actions Index Put

我经常遇到我想要一次更新许多记录的情况 – 就像GMail设置许多消息“读取”或“未读”一样。 Rails通过ActiveRecord类的’update’方法鼓励这一点 – Comment.update(keys,values) 示例 – http://snippets.dzone.com/posts/show/7495 这是很棒的function,但很难映射到一个宁静的路线。 从某种意义上说,我希望看到:对集合采取行动。 在路线中,我们可能会添加类似的东西 map.resources :comments, :collection => { :update_many => :put } 然后在表格中,你会这样做…… … 这在许多层面都不起作用。 如果你这样做:: collection => {:update_many =>:put},rails会向索引动作(CommentsController #index)提交一个post,我希望它转到’update_many’动作。 相反,您可以执行:collection => {:update_many =>:post}。 这至少会在控制器中执行正确的操作。 并且,而不是<%form for @comments …您必须执行以下操作: { :controller => :comments, :action => :update_many } do |f| %> 它会以这种方式工作 仍然不完美 – 感觉有点像我们没有做’Rails方式’。 它看起来也像:post,and:delete对集合控制器也有意义。 […]

使用嵌套表单创建用户时Rails回滚

我在尝试将用户添加到我的数据库时收到回滚,但我不确定原因。 我有三个型号: company.rb class Company < ActiveRecord::Base acts_as_paranoid has_many :locations, dependent: :destroy has_many :users, dependent: :destroy has_many :tests, through: :locations has_many :reports, dependent: :destroy accepts_nested_attributes_for :locations, :users validates_presence_of :name end ** user.rb ** class User < ActiveRecord::Base acts_as_paranoid devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :registerable belongs_to :company has_and_belongs_to_many :roles end ** location.rb ** class Location […]

为新的,编辑更新和创建操作加载其他模型集合的rails方式是什么?

如何在新的,编辑更新和创建操作中为ProductController加载Category模型的最佳方式 产品有类别集合 class Product < ActiveRecord::Base has_many :categories end 始终用于新建,编辑,创建和更新操作,我需要加载类别集合以填充check_box_tag列表 这种情况的“婴儿步骤”是: class Admin::ProductsController < Admin::AdminApplicationController def new @product = Product.new @categories = Category.all end def edit @product = Product.find(params[:id]) @categories = Category.all end def create @product = Product.new(params[:product]) if @product.save flash[:notice] = 'Product was successfully created' redirect_to edit_admin_product_path(@product) else @categories = Category.all render :new end […]

Rails:恢复使用form_tag的非模型表单的内容

我的第一个Rails应用程序(使用Rails 3)取得了很好的进展。 MVC交互一切都很顺利,但是我对一个与模型没有直接关系的表单有困难。 我正在使用form_tag,并且在成功的情况下,一切都表现良好。 但是,处理错误有点不友好。 实际的错误消息存储在flash中并通过layouts / application.html正常显示,但如果表单能够记住用户刚刚填写的内容,我真的很喜欢它。但它不是:所有的字段重置为默认值。 我喜欢对对象的RESTful操作表单的方式自动记住他们提交的值,如果有错误则以红色突出显示。 没有红色突出显示我很好,但如果我能让表单的字段保持提交的值,我真的很喜欢它。 任何人都可以建议如何做到这一点? 摘录相关文件: 意见/ cardsets / import.html.erb: :import_data, :id => @cardset do %> … 1 %> … …etc (more fields) 控制器/ cardsets_controller.rb: # POST /cardsets/1/import_data def import_data success, message = @cardset.import_data(params, current_user) if success redirect_to(@cardset, :notice => message) else flash.now[:error] = message render :import end end

如何将数组传递给Rails中的fields_for?

我想在关联中的记录子集上使用fields_for 。 我有Month模型, has_many :payments 。 但在我看来,我只想在其中支付部分款项。 例如: – fields_for @month.payments.large 这不起作用。 我可以将一组记录传递给fields_for ,而不是通常的符号( fields_for :payments )方法吗?

Railscast 198,但使用formtastic

你如何使用Formtastic单独编辑多个记录来完成RyanB的Railscast所涵盖的内容? Formtastic不使用form_tag,RyanB的方法依赖于form_tag。