Rails 3 create form for Polymorphic Association's child model
My application's route.rb file contains this namespace:
namespace :admin do
resources :index
resources :posts do
resources :images
end
resources :statics
end
I used Polymorphic Association
Post model:has_many :images, :as => :imageable
Image model: belongs_to :imageable, :polymorphic => true
And I created necessary fields in migration.
When I call @post.images,开发者_JS百科 no error message, I think this relation works fine.
I think, my problem is in the image uploader form, I use code like this: (post/edit action)[there is post edit form]
<% form_tag admin_post_images_url( @post ) , :multipart => true do %>
<%= file_field :imageable, :photo, :size => 40 %>
<input type="submit" name="send" value="upload!" />
<% end %>
The image is successful uploaded, but imageadble_id and _type doesnt filled. Something wrong (I think with admin_post_images_url( @post ) part), what is the problem?
update:
When I add manual to create imageadble_id and type, and listing its work, so assotiation is works good.Try something like this:
<% form_for([:admin, @post, @image], :multipart => true) do %>
This may not work exactly but should get you started down the right path. No matter what, you need "form_for" if you're working with a model, not "form_tag".
精彩评论