2017-02-14 4 views
1

레일즈 5로 간단한 폼을 사용하는 방법을 알아 내려고 노력하고있어, 내 폼에 전체 오류 메시지를 표시 할 수 있습니다. - 난 아무것도하지 않는 대신 문제 목록의레일즈 - 간단한 폼 부트 스트랩 - 에러의 전체 텍스트보기 - (또는 어디서나)

Please review the problems below: 

을하거나 양식 필드를 강조 : 내가 할 모든라는 메시지와 함께 상단에 박스입니다.

나는 this post의 조언을 따라했으며 내 이니셜 라이저에서 'full_error'오류로 모든 참조를 바꿨지 만 상위 양식의 문제 만 해결합니다. 중첩 된 필드는 여전히 불완전한 오류 메시지를받습니다. 누구든지 간단한 형태로 오류를 식별하는 방법을 알고 있습니까?

# Use this setup block to configure all options available in SimpleForm. 
SimpleForm.setup do |config| 
    config.error_notification_class = 'alert alert-danger' 
    config.button_class = 'btn btn-green' 
    config.boolean_label_class = nil 

    config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.use :placeholder 
    b.optional :maxlength 
    b.optional :pattern 
    b.optional :min_max 
    b.optional :readonly 
    b.use :label, class: 'control-label' 

    b.use :input, class: 'form-control' 
    b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' } 
    b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 

    config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.use :placeholder 
    b.optional :maxlength 
    b.optional :readonly 
    b.use :label, class: 'control-label' 

    b.use :input 
    b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' } 
    b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 

    config.wrappers :vertical_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.optional :readonly 

    b.wrapper tag: 'div', class: 'checkbox' do |ba| 
     ba.use :label_input 
    end 

    b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' } 
    b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 

    config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.optional :readonly 
    b.use :label, class: 'control-label' 
    b.use :input 
    b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' } 
    b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 

    config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.use :placeholder 
    b.optional :maxlength 
    b.optional :pattern 
    b.optional :min_max 
    b.optional :readonly 
    b.use :label, class: 'col-sm-3 control-label' 

    b.wrapper tag: 'div', class: 'col-sm-9' do |ba| 
     ba.use :input, class: 'form-control' 
     ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' } 
     ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 
    end 

    config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.use :placeholder 
    b.optional :maxlength 
    b.optional :readonly 
    b.use :label, class: 'col-sm-3 control-label' 

    b.wrapper tag: 'div', class: 'col-sm-9' do |ba| 
     ba.use :input 
     ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' } 
     ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 
    end 

    config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.optional :readonly 

    b.wrapper tag: 'div', class: 'col-sm-offset-3 col-sm-9' do |wr| 
     wr.wrapper tag: 'div', class: 'checkbox' do |ba| 
     ba.use :label_input 
     end 

     wr.use :full_error, wrap_with: { tag: 'span', class: 'help-block' } 
     wr.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 
    end 

    config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.optional :readonly 

    b.use :label, class: 'col-sm-3 control-label' 

    b.wrapper tag: 'div', class: 'col-sm-9' do |ba| 
     ba.use :input 
     ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' } 
     ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 
    end 

    config.wrappers :inline_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.use :placeholder 
    b.optional :maxlength 
    b.optional :pattern 
    b.optional :min_max 
    b.optional :readonly 
    b.use :label, class: 'sr-only' 

    b.use :input, class: 'form-control' 
    b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' } 
    b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 

    config.wrappers :multi_select, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.optional :readonly 
    b.use :label, class: 'control-label' 
    b.wrapper tag: 'div', class: 'form-inline' do |ba| 
     ba.use :input, class: 'form-control' 
     ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' } 
     ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 
    end 
    # Wrappers for forms and inputs using the Bootstrap toolkit. 
    # Check the Bootstrap docs (http://getbootstrap.com) 
    # to learn about the different styles for forms and inputs, 
    # buttons and other elements. 
    config.default_wrapper = :vertical_form 
    config.wrapper_mappings = { 
    check_boxes: :vertical_radio_and_checkboxes, 
    radio_buttons: :vertical_radio_and_checkboxes, 
    file: :vertical_file_input, 
    boolean: :vertical_boolean, 
    datetime: :multi_select, 
    date: :multi_select, 
    time: :multi_select 
    } 
end 

난 당신의 코드에서 발견 된 첫 번째 실수는, 당신의 중첩 된 필드에 같은 변수 f에서 촬영 한되어

<%= simple_form_for(@proposal) do |f| %> 
    <%= f.error_notification %> 

