Tag: form for

如何在Rails中设置哈希表单?

我有一些数据与散列中的模型相关联。 哈希值在控制器中生成: @hash 。 为此数据创建表单的正确方法是什么? 我想出了以下代码: 这会生成表单,但它会将每个哈希项创建为表单中的单独变量。 这似乎不是提交数据的正确方法。 我想将数据作为哈希值返回,并使用params[:hash]访问它。 做这个的最好方式是什么? 使用Rails 3.07,Ruby 1.9.2。 谢谢。 编辑:我应该说清楚。 此代码位于为模型生成的表单内。 因此,表单需要提交模型的所有字段,以及上面的哈希。

Rails 4:fields_for中的fields_for

我正在学习RoR,我正在尝试找到如何使用has_one模型在另一个中设置fields_for: class Child < ActiveRecord::Base belongs_to :father accepts_nested_attributes_for :father end class Father < ActiveRecord::Base has_one :child belongs_to :grandfather accepts_nested_attributes_for :grandfather end class Grandfather < ActiveRecord::Base has_one :father end 我在Railscasts上使用嵌套模型表单第1部分来获取这些:在children_controller.rb中: def new @child = Child.new father=@child.build_father father.build_grandfather end def child_params params.require(:child).permit(:name, father_attributes:[:name], grandfather_attributes:[:name]) end 我的表格: mother: grand mother: 我试图用以下方法检索数据: 但爷爷的名字不会起作用。 我找不到错误……有人帮忙吗? 谢谢!

Rails form_for with file_field and remote => true and format =>:js

我正在提交一个带有file_field的form_for。 当表单与post一起提交时,控制器以.js文件响应,该文件将被执行。 file_field是可选的。 当我上传文件时,js文件在新文档中打开(作为简单的文本文件),并且不执行js。 当我不上传任何文件时,响应div将更新。 我希望在响应div中通知更新成功或者独立于文件上载的失败消息。 可能出于某些安全原因这样做了吗? 想要能够做到这一点的人有一些解决方法吗? 大致, 表单部分_action.html.erb: true, :url => {…, :format => :js} do |f| %> … 控制器::行动 if not request.post? render :partial => ‘controller/action’ return else @response = “save was successful”; # renders the action.js.erb end action.js.erb: $(“#response”).html(“”);

Rails:form_for命名空间资源

我想为用户设置CRUD,仅适用于我的Web应用程序的管理员。 所以在routes.rb中: namespace :admin do resources :user end 这意味着: admin_user_index GET /admin/user(.:format) admin/user#index POST /admin/user(.:format) admin/user#create new_admin_user GET /admin/user/new(.:format) admin/user#new edit_admin_user GET /admin/user/:id/edit(.:format) admin/user#edit admin_user GET /admin/user/:id(.:format) admin/user#show PUT /admin/user/:id(.:format) admin/user#update DELETE /admin/user/:id(.:format) admin/user#destroy 显示,索引工作正常,但编辑和新的不。 我在_form第一行继续收到此错误: ###:0x007fb6645c6378>的未定义方法`user_path’ 这是这样的: 如何将form_for与命名空间资源一起使用?

RoR之间的区别:url,:action,:form_for中的方法

文档中可能有答案,但我似乎没有找到好的答案。 所以在三个:url,:action,:方法中,在Rails中的form_for中使用它们有什么区别?

使用带远程的ajax的Ruby表单:true给出了ActionController :: InvalidAuthenticityToken错误。 经典提交没有

我正在为一个RoR网站写一个聊天页面。 我已经完成了所有HTML和我试图用ajax实现它。 有一个提交消息的表单。 表单标签读取 true) do |f| %> true) do |f| %> 我的整个观点: Battleriskopoloy – Communications [ . . . Communications . . . ] “select”, :method => :get %> true) do |f| %> 控制器: class ChatsController < ApplicationController $messeges = Array.new(10, " ") def new $messeges = Array.new if $ruser != nil $messeges = […]

具有多个控制器动作的form_for用于提交

如何在form_for提交时传递url? 我正在尝试使用一个表单,每个按钮指向每个控制器操作,一个是搜索,另一个是创建。 是否可以在同一表单上设置2个具有不同操作的提交按钮? : : ‘/people/search’) %> ‘/people/create’) %>