다시 한번 form_for에 문제가 있습니다.레일, form_for 및 클립
나는 활동 모델을 가지고, 그것은 같이 :
class Activity < ActiveRecord::Base
has_many :acdocs, dependent: :destroy, autosave: true
accepts_nested_attributes_for :acdocs,
reject_if: proc { |attributes| attributes['descr'].blank?},
allow_destroy: true
end
그리고 나는 acdoc 모델을 가지고있다. acdoc은 활동 문서의 줄임말입니다. o 어딘가에서 "document"라는 단어를 사용하면 자바 스크립트에 문제가 생길 수 있습니다. 활동이 많은 acdocs을 가질 수 있기 때문에
, 나는 form_for를 사용하려면 처리하는 :
def new
@activity = Activity.new
@activity.acdocs.build
end
def create
@activity = Activity.new(activity_params)
if params[:add_item]
@activity.acdocs.build
render :action => 'new'
else
respond_to do |format|
if @activity.save
format.html { redirect_to @activity, notice: 'Activity was successfully created.' }
format.json { render action: 'show', status: :created, location: @activity }
else
format.html { render action: 'new' }
format.json { render json: @activity.errors, status: :unprocessable_entity }
end
end
end
end
def update
if params[:add_item]
unless params[:activity][:acdocs_attributes].blank?
for attribute in params[:activity][:acdocs_attributes].permit!
@activity.acdocs.build(attribute.last.except(:_destroy)) unless attribute.last.has_key?(:id)
end
end
@activity.acdocs.build
render :action => 'edit'
else
respond_to do |format|
if @activity.update(activity_params)
format.html { redirect_to @activity, notice: 'Activity was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @activity.errors, status: :unprocessable_entity }
end
end
end
end
이 설정, 좀 일 :
<%= f.fields_for :acdocs do |acdocs| %>
<div>
<%= acdocs.label :descr" %>
<%= acdocs.text_field :descr %>
<%= acdocs.label :document %>
<%= acdocs.file_field :document b%>
</div>
<% end %>
<p>
<%= f.submit 'add doc', :name => "add_item" %>
</p>
<div class="actions">
<%= f.submit %>
</div>
그리고 컨트롤러에 대한
, 내가 이것을 사용 . "add_item"필드 묶음을 선택하고 파일을 선택하면 모든 파일이 업로드됩니다.나를위한 문제는 사용자가 add_item을 누르거나 저장된 활동을 편집 할 때 양식이 열리는 것입니다. itens로 파일 첨부 버튼과 텍스트 : "첨부 파일 없음". 이 파일은 앱에 저장된 파일이 아닌 파일 (자신의 컴퓨터에서)이 업로드되고 있음을 사용자에게 알리기위한 것입니다.하지만 사용자가 업로드 한 파일이 없다고 생각하게 만듭니다.
어떻게 그곳에 파일이 있다고 말하는 텍스트를 넣을 수 있습니까?
또한이 형식에 대한 비슷한 문제가있는 첫 번째 시간이 아닙니다. 때로는 개체가 이미 데이터베이스에있는 경우 무언가를 보여주고 싶습니다. 또는 새로운 것이면. (기존 아이템의 경우 "파괴 체크 박스"를 표시하고 새 아이템의 경우에는 숨기기)
어떻게하면됩니까?
것은 내가 form_for 안에 있습니다. 그래서, 문서 객체는 내부에 있고, 올바른 객체를 배치합니다. 폼 빌더 도우미입니다 : 정의되지 않은 메소드'exists? ' –
Techmago