0

现在我有 2 个提交评论的表单。

表格类型 A

<%=form_for(([@community, @comment]), :remote => true, :class => 'form' ) do |f| %>
    <%= f.text_field :body, :id => "body", :class => "chat" %> 
    <button type="submit" class="btn">submit</button>
<% end %>

表格类型 B

<%=form_for(([@user, @comment]), :remote => true, :class => 'form' ) do |f| %>
    <%= f.text_field :body, :id => "body", :class => "chat" %> 
    <button type="submit" class="btn">submit</button>
<% end %>

现在,我想要一个link_to按钮,如果用户点击它,它的功能与那些表单一样。

当用户点击链接时,@comment将自动填充,如下所示。
表格类型 A
@comment = "this is for community"
表格类型 B
@comment = "this is for user"

我怎样才能做到这一点?据我了解我的情况。
表单是put类型,然后link_toget类型,所以不可能重用表单的事务。

4

1 回答 1

1

不确定“表单事务”是什么意思,但如果您询问是否可以通过单个按钮或链接创建/修改数据,而不是答案是Yes,这是可能的。

  1. 您实际上可以put使用link_toin rails ( {:method => :put}( http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to )

  2. 如果您想要一个按钮来执行此操作,您应该结帐button_tohttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

最好使用button_to.

于 2013-08-03T04:33:01.377 回答