<% @proposal.errors.full_messages.each do |msg| %> 
    <%= li= msg %> 
<% end %> 



    <div class="form-inputs" style="margin-bottom: 50px"> 
    <!-- General Organisation details --> 
    <div class="row"> 
     <div class="col-md-12"> 
     <%= f.input :title, :label => "Title" %> 

     </div> 
    </div> 
    <div class="row"> 
     <div class="col-md-12"> 
     <%= f.input :byline, :label => "Tagline" %> 
     </div> 
    </div> 

    <div class="row"> 
     <div class="col-md-12"> 
     <%= f.input :description, as: :text, :label => "Outline your proposal", input_html: { rows: 15 } %> 
     </div> 
    </div> 




    <!-- Package: :ethics considerations --> 
    <div class="row"> 
    <div class="col-md-12"> 
     <div class="form_title">Research Ethics</div> 
    </div> 
    </div> 

    <div class="row"> 
    <div class="col-md-12"> 

    <%= f.simple_fields_for :ethics do |f| %> 
     <%= f.error_notification %> 
     <%= render 'package/ethics/ethics_fields', f: f %> 

    <% end %> 

    <%= link_to_add_association 'Add another ethics issue', f, :ethics, partial: 'package/ethics/ethics_fields' %> 
    </div> 
    </div> 


    </div> 

    <div class="row"> 
    <div class="col-md-10 col-md-offset-1" style="margin-top: 50px"> 
     <div class="form-actions"> 
     <%= f.button :submit %> 
     </div> 
    </div> 
    </div> 
<% end %> 

NESTED FIELDS

<div class="nested-fields"> 

    <%# @proposal.ethics.errors.full_messages.each do |e| %> 
     <li><%#= e %></li> 
    <%# end %> 
    <div class="form-inputs"> 
     <div class="row"> 
      <div class="col-md-12 "> 
      <%= f.select :category, [ "Risk of harm", "Informed consent", "Anonymity and Confidentiality", "Deceptive practices", "Right to withdraw"], { label: "Principle" }, id: "main_category" %> 
      <%= f.select :subcategory, [], {}, id: "sub_category", disabled: true %> 
      <%= f.input :consideration, as: :text, :label => "Identify the ethics considerations?", :input_html => {:rows => 8} %> 
      <%= f.input :plan, as: :text, :label => "How will these considerations be managed?", :input_html => {:rows => 8} %> 
     </div> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="col-md-6" style="margin-top: 20px; margin-bottom: 50px"> 
     <%= link_to_remove_association 'Remove this ethical issue', f %> 
     </div> 

    </div> 
</div> 
+0

양식을 붙여주십시오. – Sravan

+0

위 양식을 추가하면 – Mel

답변

0

것 같습니다.

이 :

<% @proposal.errors.full_messages.each do |msg| %> 
    <%= li= msg %> 
<% end %> 

더 다음과 같아야합니다

<% if @proposal.errors.any? %> 
<ul> 
<% @proposal.errors.full_messages.each do |msg| %> 
    <li><%= msg %></li> 
</ul> 
<% end %> 

을 당신은 그래서 당신은 그것을 정확하게 설정해야합니다 코쿤을 사용하는 추측하고있어하지만 잡았다 경우가있다 사용 레일 5.

당신은 당신의 모델에서 다음과 같이해야합니다

:

class Ethic < ApplicationRecord 
    belongs_to :proposal, inverse_of: :ethics 
... 

주 레일 (5)의 버그 라운드 작동 절 inverse_of has_many ....

accepts_nested_attributes_for 절을 올바르게 설정하면 @ proposal.errors에 양식의 모든 오류가 포함되어야한다고 생각합니다.

+0

당신은 윤리적 모델에 반대의 것이 옳습니다. 그것이 오류를 없애 버렸지 만, 윤리적 속성에 대한 오류를 어떻게 인쇄합니까? 윤리 형식 중첩 된 부분? 그들은 오류의 제안 목록과 함께 양식의 맨 위에 나열하지만, ​​내가 윤리 속성으로 스크롤 할 때 나타나는 모든 것은 빨간색 상자가 아래의 문제를 검토 한 다음 목록이 없다는 것을 말합니다. – Mel

+0

부모와 자식 오류를 분명한 방법으로 생각하지 않습니다. simple_fields_for 블록에서 "<% = f.error_notification %>"행을 제거하고 simple_form을 사용하여 오류 메시지를 생성합니다. BTW는 Sravan이 simple_fields_for 블록에 대해 다른 변수가 필요하다고 말했다. –

