标签: form-for

rails simple_form - 隐藏字段 - 创建?

你怎么能有一个简单形式的隐藏领域?

以下代码:

= simple_form_for @movie do |f|
  = f.hidden :title, "some value"
  = f.button :submit
Run Code Online (Sandbox Code Playgroud)

导致此错误:

undefined method `hidden' for #SimpleForm::FormBuilder:0x000001042b7cd0
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails hidden-field form-for ruby-on-rails-3 simple-form

171
推荐指数
4
解决办法
11万
查看次数

具有嵌套资源的form_for

我有一个关于form_for和嵌套资源的两部分问题.假设我正在编写一个博客引擎,我想将评论与文章联系起来.我已经定义了一个嵌套资源,如下所示:

map.resources :articles do |articles|
    articles.resources :comments
end
Run Code Online (Sandbox Code Playgroud)

评论表单位于文章的show.html.erb视图中,位于文章本身下方,例如:

<%= render :partial => "articles/article" %>
<% form_for([ :article, @comment]) do |f| %>
    <%= f.text_area :text %>
    <%= submit_tag "Submit" %>
<%  end %>
Run Code Online (Sandbox Code Playgroud)

这给出了一个错误,"为nil调用id,这会错误等等".我也试过了

<% form_for @article, @comment do |f| %>
Run Code Online (Sandbox Code Playgroud)

哪个呈现正确但将f.text_area与文章的"文本"字段而不是注释相关联,并在该文本区域中显示了article.text属性的html.所以我似乎也有这个错误.我想要的是一个表单,其'submit'将在CommentsController上调用create action,在params中有一个article_id,例如对/ articles/1/comments的post请求.

我的问题的第二部分是,开始创建评论实例的最佳方法是什么?我正在ArticlesController的show动作中创建一个@comment,因此一个注释对象将在form_for帮助器的范围内.然后在CommentsController的create动作中,我使用从form_for传入的参数创建新的@comment.

谢谢!

ruby-on-rails form-for nested-resources

118
推荐指数
3
解决办法
10万
查看次数

rails form_for标签的自定义文本

我想在以下位置显示标签form_for:

<div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name %>
</div>
Run Code Online (Sandbox Code Playgroud)

这会生成标签"Name",但我希望它是"你的名字".我该怎么改变它?

ruby ruby-on-rails form-for ruby-on-rails-3.2

95
推荐指数
2
解决办法
6万
查看次数

Rails不可编辑的文本字段

我有一个以下列方式编写的form_for:

<div class="field">
    <%= location.label :city %>
    <%= location.text_field :city, :disabled=>true%>
</div>
<div class="field">
    <%= location.label :country %>
    <%= location.text_field :country, :disabled=>true%>
</div>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,2文本字段被禁用,因为它们是由jquery函数自动填充的,我不希望让用户处理它们.问题是,通过这种方式,视图不会将该参数传递给控制器​​,因为已禁用!有没有其他方法可以将不可编辑的text_field传递给控制器​​,注意我不想使用隐藏字段,因为我想在文本框中向用户显示结果

TNX

ruby-on-rails textfield form-for

77
推荐指数
1
解决办法
5万
查看次数

Ruby on rails:奇异资源和form_for

我希望用户只使用一个连接到用户会话的订单.所以我为订单设置了奇异的资源

routes.rb中:

resource :order
Run Code Online (Sandbox Code Playgroud)

意见/命令/ new.html.erb:

<%= form_for @order do |f| %>
   ...
<% end %>
Run Code Online (Sandbox Code Playgroud)

但是当我打开新的订单页面时,我收到一个错误:

undefined method `orders_path`
Run Code Online (Sandbox Code Playgroud)

我知道,我可以设置:url => order_pathform_for,但什么是解决这一冲突的真正方法是什么?

resources router ruby-on-rails form-for singular

62
推荐指数
2
解决办法
1万
查看次数

使用浅路由时,不同的路由需要不同的form_for参数

我在这里使用Simple Form,但这也是普通Rails表单的问题.使用浅路由时,form_for需要不同的参数,具体取决于它使用的上下文.

示例:对于编辑(http://localhost:3000/notes/2/edit),_ form.html.erb需要simple_form_for(@note).但是为了创建一个新的音符(http://localhost:3000/customers/2/notes/new)_form.html.erb需要simple_form_for([@customer, @note]).如果接收到错误的参数,我将得到一个找不到方法的错误.

处理这个问题的最佳方法是什么?

  • 我可以制作两种不同的形式,但这看起来很混乱.
  • 我必须为后端链接设置@customer,但我可以在表单中使用不同的变量(比如@customer_form),而不是在编辑和更新方法中设置它,但这是不一致的,有点混乱,因为我必须在新方法中设置@customer_form和@customer.
  • 我可以做这个人做的事情并将表格分成多个文件.它看起来是迄今为止最好的选择,但我并不是很喜欢它,因为你不能只打开_form.html.erb看看发生了什么.

这些是我唯一的选择吗?

示例如下:

配置/ routes.rb中

Billing::Application.routes.draw do
  resources :customers, :shallow => true do
    resources :notes
  end
end
Run Code Online (Sandbox Code Playgroud)

耙路线| grep note

    customer_notes GET    /customers/:customer_id/notes(.:format)         notes#index
                   POST   /customers/:customer_id/notes(.:format)         notes#create
 new_customer_note GET    /customers/:customer_id/notes/new(.:format)     notes#new
         edit_note GET    /notes/:id/edit(.:format)                       notes#edit
              note GET    /notes/:id(.:format)                            notes#show
                   PUT    /notes/:id(.:format)                            notes#update
                   DELETE /notes/:id(.:format)                            notes#destroy
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/笔记/ _form.html.erb

#                      v----------------------------- Right here
<%= simple_form_for (@note), html: { class: 'form-vertical'} do |f| %>
  <%= …
Run Code Online (Sandbox Code Playgroud)

form-for ruby-on-rails-3

42
推荐指数
3
解决办法
7121
查看次数

更改form_for rails 3.1生成的html表单id

我有这个form_for:

<%= form_for [post, Comment.new,], :remote => true do |f| %>
<%= f.text_area :content, :cols =>10, :rows => 1%>
<% end %>
<%= f.submit :class => "input_comment"  %>
Run Code Online (Sandbox Code Playgroud)

那会生成下一个代码html:

<form method="post" id="new_comment" data-remote="true" class="new_comment" 
action="/post/4efcda9e1d41c82486000077/comments" accept-charset="UTF-8"><div 
style="margin:0;padding:0;display:inline"><input type="hidden" value="?" name="utf8">
<input type="hidden" value="ctVfDF/O4FIR91I7bC5MVezQmutOCkX3dcXe73uNPZY=" name="authenticity_token">

<textarea rows="1" name="comment[content]" id="comment_content" cols="10"></textarea>
<input type="submit" value="Create Comment" name="commit" class="input_comment">
</form>
Run Code Online (Sandbox Code Playgroud)

如果我在同一页面中有多个表单,则不是具有相同ID的html有效.

  • form_for的id生成id ="new_comment"
  • textarea的id生成id ="comment_content"

在同一页面中有这么多表单是无效的HTML.

如何通过rails 3.1中的form_for方法助手更改id autogenerate?

ruby ruby-on-rails form-for ruby-on-rails-3

38
推荐指数
3
解决办法
4万
查看次数

命名空间form_for中的嵌套资源

问题

form_for帮助程序错误地确定了命名空间内嵌套资源的路径.有问题的模型是:Forum :: ThreadForum :: Reply,分别位于我的models目录下名为"forum"的子文件夹中.这是在Rails 3 BETA 3中.

的routes.rb

  namespace :forum do
    root :to => 'threads#index'
    resources :threads do
      resources :replies
    end
  end
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/论坛/回复/ _form.html.haml

...
  - form_for [@thread, @reply] do |f|
...
Run Code Online (Sandbox Code Playgroud)

应用程序/控制器/论坛/ replies_controller.rb

...
  def new
    @reply = Forum::Reply.new
  end
...
Run Code Online (Sandbox Code Playgroud)

错误

undefined method `forum_thread_forum_replies_path'
Run Code Online (Sandbox Code Playgroud)

