Rails 3.2.13 undefined method `photos_path' with nested resources with CarrierWave -
i hoping can give me insights on how nested resources , more how tie carrierwave.
i'm getting error undefined method 'photos_path' , bit stuck on how go getting work.
i want able have following urls
create new photo
site.com/animal/dog/photos/new
show photo
site.com/animal/dog/photos/my-dog-in-the-park
should using nested form? appreciated.
my routes
root to: "home#index" resources :animal resources :photos end my home view
<%= link_to "add dog photo", new_animal_photo_path(animal_permalink: "dog") %> my _form partial
<%= form_for [@animal, @photo], :html => {:multipart => true} |f| %> <%= f.hidden_field :animal_permalink %> <p> <%= f.label :title %><br /> <%= f.text_field :title %> </p> <p> <%= f.file_field :image %> </p> <p> </p> <p><%= f.submit %></p> <% end %> my photo model
class photo < activerecord::base include activemodel::forbiddenattributesprotection before_create :set_permalink before_update :set_permalink belongs_to :dog mount_uploader :image, photouploader def set_permalink self.permalink = title.parameterize end def to_param permalink.parameterize end end my animal model
class animal < activerecord::base include activemodel::forbiddenattributesprotection has_many :photos scope :dog, where(name: "dog") def to_param permalink.parameterize end end my photocontroller
class photoscontroller < applicationcontroller def show @photo = photo.find(params[:id]) end def new @animal = animal.find_by_permalink(params[:id]) @photo = photo.new end def create @photo = photo.new(photo_params) if @photo.save flash[:notice] = "successfully created photo." redirect_to root_url else render :action => 'new' end end def edit @photo = photo.find(params[:id]) end def update @photo = photo.find(params[:id]) if @photo.update_attributes(photo_params) flash[:notice] = "successfully updated photo." redirect_to root_url else render :action => 'edit' end end def destroy @photo = photo.find(params[:id]) @photo.destroy flash[:notice] = "successfully destroyed photo." redirect_to root_url end private def photo_params params.require(:photo).permit(:title, :image, :animal_id) end end thanks taking look. appreciated.
i've made few adjustments code.
notice i've added "s" animal
root to: "home#index" resources :animals resources :photos end notice i've removed "s" animals. i've changed animal_permalink id because default expected nested resource. also, note that, in photos controller new method check "id" param, not animal_permalink param.
new_animal_photo_path(id: "dog") in _form. set value of animal_permalink
<%= f.hidden_field :animal_permalink, value: @animal.permalink %> this assumes photo able recognize animal_permalink attribute , you've defined animal.permalink method/attribute. i'm not familiar permalink approach, may have fiddle around bit, should steer in right direction (i set :dog_id/:animal_id attribute).
let me know how goes.
Comments
Post a Comment