+0

하지만 실제로 f : f와 잘 작동합니다. - 필자가 필요로하는지 확신 할 수 없습니다. 속성은 f (제안서와 동일)를 사용하여 생성됩니다. – Mel

0

FORM 부모 파라미터와 함께 consfused 될 수있는 매개 변수 10 TER f

지금

될 것 중첩

,

<%= f.simple_fields_for :ethics do |ff| %> 
     <%= ff.error_notification %> 
     <%= render 'package/ethics/ethics_fields', f: f %> 
<% end %> 

형태

<%= simple_form_for(@proposal) do |f| %> 
    <%= f.error_notification %> 

<% @proposal.errors.full_messages.each do |msg| %> 
    <%= li= msg %> 
<% end %> 



    <div class="form-inputs" style="margin-bottom: 50px"> 
    <!-- General Organisation details --> 
    <div class="row"> 
     <div class="col-md-12"> 
     <%= f.input :title, :label => "Title" %> 

     </div> 
    </div> 
    <div class="row"> 
     <div class="col-md-12"> 
     <%= f.input :byline, :label => "Tagline" %> 
     </div> 
    </div> 

    <div class="row"> 
     <div class="col-md-12"> 
     <%= f.input :description, as: :text, :label => "Outline your proposal", input_html: { rows: 15 } %> 
     </div> 
    </div> 




    <!-- Package: :ethics considerations --> 
    <div class="row"> 
    <div class="col-md-12"> 
     <div class="form_title">Research Ethics</div> 
    </div> 
    </div> 

    <div class="row"> 
    <div class="col-md-12"> 

    <%= f.simple_fields_for :ethics do |ff| %> 
     <%= ff.error_notification %> 
     <%= render 'package/ethics/ethics_fields', f: ff %> 

    <% end %> 

    <%= link_to_add_association 'Add another ethics issue', f, :ethics, partial: 'package/ethics/ethics_fields' %> 
    </div> 
    </div> 


    </div> 

    <div class="row"> 
    <div class="col-md-10 col-md-offset-1" style="margin-top: 50px"> 
     <div class="form-actions"> 
     <%= f.button :submit %> 
     </div> 
    </div> 
    </div> 
<% end %> 




<%= f.simple_fields_for :ethics do |ff| %> 
    <%= ff.error_notification %> 
    <%= render 'package/ethics/ethics_fields', f: ff %> 
    <% @proposal.ethics.errors.full_messages.each do |msg| %> 
     <%= li= msg %> 
    <% end %> 
<% end %> 

편집 : 위의 문제가 해결되지 않는 경우에도,

에서 ethics object의 오류를 가져 오십시오. 당신이 HAML 및 ERB이 오류 표시 코드에 혼합지고처럼변수

그래서,

<%= f.simple_fields_for :ethics do |ff| %> 
    <%= ff.error_notification %> 
    <% @proposal.ethics.errors.full_messages.each do |msg| %> 
    <%= li= msg %> 
    <% end %> 
    <%= render 'package/ethics/ethics_fields', f: f %> 
<% end %> 
+0

pls입니다.이 대답을 확인하고 언제든지 문의하십시오. – Sravan

+0

그러나 그러한 경우라면 모든 윤리 분야가 작동하지 않을 것입니다. f.input은 입력 요소에 대해 부분적인 윤리 중첩 필드에서 올바르게 작동합니다. 그 형식으로 오류를 표시하는 방법을 찾을 수 없습니다. – Mel

+0

윤리 필드는 작동하지만 오류 메시지가 표시되지 않을 수 있습니다. 대답의 마지막 부분을 확인하십시오. – Sravan

0

오류 메시지를 필드에 직접 표시 할 수도 있습니다.

<%= f.input :title, :label => "Title", id:"title" %> 
<%= f.error :title, :id => "title_error" %> 

제목 필드 옆에 오류 메시지가 표시됩니다. <ul>을 양식 위에 표시하거나 양식 위에 <ul>과 함께 표시하는 대신 양식의 각 필드에 f.error 메서드를 추가 할 수 있습니다.

nested_form_fields의 경우 기본 모델에 validates_associated :assoc_model_name을 추가하십시오. 관련 모델에 대한 유효성 검사도 실행됩니다.

+0

이것은 simple_form gem을 사용하여 자동으로 발생합니다. 관련 필드에 필요한 경우에도 오류가 포함됩니다. –