Tag: 形成

在Rails中将多级嵌套表单标记为“脏”

我在Rails中有一个三级多嵌套表单。 设置如下:项目有许多里程碑,里程碑有很多笔记。 目标是使用JavaScript在页面中编辑所有内容,我们可以在页面中向项目添加多个新里程碑,并将新Notes添加到新的和现有的里程碑。 一切都按预期工作,除了当我向现有的里程碑添加新笔记(新的里程碑在向他们添加笔记时工作正常)时,新笔记将不会保存,除非我编辑任何实际属于里程碑的字段以标记forms“脏”/编辑。 有没有办法标记里程碑,以便添加的新Notes将保存? 编辑:对不起,很难粘贴所有代码,因为有很多部分,但是这里有: 楷模 class Project :destroy has_many :milestones, :dependent => :destroy accepts_nested_attributes_for :milestones, :allow_destroy => true accepts_nested_attributes_for :notes, :allow_destroy => true, :reject_if => proc { |attributes| attributes[‘content’].blank? } end class Milestone :destroy accepts_nested_attributes_for :notes, :allow_destroy => true, :allow_destroy => true, :reject_if => proc { |attributes| attributes[‘content’].blank? } end class Note < […]

如何为具有多态关联的模型优雅地构建表单?

这是我的模特: class Lesson true validates_presence_of :topic_type, :topic_id end class Subject :topic end class Category :topic end 现在,我需要的是一个允许用户创建或更新课程的表单。 问题是,我如何提供一个提供主题和类别混合的选择菜单? (对于用户而言,在此特定表格中,主题和类别是可互换的,但在其他地方并非如此。) 理想情况下,这看起来像这样: 意见/经验/ _form.html.haml = simple_form_for(@lesson) do |f| = f.input :title = f.association :topic, :collection => (@subjects + @categories) 这不起作用,因为我们只指定topic_id,我们也需要topic_types。 但是我们如何指定这些价值呢? 我想这个问题的症结在于我真的想要一个单一的选择菜单,它指定两个对应于两个不同属性的值(topic_id和topic_type)。 有没有优雅的方式来做到这一点? 几点说明: a)单表inheritance会使这个问题消失,但我想避免这种情况,因为类别和主题有他们自己的关系……我会饶有你的细节。 b)我可能会拉一些javascript shenanigans,是吗? 但这听起来很混乱,如果有一种更清洁的方式来做,一些神奇的forms助手或其他东西,那么这当然更可取。 c)虽然我使用的是simple_form,但我不会坚持使用它,以防万一这会让事情变得复杂。 谢谢

使用rails中的simple_form和AJAX解析JSON错误

我有一个表单在用户仪表板上使用Simple Forms和remote => true(由仪表板视图和仪表板控制器处理)。 该表单允许用户添加自定义游戏规则; 但是,它通过Jquery的.load()函数加载表单。 表单从“规则”视图加载(由规则控制器处理)。 当用户单击提交按钮时,我没有得到任何错误validation。 她是规则控制器上的创建操作: def create @rule = Rule.new(params[:rule]) respond_to do |format| if @rule.save format.html { redirect_to dashboard_path, notice: ‘Rule was successfully created.’ } format.json { render json: @rule, status: :created, location: @rule } format.js else format.html { render template: ‘dashboard/index’} format.json { render json: @rule.errors, status: :unprocessable_entity } format.js […]

Rails – 一种forms到两种模式

我正在Rails(版本4.2.5)中构建一个表单来收集有关商店的基本信息,我希望将表单提交给两个单独的模型 – 一个名为Store,它收集有关商店的信息,一个名为Address ,它只保存地址信息。 我已经看到这个问题浮在Stack和其他地方,但我对Rails很新,可以使用更多的逐步指导。 (另外,我知道Rails地理编码器的gem,但不想在这个项目中使用它,尽管我正在收集地址和纬度/长信息) 对于初学者,这是我的商店和地址模型…… class Store < ActiveRecord::Base belongs_to :user belongs_to :store_type has_one :address end class Address < ActiveRecord::Base belongs_to :store end 和我的StoresController …… class StoresController < ApplicationController def new @store = Store.new @storeType = StoreType.all end def create @store = Store.new(store_params) @store.user = current_user if @store.save flash[:notice] = "New store created" […]

