2012-05-20 102 views
0

我有一个安装有“产品”脚手架的应用程序。我还创建了一个新的foo控制器和视图。我在我的foo索引中有一个来自我的产品模型的部分表单。在我的foo控制器中,我定义了各种变量。我如何将这些变量作为默认值传递到正在渲染为我的foo索引的产品窗体中?在Rails中设置渲染窗体的默认设置

我的表格:

<%= form_for @product, :html => { :multipart => true } do |f| %> 
    <% if @product.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2> 

     <ul> 
     <% @product.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field">Feed ID 
    <%= params[:feed_id] %> 
    <%= hidden_field_tag('passed_feed_id', params[:feed_id]) %> 
    </div> 

    <div class="field"> 
    <%= f.label :uploadedimage %><br> 
    <%= f.file_field :uploadedimage %> 
    </div> 

    <div class="field"> 
    <%= f.label :title %> 
    <%= f.text_field :title %> 
    </div> 

    <div class="field"> 
    <%= f.label :category_names %> 
    <%= f.text_field :category_names, :size => 65 %> 
    <%= f.label :categories %> 
    <%= f.text_field :categories %> 
    </div> 

    <div class="field"> 
    <span><%= f.check_box :published %> <%= f.label :published %> </span> 
    </div> 

    <div class="field"> 
    <%= f.label :url %> 
    <%= f.text_field :url %> 

    <%= f.label :canonical_url %> 
    <%= f.text_field :canonical_url %> 

    <%= f.label :image_url %> 
    <%= f.text_field :image_url %> 
    </div> 
    <div class="field"> 
    <%= f.label :long_descr %><br /> 
    <%= f.text_area :long_descr %> 
    </div> 
    <div class="field"> 
    <%= f.label :mp_seller_name %><br /> 
    <%= f.text_field :mp_seller_name %> 
    </div> 
    <div class="field"> 
    <%= f.label :curr_item_price %> 
    <%= f.text_field :curr_item_price %> 
    <%= f.label :base_item_price %> 
    <%= f.text_field :base_item_price %> 
    </div> 
    <div class="field"> 
    <%= f.label :id_str %><br /> 
    <%= f.text_field :id_str %> 
    </div> 
    <div class="field"> 
    <%= f.label :id %><br /> 
    <%= f.text_field :id %> 
    </div> 
</div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 
+0

你能告诉你有这么远的代码? –

回答

0

从你的索引视图,你需要渲染局部与控制器实例变量的哈希值。请参阅rails guide了解如何操作。

0

我不完全确定你在问什么,但你可能想尝试传递局部变量到你的部分。

例如

<%= render :partial => "foo/form", :locals => { :a => 3, :b => "bar", :c => @baz } %> 
从您的部分,你可以利用 ab,并 c

然后。

结帐轨道的传递局部变量部分引导http://guides.rubyonrails.org/layouts_and_rendering.html

+0

这是它在我的视图中呈现的方式:<%= render:partial =>'products/form',:locals => {:title => @title}%>和“:title”和“@title”是在视图的控制器中声明的一个变量来渲染部分,但是当我提交产品时参数是空的:参数: {“utf8”=>“✓”, “authenticity_token”= >“JDNJLNfjlnejddfnjwenr3njnrefjnfjl =”, “passed_feed_id”=>“832”, “product”=> {“title”=>“”, – Yogzzz

+0

你能分享你的部分代码吗? – Cyrus

+0

上面的代码控制器代码,这是我在我的部分<%= render:partial =>'products/form',:locals => {:title => @title}%> – Yogzzz