更好的方式向foreign_key值传递给控制器的Rails [英] The better way to pass the foreign_key value to the Rails controller
问题描述
这是近一个星期,因为我已经开始在形式的深入挖掘,协会,散列符号......但似乎没有你的帮助我解决不了这个难题。
It's been almost a week since I've began to dig deeper in forms , associations , hashes , symbols... But it seems I cannot solve the puzzle without your help .
我工作的一个项目显示不同的画廊的内容。的基本思想是,当用户看到画廊的名称(名称链接)以便能够点击选择之一。然后,属于该库中的所有图像,被显示。在底部应该有一个链接添加图片在这个画廊。
I am working on a project for displaying different galleries content . The basic idea is when the user sees the names of galleries (names are links ) to be able to click on chosen one. Then all the images ,that belong to this gallery , are displayed . On the bottom there should be a link "Add image in this gallery" .
我的模型:
class Gallery < ActiveRecord::Base
attr_accessible :name
has_many :pictures
end
class Picture < ActiveRecord::Base
attr_accessible :image
belongs_to :gallery
end
我建立了gallery_id指数为'图片'表。
I have created index on gallery_id for the 'pictures' table .
我的大问题在这里出现,如何将gallery_id传递给控制器的动作'新'。正如我在敏捷Web开发使用Rails已经看到了它可能是:结果
&LT;%=的link_to添加图片在这里...,new_picture_path(:gallery_id => @ gallery.id)%>
My big problem appears here , how to pass the gallery_id to the controller's action 'new' . As I've seen in "Agile web development with Rails" it could be :
<%= link_to 'Add a picture here...',new_picture_path(:gallery_id=>@gallery.id) %>
,因为它似乎在这种情况下,foreign_key:gallery_id在浏览器的地址栏露出。第二个问题是:gallery_id可用于控制新的功能,但消失为创造功能(导致一个错误不能没有一个ID找到画廊)。
当我在为_form图片添加一个隐藏字段的问题消失了,在我的情况:
As it seems in this case the foreign_key :gallery_id is exposed in the URL bar of the browser . The second problem is that :gallery_id is available for the controller 'new' function , but "disappears" for the 'create' function (causing an error " Couldn't find Gallery without an ID ") . The problem is gone when I add a hidden field in the _form for pictures , in my case :
<%= form_for(@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 %>
下面是我的定义中的图片控制器:
Here are my definitions in the '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 was successfully created.')
else
redirect_to(galleries ,:notice => 'Picture was NOT created.')
end
end
和finaly的的link_to定义为show.html.erb画廊:
And finaly the link_to definition in show.html.erb for galleries:
<% for picture in selpics(@gallery) %>
<div id= "thumb" >
<%= image_tag picture.image %>
</div>
<% end %>
<%= link_to 'Add a picture here...',new_picture_path(:gallery_id=>@gallery.id) %>
下面是提交图像前的调试输出:
!---地图:::的ActiveSupport HashWithIndifferentAccess
gallery_id:6
动作:新
控制器:图片
Here is the debug output before submitting the image : --- !map:ActiveSupport::HashWithIndifferentAccess gallery_id: "6" action: new controller: pictures
和提交创建按钮(带发生异常)后:
and after submitting the 'create' button (with exception raised ) :
{"utf8"=>"✓",
"authenticity_token"=>"IGI4MfDgbavBShO7R2PXIiK8fGjkgHDPbI117tcfxmc=",
"picture"=>{"image"=>"wilsonblx.png"},
"commit"=>"Create"}
正如你所见,有没有像gallery_id中的图片哈希值。
As you see , there is nothing like "gallery_id" in the "pictures" hash .
总结我的问题给你:
-
有没有办法来传递foreign_key没有hidden_field?
Is there a way to pass the foreign_key without hidden_field ?
莫非我躲在通过某种方式在地址栏显示的外键的形式?
Could I hide somehow passing the foreign key form showing in the URL bar ?
有没有使用的link_to'传递参数的方法吗?
Is there an alternative on passing arguments using 'link_to' ?
感谢您。
推荐答案
您可能要考虑阅读的Rails指南嵌套的资源:
You may want to consider reading the Rails Guide on nested resources:
<一个href=\"http://guides.rubyonrails.org/routing.html#nested-resources\">http://guides.rubyonrails.org/routing.html#nested-resources
在一言以蔽之:
的routes.rb
routes.rb
resources :galleries do
resources :pictures do
end
# Generates the routes: /galleries/:gallery_id/pictures
pictures_controller.rb
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
图片/ new.html.erb
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 %>
好吧,所以gallery_id还是通过URL过去了,但我实在看不出什么不妥。你必须从某个地方传了吧?你真的只有3理智的选择在哪里把它传递:一个隐藏字段作为查询字符串参数,或者卷起的URL(嵌套资源)内。 3,后者是恕我直言,最干净的方法。
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.
如果你想使事情变得更简单了自己,我强烈建议寻找到何塞Valim的继承资源的宝石,需要照顾很多这样的样板污秽的你:
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:
<一个href=\"https://github.com/josevalim/inherited_resources\">https://github.com/josevalim/inherited_resources
这篇关于更好的方式向foreign_key值传递给控制器的Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!