参考上面_form.html.haml中概述的行

namespaces ruby-on-rails form-for ruby-on-rails-3

30
推荐指数
1
解决办法
2万
查看次数

来自Rails form_for的params哈希的自定义名称

通常,使用form_for(@foo)表示在表单操作的后端,您将拥有表单数据params[:foo],但在我的情况下,我希望将自定义命名空间应用于这些参数,即params[:bar]params[:foo].

不是说通过向方法提供:namespace参数来使命名空间更长form_for.相反,我现在的名字是长期的,我想缩短它.更重要的是,我实际上正在交换一个新模型代替现有模型,因此控制器充满了调用params[:quoter],而我们的新模型供应params[:company_quoter_intf_quoter].有任何想法吗?

规范:Ruby 1.9.3,Rails 3.2.3

ruby-on-rails form-for

29
推荐指数
1
解决办法
1万
查看次数

Rails:多次使用form_for(DOM ID)

我想在同一页面中对同一模型多次使用form_for帮助器.但是输入字段使用相同的ID属性(在HTML中),因此单击另一个表单中字段的标签将在第一个表单中选择相同的输入.

是否有解决方案除了通过以下方式设置所有属性:for =>"title _#{item.id}"和:id =>"title _#{item.id}"?

使用Rails 3.0.9

ruby-on-rails form-for

23
推荐指数
2
解决办法
7833
查看次数