建立多个父对象的嵌套属性 [英] Building a nested attribute from multiple parent objects

查看:353
本文介绍了建立多个父对象的嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用的link_to 用它来创建链接使用动态创建的项目,而不是 new_object_path 的方法。轨道3允许多态的URL,这fl00r帮助我识别和形式我本来正在寻找使用。然而,在使用Rails的如下所示polymorphic_url 方法并通过HTTP请求的参数,返回的页面上的相关表单字段没有填写,和ISN'牛逼越来越设置。我试图建立一个 order_item 这也从一个内置照片

有谁看到发生了什么事情错在这里?相关code是如下:

 #辅助方法
高清add_cart_button_for(ARR,attr_hash = {})
  的link_to按钮(:cart_add),polymorphic_url([新] + ARR,attr_hash)
结束

#在index.html.haml通话
= add_cart_button_for [current_order,:order_item]:photo_id => photo.id

#_form.html.haml的order_item
 - 的form_for [@order,@order_item]做| F |
  %P
    = f.label:photo_id
    %BR
    = f.text_field:photo_id
 


下面是原题:

我试图创建一个链接来创建一个 [新,@order,@order_item] ,其中 @ order_item.photo_id 可以通过在链接。

现在,我不知道是否是有很大的意义,所以我会尽力在这里澄清我的问题。

这是我所看到的,Rails 3中有一个新望语法链接到资源利用结构为的数组操作[:动作,目标] 或深度嵌套资源, [:动作,父母,子女] 。对于这个特殊的例子,我使用的是的link_to [新,@order,:order_item] 。这些按钮会附着在照相馆的每张照片。

是否有可能通过 ID 的照片作为新的订单项目的属性 photo_id 的<? / P>

到目前为止,我已经试过的link_to [新,@order,:order_item]:photo_id =&GT; photo.id ,但似乎并没有工作。我也试图把:photo_id 在一个明确的哈希值。有没有办法遵循这一新的语法和实现这样的功能?

更新: 我真的很希望能找到沿线的东西:

 的link_to新的照片,[新,@order,:order_item =&GT; {:photo_id =&GT; photo.id}]
 

样的,我正在做一个:建立,但多只被提供的建设者模型的属性

更新 11 2011年4月 我得到它的工作,我可以通过使用属性为一个GET请求 polymorphic_url 如下所示:

 #辅助方法
高清add_cart_button_for(ARR,attr_hash = {})
  的link_to按钮(:cart_add),polymorphic_url([新] + ARR,attr_hash)
结束

#在index.html.haml通话
= add_cart_button_for [current_order,:order_item]:photo_id =&GT; photo.id
 

这似乎应该是工作。我可以看到,似乎预期是工作的GET请求:

 入门使用/命令/ 1 / ORDER_ITEMS /新?photo_id = 130为127.0.0.1在2011-04-11 18时十一分07秒-0400
  处理由OrderItemsController#新的HTML
  参数:{photo_id=&gt;中130,的order_id=&gt;中1}
 

和网址与预期一致,但形式似乎并没有被捕获的URL的变量。

本:ORDER_ID作品。本:photo_id,但似乎并没有被结转到的OrderItems#新的一页。我创建了一个 text_field :photo_id ,每一次它是空的。这是在 attr_accessible 。我不明白为什么它不工作。

解决方案

我终于想通这件事了。

在我的OrderItemsController#新的动作,我的建立函数要么不采取任何参数或 PARAMS [:order_item] 作为参数。由于从 polymorphic_url 实际上并没有与任何对象传递的参数,但只是一个原始属性,我试图简单地使整个 PARAMS 散到构建功能。工作就像一个魅力。所以我用这个code的链接:

  = add_cart_button_for [current_order,:order_item]:photo_id =&GT; photo.id
 

这code在我的控制器:

 高清新
  @order_item = @ order.order_items.build(PARAMS)
结束



