【问题标题】:form_for signature for Mongoid::Document subclassMongoid::Document 子类的 form_for 签名
【发布时间】:2013-04-15 16:10:27
【问题描述】:

我有一个 Mongoid 文档:

class Index
  include Mongoid::Document

end

'Index' 的两个子类为:

class PercentileRankIndex < Index

end 

class SocialStockIndex < Index


end

我想创建一个 form_for 说“PercentileRankIndex”,这是我现有的代码签名:

<%= form_for ([@index, @question]) do |f| %>

去路由:'percentile_rank_index_questions_path' 因为@index 是 PercentileRankIndex 的实例。

但是存在的正确路径是:'index_questions'

当 Index 实例使用适当的 _type 属性创建时,我的 form_for 签名应该如何?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongoid form-for


    【解决方案1】:

    我使用 form_tag 来解决这种情况:

    <%= form_tag index_questions_path(:index_id => @index.id) do  %>
    
      <%= label_tag :name, "Name:" %>
      <%= text_field_tag :name %>
    
      <%= label_tag :weight, "Weight" %>
      <%= text_field_tag :weight %>
    
      <div class="actions">
        <%= submit_tag "Save", :method => :post, :class => "btn btn-primary" %>
        <%= link_to 'Back',  index_questions_path, :class => "btn btn-primary" %>
      </div>
    <% end %>
    

    这可能不是最干净的方法,但仍然有效。也希望看到有人提出更好的想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      • 2021-06-27
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多