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

后端 未结 2 990
忘了有多久
忘了有多久 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' %>
    

提交回复
热议问题