开发者

Rails multiple models (nested) form not showing the child model after validation error

开发者 https://www.devze.com 2023-03-22 20:52 出处:网络
I used multiple model from in rails, it fails to work properly in a sinario <%= form_for [@listing] do |f| %>

I used multiple model from in rails, it fails to work properly in a sinario

  <%= form_for [@listing] do |f| %>

    <%= f.fields_for :photos do |ph| %>

      <%= ph.file_field :data %>

    <% end %>

  <% end %>

while validation,i didn't upload a image(image is optional one) and submit the form. if the first model has any error then the form shows error message, at the time the form fails to show the input field of second form(<%= ph.file_field开发者_开发问答 :data %>)


I assume you

render "new"

in case of an error. When you render the new view, the corresponding controller action is not called! So your

@listing = Listing.new(params[:listing])

will not have a photo associated with it. Do

@listing.build_photo # or @listing.photos.build (for has_many association)

before rendering new.


def new

@listing = Listing.new    

1.times do

  @listing.photos.build

end

respond_to do |format|

  format.html # new.html.erb

  format.xml  { render :xml => @listing }

end

end

it helps in development only. In production it doesn't helps. In online even when click new listing, it doesn't show the photos fields.

0

精彩评论

暂无评论...
验证码 换一张
取 消