Rails简单forms给出了InvalidAuthenticityToken错误

我有一个这样的简单forms: 和这种方法的控制器: def busca puts params[:searchText] end 当我单击表单中的图像按钮时,我得到一个ActionController :: InvalidAuthenticityToken。 这是完整的StackTrace: /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/request_forgery_protection.rb:86:in verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in发送’/Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in evaluate_method’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:166:in verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in / verify_authenticity_token’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in evaluate_method’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:166:in调用’/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:225:in call’ /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:629:in […]

在Rails表单中使用textarea助手

为什么此代码在文本区域显示错误? {:action => ‘create’}) do |f| %> “50×10”) %>

使用带参数和form_tag的命名路由

我正在尝试在Rails中创建一个简单的搜索表单,但我想我错过了一些东西。 我有一个命名的搜索路线: map.search “:first_name/:last_name”, :controller => “home”, :action => “search” 我正在尝试在我的搜索表单中使用它: ‘get’) do %> 但是当我加载搜索表单时,我得到一个ActionController :: RoutingError: search_url无法从{:action =>“search”,:controller =>“home”}生成 – 您可能有不明确的路线,或者您可能需要为此路线提供其他参数。 content_url具有以下必需参数:[:first_name,:last_name] – 它们是否都满足? 我错过了什么? 我认为我的表单中定义的字段会自动与我的路由参数相关联。 : – / 更新: 我知道search_path是在现在显示表单之前生成的,因此无法更新。 后见之明显而易见! 我改变了路线: map.search ‘search’, :controller => “home”, :action => “search” map.name ‘:first_name/:last_name’, :controller => “home”, :action => “name” 现在search操作就是: def search redirect_to name_path(params) […]

如何使用改革预填充特色对象?

当用户点击,例如“拍照”时,我们如何预先填充create.html.erb表格,其中包含专门针对该特色挑战设置的属性,例如12天,以及Tue, Thu吗? 我正在使用reformgem 。 #challenges_controller def new @challenge = Challenge.new respond_modal_with @challenge, location: root_path end #challenges/new.html.erb Or choose a featured challenge: #challenges_controller def create @challenge = Challenge.new(challenge_params) #create is being used to GET and then POST if params[:step] == ‘2’ @challenge = current_user.challenges.build(challenge_params) @challenge.save redirect_to @challenge end end #challenges/create.html.erb Challenge: Do For: Do On: class […]

ruby on rails link_to删除方法无法正常工作

我想使用以下代码删除post: :delete, :onclick => “return confirm(‘Are you sure you want to delete this post?’)” %> 这不起作用……它只是将我重定向回post(posts/:id} 但是,如果我使用以下代码,它的工作原理 “return confirm(‘Are you sure you want to delete this post?’)” %> 在这种情况下,是否可以使link_to表现为button_to ? 编辑:破坏控制器function def destroy @post = Post.find(params[:id]) @post.destroy respond_to do |format| format.html { redirect_to posts_url } format.json { head :no_content } end end 单击“销毁”按钮时记录: Started GET […]

更改rails text_field表单构建器类型

我一直在研究HTML5表单中提供的新选项,例如将输入类型声明为“email”,“url”和“number”,如此处所述。 如何将这些与轨道表单构建器一起使用? 我试过了 ‘user@domain.com’} %> 但这不起作用。 我也试过了 ‘user@domain.com’, :type => :email} %> 但类型仍然是“文本”而不​​是被覆盖。 是否可能,或者这是否需要在Rails中解决?