【问题标题】:Rails model/form layout questionRails 模型/表单布局问题
【发布时间】:2011-05-21 20:22:39
【问题描述】:

我正在 Rails 中创建一个食谱管理器(谁不是他们的第一个应用程序?),这是我的布局:

Ingredient belongs to Recipe
Recipe has many Ingredients

制作反映这种关系的表格的最佳方法是什么?我在想一个输入,当一个输入被填满时,会创建另一个输入,所以成分表的末尾总是有“一个更多”。

创建 UI 后,模型和控制器的结构会是什么样子?现在我有脚手架控制器create 方法:

  def create
    @recipe = Recipe.new(params[:recipe])

    respond_to do |format|
      if @recipe.save
        format.html { redirect_to(recipes_path, :notice => 'You made a new recipe!') }
        format.xml  { render :xml => @recipe, :status => :created, :location => @recipe }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @recipe.errors, :status => :unprocessable_entity }
      end
    end
  end

params[:recipe] 是否应该只是一个嵌套更深的对象/哈希/字典,包含一系列成分或其他东西?

感谢您在此提供的任何指导。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 model controller has-many


    【解决方案1】:

    你应该在这里使用accepts_nested_attributes

    一些链接:

    API:
    http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for
    截屏视频:
    http://railscasts.com/episodes/196-nested-model-form-part-1
    http://railscasts.com/episodes/197-nested-model-form-part-2

    所以你的模型看起来像这样

    class Recipie  < ActiveRecord::Base 
      has_many :ingredients
      accepts_nested_attributes_for :ingridients, :allow_destroy => true
    end
    

    观看次数:

    <%= form_for @recipe do |f| %>
      ... # reciepe fields
      <%= f.fields_for :ingridients do |i| %>
        ... # your ingridients forms
      <% end %>
      ...
    <% end %>
    

    和控制器

    def create
      @recipe = Recipe.new(params[:recipe])
      @recipe.save # some save processing
    end
    

    【讨论】:

    • 啊哈,感谢您向我指出这一点。如果您不完全确定要查找的内容,则很难找到资源。这看起来像是专门针对这个问题的。谢谢!
    【解决方案2】:

    只需用逗号分隔添加成分。

    这可以是text_field_tag,因为您需要对其进行解析并保存每个单词,每个单词之间用逗号隔开,并在保存前使用。

    class Recipie  < ActiveRecord::Base 
    
        has_many :ingredients
    
        before_save :add_ingredients 
    
        attr_accessor :ingredients_to_parse #this will be the text_field_tag
    
        def add_ingredients
            #create an array of ingredients from #ingredients_to_parse
            #then loop through that array i.e. you have your ingredients_array   
            ingredients_array.each do 
                Ingredient.create(:recipe => self, :other_params => 'stuff')
            end
            #there are a lot of ways, I just used create to show you how to add it
        end
    end
    

    那么在你的表单中只需要text_field_tag

    <%= form_for(@recipe) do |f| %>
        <% f.text_field :name %>
        <% text_field_tag :ingredients_to_parse %>
        <%= f.submit %>
    <% end %>
    

    然后您可以添加 Javascript,以便每次在 text_field_tag 中添加逗号时,您都可以使用一些 js 来制作如此花哨的东西。

    这种方式在服务器速度慢、js 运行不正常等情况下也能正常工作。让 HTML 版本也先运行总是一个好主意。

    祝你好运,如果您有任何疑问/问题,请告诉我。

    【讨论】:

    • 啊,所以你的策略就是让用户提交一个字符串(恰好有很多成分),然后在它到达服务器端后将其解析出来。有趣的是,我会考虑一下,看看它如何融入应用程序的设计。谢谢!
    • 是的,就 HTML 而言,工作量要少得多;然而,js 需要做更多的工作,但之前已经完成了,所以当时有资源。例如谷歌'javascript 添加标签'。一开始甚至根本不需要 ajax。如果您需要有关 js 的帮助,请告诉我。
    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多