DEF创建
  @order_item = @ order.order_items.build(PARAMS [:order_item])
  如果@ order_item.save
    @ order.save
    闪光[:通知] =成功创建订单项目。
    redirect_to时@order
  其他
    渲染:行动=&GT; '新'
  结束
结束
 

现在整个PARAMS散列获得通过,形式建设的新订单项目时能够找到正确的照片。

I'm currently trying to use link_to with a helper method that creates links for creating items dynamically, rather than using the new_object_path methods. Rails 3 allows for polymorphic URLs, which fl00r helped me identify and use in the form I was originally searching for. However, while using Rails' polymorphic_url method as shown here does pass the argument in the http request, the related form field on the returned page isn't filled in, and isn't getting set. I'm trying to build an order_item from an order that also is built from a photo.

Does anyone see what's going wrong here? Related code is below:

# the helper method
def add_cart_button_for(arr, attr_hash={})
  link_to button(:cart_add), polymorphic_url( [:new] + arr, attr_hash )
end

# the call in index.html.haml
= add_cart_button_for [current_order, :order_item], :photo_id => photo.id

# _form.html.haml for order_item
- form_for [ @order, @order_item ] do |f|
  %p
    = f.label :photo_id
    %br
    = f.text_field :photo_id


Here's the original question:

I'm trying to create a link to create a [:new, @order, @order_item] where @order_item.photo_id can be passed in the link.

Now, I'm not sure if that makes a lot of sense, so I'll try to clarify my problem here.

From what I've seen, Rails 3 has a newish syntax for linking to resource actions using an array structured as [:action, object] or for deeply nested resources, [:action, parent, child]. For this particular example, I'm using link_to [:new, @order, :order_item]. These buttons will be attached to each photo in a photo gallery.

Is it possible to pass the id of the photo as the attribute photo_id of the new order item?

So far I've tried link_to [:new, @order, :order_item], :photo_id => photo.id, but that doesn't seem to work. I've also tried putting :photo_id in an explicit hash. Is there a way to follow this new syntax and achieve this functionality?

Update: I'm really hoping to find something along the lines of:

 link_to "new photo", [:new, @order, :order_item => {:photo_id => photo.id}]

where I'm kind of doing a :build but with more than just the attributes from the builder model being supplied.

UPDATE 11 Apr 2011 I got it working where I can pass an attribute into a get request using polymorphic_url as seen below:

# the helper method
def add_cart_button_for(arr, attr_hash={})
  link_to button(:cart_add), polymorphic_url( [:new] + arr, attr_hash )
end

# the call in index.html.haml
= add_cart_button_for [current_order, :order_item], :photo_id => photo.id

This seems like it should be working. I can see the get request that seems to be working as expected:

Started GET "/orders/1/order_items/new?photo_id=130" for 127.0.0.1 at 2011-04-11 18:11:07 -0400
  Processing by OrderItemsController#new as HTML
  Parameters: {"photo_id"=>"130", "order_id"=>"1"}

and the url is as expected, but the form doesn't seem to be catching that variable from the url.

The :order_id works. The :photo_id, however, does not appear to be carrying over to the OrderItems#new page. I created a text_field for :photo_id, and it's empty every time. It's in the attr_accessible. I don't see why it's not working.

解决方案

I finally figured this thing out.

In my OrderItemsController#new action, my build function was either taking no parameters or params[:order_item] as the parameter. Since the parameter passed from the polymorphic_url wasn't actually related to any object, but was just a raw attribute, I tried simply passing the entire params hash into the build function. Worked like a charm. So I'm using this code for the link:

= add_cart_button_for [current_order, :order_item], :photo_id => photo.id

and this code in my controller:

def new
  @order_item = @order.order_items.build(params)
end



def create
  @order_item = @order.order_items.build(params[:order_item])
  if @order_item.save
    @order.save
    flash[:notice] = "Successfully created order item."
    redirect_to @order
  else
    render :action => 'new'
  end
end

Now that the entire params hash is getting passed, the form can find the correct photo when building the new order item.

这篇关于建立多个父对象的嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