Rails: Restoring contents of non-model form that uses form_tag

后端 未结 2 991
忘了有多久
忘了有多久 2021-01-19 00:28

I\'m making good progress with my first Rails app (using Rails 3). The MVC interaction is all going fine, but I\'m having difficulty with a form that doesn\'t relate directl

相关标签:
2条回答
  • 2021-01-19 01:02

    If your field has a default, you will want to set it from the "show" action for the form:

    # GET
    def show_form
      params[:key] = 'default'
    end
    
    # POST
    def validate_form_and_act
      # Don't set it here to reuse what the user passed.
    end
    

    or directly on the template (less good because uses an || every time and adds more controller data to view):

     <%= text_field_tag 'name', params[:key] || 'default' %>
    
    0 讨论(0)
  • 2021-01-19 01:03

    The second arg to text_field_tag is the value to fill in the field with. Try something like this:

      <%= text_field_tag "separator", params[:separator], :maxlength => 1 %>
    
    0 讨论(0)
提交回复
热议问题