associations - The better way to pass the foreign_key value to the Rails controller -


it's been week since i've began dig deeper in forms , associations , hashes , symbols... seems cannot solve puzzle without .

i working on project displaying different galleries content . basic idea when user sees names of galleries (names links ) able click on chosen one. images ,that belong gallery , displayed . on bottom there should link "add image in gallery" .

my models :

 class gallery < activerecord::base attr_accessible  :name has_many :pictures   end   class picture < activerecord::base  attr_accessible  :image  belongs_to :gallery  end 

i have created index on gallery_id 'pictures' table .

my big problem appears here , how pass gallery_id controller's action 'new' . i've seen in "agile web development rails" :
<%= link_to 'add picture here...',new_picture_path(:gallery_id=>@gallery.id) %>

as seems in case foreign_key :gallery_id exposed in url bar of browser . second problem :gallery_id available controller 'new' function , "disappears" 'create' function (causing error " couldn't find gallery without id ") . problem gone when add hidden field in _form pictures , in case :

<%= form_for(@picture)   |f| %> <div class="field">        <%= f.hidden_field :gallery_id , :value=>params[:gallery_id] %> <%= f.label :image %><br /> <%= f.file_field :image %> </div>  <div class="actions"> <%= f.submit "create" %> </div> <% end %> 

here definitions in 'pictures' controller :

def new @gallery=gallery.find(params[:gallery_id]) @picture=@gallery.pictures.build   end   def create    @gallery = gallery.find(params[:gallery_id])      @picture = @gallery.pictures.new(params[:picture])   if @picture.save      redirect_to(@picture, :notice => 'picture created.')   else          redirect_to(galleries ,:notice => 'picture not created.')   end   end 

and finaly link_to definition in show.html.erb galleries:

<% picture in selpics(@gallery) %>  <div id= "thumb" >   <%= image_tag picture.image %>  </div> <% end %>   <%= link_to 'add picture here...',new_picture_path(:gallery_id=>@gallery.id) %> 

here debug output before submitting image : --- !map:activesupport::hashwithindifferentaccess gallery_id: "6" action: new controller: pictures

and after submitting 'create' button (with exception raised ) :

{"utf8"=>"✓",  "authenticity_token"=>"igi4mfdgbavbsho7r2pxiik8fgjkghdpbi117tcfxmc=",  "picture"=>{"image"=>"wilsonblx.png"},  "commit"=>"create"} 

as see , there nothing "gallery_id" in "pictures" hash .

summarizing questions :

  1. is there way pass foreign_key without hidden_field ?

  2. could hide somehow passing foreign key form showing in url bar ?

  3. is there alternative on passing arguments using 'link_to' ?

thank .

you may want consider reading rails guide on nested resources:

http://guides.rubyonrails.org/routing.html#nested-resources

in nutshell:

routes.rb

resources :galleries   resources :pictures end # generates routes: /galleries/:gallery_id/pictures 

pictures_controller.rb

def new   @gallery = gallery.find(params[:gallery_id])   @picture = picture.new end def create   @gallery = gallery.find(params[:gallery_id]) # gallery_id passed in url   @picture = @gallery.build(params[:picture])   if @picture.save   # success   else   # fail   end end 

pictures/new.html.erb

<%= form_for [@gallery, @picture] |f| %>   <div class="field">      <%= f.hidden_field :gallery_id , :value=>params[:gallery_id] %>     <%= f.label :image %><br />     <%= f.file_field :image %>   </div>    <div class="actions">     <%= f.submit "create" %>   </div>  <% end %> 

ok, gallery_id still passed through url, don't see wrong that. have pass somewhere, right? have 3 sane choices on pass it: hidden field, querystring parameter, or tucked away inside url (nested resource). of 3, latter imho cleanest method.

if want make things easier on yourself, highly recommend looking jose valim's inherited resources gem takes care of lot of boilerplate nastiness you:

https://github.com/josevalim/inherited_resources


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -