你怎么能有一个简单形式的隐藏领域?
以下代码:
= 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
我有一个关于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.
谢谢!
我想在以下位置显示标签form_for:
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
Run Code Online (Sandbox Code Playgroud)
这会生成标签"Name",但我希望它是"你的名字".我该怎么改变它?
我有一个以下列方式编写的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
我希望用户只使用一个连接到用户会话的订单.所以我为订单设置了奇异的资源
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_path在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]).如果接收到错误的参数,我将得到一个找不到方法的错误.
处理这个问题的最佳方法是什么?
这些是我唯一的选择吗?
示例如下:
配置/ 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:
<%= 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有效.
在同一页面中有这么多表单是无效的HTML.
如何通过rails 3.1中的form_for方法助手更改id autogenerate?
问题
form_for帮助程序错误地确定了命名空间内嵌套资源的路径.有问题的模型是:Forum :: Thread和Forum :: 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中概述的行
通常,使用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
我想在同一页面中对同一模型多次使用form_for帮助器.但是输入字段使用相同的ID属性(在HTML中),因此单击另一个表单中字段的标签将在第一个表单中选择相同的输入.
是否有解决方案除了通过以下方式设置所有属性:for =>"title _#{item.id}"和:id =>"title _#{item.id}"?
使用Rails 3.0.9
form-for ×10
ruby ×2
hidden-field ×1
namespaces ×1
resources ×1
router ×1
simple-form ×1
singular ×1
textfield ×1