Attachment_fu 和多部分 form_for
呜。我的第一个问题。
我有一种感觉,我忽略了表单构建中一些非常基本的东西。我正在使用 Attachment_fu,但无法让此表单传递除文件数据之外的任何内容。一个用户有多个配置文件,一个配置文件有多个文档。
我的表单如下所示:
<%= 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,它是我的文档模型中的外键。我整夜都在思考这个问题,今天早上想不出任何新鲜的东西。任何帮助都会很棒。
Woo. My first question.
I have a feeling I'm overlooking something pretty basic in the construction of my form. I'm using attachment_fu and can't get this form to pass anything besides the file data. A user has_many profiles and a profile has_many documents.
My form looks like this:
<%= 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 -%>
And here's the controller:
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
The logs show all the image data getting posted, but I cannot pass the description or, more importantly, the profile_id, which is the foreign key in my document model. I was stuck on this all night, and can't think of anything fresh this morning. Any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于
profile_id
您将需要类似的内容:如果需要,您将在控制器中使用
params[:document][:profile_id]
获得该信息。尽管试图猜测您的代码在做什么,但我怀疑
params[:profile_id]
已经从您到达此控制器的任何路线设置。我不知道为什么你没有看到任何描述。它应该作为
params[:document][:description]
进入您的控制器。For the
profile_id
you will need something like:Which in your controller you will get at using
params[:document][:profile_id]
if needed.Although from trying to guess at what your code is doing, I suspect that
params[:profile_id]
is already set from whatever route got you to this controller.I am not sure why you aren't seeing anything for the description. It should be coming into your controller as
params[:document][:description]
.