attachment_fu 및 multipart form_for
-
20-09-2019 - |
문제
우. 내 첫 번째 질문.
나는 내 형태의 구성에서 매우 기본적인 것을 간과하고 있다고 느낀다. attachment_fu를 사용하고 있으며 파일 데이터 외에이 양식을 전달할 수 없습니다. 사용자는 has_many 프로필과 프로필 has_many 문서입니다.
내 양식은 다음과 같습니다.
<%= error_messages_for :document %>
<% form_for([@user, @profile, @document], :html => {:multipart => true }) do |f| -%>
<p>
<label for="document">Upload A document:</label>
<%= f.file_field :uploaded_data %>
</p>
<%= f.label :description%>
<%= f.text_field :description%>
<p>
<%= submit_tag 'Upload' %>
</p>
<% end -%>
그리고 여기 컨트롤러가 있습니다.
before_filter :require_user, :get_profile
def new
@document = @profile.documents.build
end
def create
@document = @profile.documents.build(params[:document])
if @document.save
flash[:notice] = 'Your document was successfully created.'
redirect_to document_url(@document)
else
render :action => :new
end
end
private
def get_profile
@user = current_user
@profile = @user.profiles.find(params[:profile_id])
end
로그는 게시 된 모든 이미지 데이터가 표시되지만 설명을 전달할 수는 없습니다. 또는 더 중요한 것은 내 문서 모델의 외래 키 인 Profile_id를 전달할 수 없습니다. 나는 밤새도록 갇혀 있었고 오늘 아침에 신선한 것을 생각할 수 없었다. 어떤 도움이든 좋을 것입니다.
해결책
용 profile_id
다음과 같은 것이 필요합니다.
<%= f.hidden_field :profile_id %>
컨트롤러에서 사용할 수 있습니다 params[:document][:profile_id]
필요한 경우. 코드가 무엇을하고 있는지 추측하려고하지만 params[:profile_id]
이 컨트롤러에 대한 모든 경로에서 이미 설정되어 있습니다.
왜 당신이 설명을 위해 아무것도 보지 못하는지 잘 모르겠습니다. 컨트롤러로 들어와야합니다 params[:document][:description]
.
제휴하지 않습니다 StackOverflow