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

半城伤御伤魂 提交于 2019-11-27 15:20:44

You may want to consider reading the Rails Guide on nested resources:

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

In a nutshell:

routes.rb

resources :galleries do
  resources :pictures do
end
# Generates the 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 is passed in the URL
  @picture = @gallery.build(params[:picture])
  if @picture.save
  # success
  else
  # fail
  end
end

pictures/new.html.erb

<%= form_for [@gallery, @picture] do |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, so the gallery_id is still passed through the URL, but I don't really see anything wrong with that. You have to pass it somewhere, right? You really only have 3 sane choices on where to pass it: a hidden field, as a querystring parameter, or tucked away inside the URL (nested resource). Of the 3, the latter is IMHO the cleanest method.

If you want to make things even easier on yourself, I highly recommend looking into Jose Valim's Inherited Resources gem that takes care of a lot of this boilerplate nastiness for you:

https://github.com/josevalim/inherited_resources

You need not use the numeric ID's in your RESTful routes. Look at permalink_fu, and use the :permalink field rather than the :id to refer to each gallery resource.

/galleries/louvre
/galleries/moma/382

And

... new_picture_path(:gallery_id => @gallery.permalink)

The key here is using a symbolic, unique key that's not the ID, permalink's are pretty good for that.

You can choose to pass the permalink in as :id and update your controller actions to expect that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!