for가없는 SimpleForm (비 모델 양식)
모델없이 단순 양식 (기준 : Plataformatec)을 사용할 수 있습니까?
https://github.com/plataformatec/simple_form
:symbol
첫 번째 인수로 사용할 수 있습니다 .
<%= simple_form_for :user, url: users_path do |f| %>
<%= f.input :name, as: :string %>
...
<% end %>
다음과 같이 출력됩니다.
<form novalidate="novalidate" class="simple_form user" action="/users" accept-charset="UTF-8" method="post">
...
<div class="input string required user_name">
<label class="string required" for="user_name">
<abbr title="required">*</abbr> Name
</label>
<input class="string required" type="text" name="user[name]" id="user_name" />
</div>
...
</form>
불행히도 simple_form은 모델 사용에 의존합니다. _tag 도우미와 동등한 물건으로 사용하는 것이 좋습니다. 그때는 쉬운 해결 방법이 있습니다.
양식에서 클래스 대신 기호를 사용하고 값을 명시 적으로 전달하여 simple_form이 모델 속성에 액세스하지 못하도록합니다.
<%= simple_form_for :user, :url => '/users' do |f| %>
<%= f.text_field :name, input_html: { value: nil } %>
<% end %>
이 undefined method 'name' for User
오류 를 방지합니다 .
다음과 같이 simple_fields_for를 사용하여 양식 모델 내에서 모델 외부의 필드를 사용할 수 있습니다.
<%= simple_form_for @user do |f| %>
<%= f.input :name %>
<%= simple_fields_for :no_model_fields do |n| %>
<%= n.input :other_field %>
<% end %>
<% end %>
모델을 사용하지 않고 다른 모델에서 다른 종류의 필드를 만들 수 있기 때문에 간단하고 실용적인 솔루션입니다.
:symbol
대신에 @object
를 인수로 받을 수도 있습니다 simple_form_for
.
<%= simple_form_for :email, :url => '/post_email' do |f| %>
<%= f.input :subject, :as => :string %>
<% end %>
다음과 같이 출력됩니다.
<form method="post" class="simple_form email" action="/post_email" accept-charset="UTF-8">
...
<input type="text" size="30" name="email[subject]" id="email_subject">
</form>
다음과 같은 단점에 유의하십시오.
- 자동 모델 유효성 검사를 이용할 수 있습니다.
- 명시 적으로 정의
:url
하고 각 유형을input
위의 모든 메소드는 여전히 "사용자"또는 첫 번째 인수로 전달하는 기호 유전자 서열화 된 양식 데이터를 남깁니다. 그거 짜증나네.
simple_form 스타일 / 장점을 모방 할 객체 / 기호 및 강제 데이터 중첩을 제거 할 수 있습니다.
HAML
예 :
양식보기 :
= form_tag("path/to/action", method: "POST") do
= render "path/to/partial/field", type: "string", required: true, item: "first_name"
field
부분 :
- required_string = required ? "required" : ""
%div{class: "input #{type} #{required_string} #{item}"}
%label{class: "#{type} #{required_string}", for: "#{item}"}
- if required
%abbr{title: "required"}
*
= t("application.#{item}")
%input{name: "#{item}", |
placeholder: t("application.#{item}"), |
type: "#{type}", |
required: required, |
"aria-required" => "#{required}" }
참고 URL : https://stackoverflow.com/questions/5181143/simpleform-without-for-non-model-form
'ProgramingTip' 카테고리의 다른 글
DispatchQueue.main.async와 DispatchQueue.main.sync의 차이점 (0) | 2020.10.26 |
---|---|
Vim- 검색 적으로 끌어 당김 (0) | 2020.10.26 |
jQuery .each ()의 각 반복 사이에 일시 중지를 추가하는 방법은 무엇입니까? (0) | 2020.10.26 |
Windows의 명령 줄 Git (0) | 2020.10.26 |
AngularJS 클라이언트 MVC 패턴? (0) | 2020.10.26 |