标签: nested-forms

Rails嵌套表单与图像

因此,我正在构建一个嵌套的表单,其中包含一个广告系列模型和一个广告系列模型,其中广告系列具有很多网站.在我的广告系列表单中,我有:

<%= f.fields_for :sites do |builder| %>
    <%= render "site_fields", :f => builder %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

然后在_site_fields.html.erb中我有:

<div class="field">
 <%= f.label :title %><br />
 <%= f.text_field :title %>
</div>
 <%= f.label "Image"%><br>
 <%= f.file_field :image %>
<div class="field">
 <%= f.label :url %><br>
 <%= f.text_field :url %>
</div>
Run Code Online (Sandbox Code Playgroud)

这一切似乎都有效(令人震惊),但我希望预览已经上传到表格中特定网站的图像.所以,我有f.file_field:image我还希望能够显示该图像的预览.问题是我似乎无法访问当前正在渲染的网站,因为我正在使用f.fields_for.

有什么建议?我确定我错过了一些比较简单的东西.

提前致谢!

ruby-on-rails nested-forms

10
推荐指数
1
解决办法
5085
查看次数

如何将MultiDict转换为嵌套字典

我想将一个POST从Webob MultiDict转换为嵌套字典.例如

所以从POST:

'name=Kyle&phone.number=1234&phone.type=home&phone.number=5678&phone.type=work'
Run Code Online (Sandbox Code Playgroud)

多元化;

[('name', 'Kyle'), ('phone.number', '1234'), ('phone.type', 'home'), ('phone.number', '5678'), ('phone.type', 'work')]
Run Code Online (Sandbox Code Playgroud)

到嵌套字典

{'name': 'Kyle',
 'phone': [
  {
    'number': '12345',
    'type': 'home',
  },{
    'number': '5678',
    'type': 'work',
  },
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

编辑

我最终variable_decode从Will发布的formencode包中提取了该方法.唯一需要的改变是明确列表,例如

'name=Kyle&phone-1.number=1234&phone-1.type=home&phone-2.number=5678&phone-2.type=work'
Run Code Online (Sandbox Code Playgroud)

哪个更好有很多原因.

python nested nested-forms webob

10
推荐指数
1
解决办法
1万
查看次数

通过AJAX将动态字段添加到嵌套表单

我一直在观看和重现我的应用程序上的这些railscast:196-nested-model-form-part-1197-neested-model-form-part-2.我还没有专业帐户,所以我无法观看修改后的剧集.

我正在rails4(edge)下开发并且link_to_function已经被弃用而不喜欢不引人注目的JS(这很棒).

我将保留上述railscast的示例(即调查/问题).我想做的是使用问题的部分通过不引人注目的JavaScript,我只是不知道我应该如何以及在哪里做到这一点.

我想到了两种方法:

  • 第一种方法是添加我app/assets/javascripts的文件surveys.js与功能,add_question但我不知道如何从这里使用我的部分.
  • 其他方式是在我的行动中创建一个新的行动,SurveyController使用部分问题作出回应,但我对调查控制器中的问题采取特定行动的事实感到困扰.

我不禁认为必须有更好的方法来做到这一点.

ajax ruby-on-rails nested-forms railscasts

10
推荐指数
2
解决办法
2万
查看次数

has_many嵌套表单,其中包含has_one嵌套表单

我目前正在尝试为模型创建一个表单,该模型具有动态数量的嵌套模型.我正在使用嵌套表单(如RailsCasts 197中所述).为了使事情变得更复杂,我的每个嵌套模型都has_one与第三个模型相关联,我也希望将其添加到表单中.

对于任何想知道过度规范化或不正确方法的人来说,这个例子是我面临的问题的简化版本.实际上,事情稍微复杂一点,这就是我们决定采取的方法.

一些示例代码来说明下面的问题:

#MODELS
class Test 
  attr_accessible :test_name, :test_description, :questions_attributes
  has_many :questions
  accepts_nested_attributes_for :questions
end

class Question
  attr_accessible :question, :answer_attributes
  belongs_to :test
  has_one :answer
  accepts_nested_attributes_for :answer
end

class Answer
  attr_accessible :answer
  belongs_to :question
end

#CONTROLLER
class TestsController < ApplicationController

  #GET /tests/new
  def new
    @test = Test.new
    @questions = @test.questions.build
    @answers = @questions.build_answer
  end

end

#VIEW
<%= form_for @test do |f| %>
  <%= f.label :test_name %>
  <%= f.text_box :test_name %>
  <%= f.label :test_description %>
  <%= f.text_area …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails nested-forms nested-attributes

10
推荐指数
1
解决办法
2540
查看次数

找不到名称的关联 - 嵌套表格Rails 4

对于以下情况,我使用嵌套表单遇到has_many/has_many关系问题.

我有文章,这可以有很多单位(Kg,袋等).通过我的表格,我需要记录这些数据.

所以我决定使用带有关系has_many的嵌套表单,但是我有一个错误: ArgumentError (No association found for name 'article_unit_of_measurements'. Has it been defined yet?)

这是我的Classes代码:

class Article < ActiveRecord::Base
    has_many :unit_of_measurements, :through => :article_unit_of_measurements
    accepts_nested_attributes_for :article_unit_of_measurements
end

class ArticleUnitOfMeasurement < ActiveRecord::Base
    belongs_to :article
    belongs_to :unit_of_measurement

    accepts_nested_attributes_for :unit_of_measurement
end


class UnitOfMeasurement < ActiveRecord::Base
    has_many :articles, :through => :article_unit_of_measurements
end
Run Code Online (Sandbox Code Playgroud)

在我的表单中使用jquery插件select2.js将我的选择单元显示为标签.

= simple_form_for([:shop, @article], html: {class: 'form-horizontal' }) do |f|
  .col-md-10
    = f.input :name, placeholder: "Name", :input_html => {:class => 'form-control'}, label: false
  %label.col-md-2.control-label{for: "unit_of_measurement_id"} Unidad de medida
    .col-md-10
      %select#unit-select-measure.select2{:name =>"article[article_unit_of_measurements_attributes][#{Time.now.to_s}][unit_of_measurement_id]", …
Run Code Online (Sandbox Code Playgroud)

select has-many nested-forms nested-attributes ruby-on-rails-4

10
推荐指数
1
解决办法
5972
查看次数

具有嵌套属性的表单,其中has_one关联在Rails 3中不起作用

我想使用accepts_nested_attributes_for设置具有has_one关联的两个模型的值,但视图中不显示fields_for中的任何内容.

我已经确认相同的代码适用于Rails 2.x,并且它的has_many工作正常.代码如下.

模型

class Parent < ActiveRecord::Base

  has_one :child
  accepts_nested_attributes_for :child
end

class Child < ActiveRecord::Base

  belongs_to :parent
end
Run Code Online (Sandbox Code Playgroud)

调节器

def new
    @parent = Parent.new
    @parent.build_child
end
Run Code Online (Sandbox Code Playgroud)

视图

<%= form_for @parent do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>

  <% f.fields_for :child do |builder| %>

    <%= builder.label :childname %>
    <%= builder.text_field :childname %>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

..在复制并查看它之后,这是一个可怕的类名.

associations nested-forms ruby-on-rails-3

9
推荐指数
1
解决办法
6629
查看次数

param'samples'的预期Hash(得到数组)

我一直在关注嵌套表单和复杂表单的Railscasts剧集.在单个表单中创建多个模型的过程中,我能够编辑,更新,删除和创建嵌套在Batch模型中的样本模型的记录.

我很长时间以来一直在试图解决这个问题但是无法找到合适的解决方案.

我的开发日志文件给我以下错误.

错误信息:

Status: 500 Internal Server Error
  expected Hash (got Array) for param `samples'
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我有这样的更新动作

def update
     @batch = Batch.find(params[:id])

     respond_to do |format|
       if @batch.update_attributes(params[:batch])
         flash[:notice] = 'Successfully updated Batch.'
         format.html { redirect_to(@batch) }
         format.xml  { head :ok }
       else
         format.html { render :action => "edit" }
         format.xml  { render :xml => @batch.errors, :status => :unprocessable_entity }
       end
     end
   end
Run Code Online (Sandbox Code Playgroud)

我的观点是这样的:

<%= form_for @batch do |f| %>
......
<%= f.fields_for :samples do |s_form| %>
.... s_form things
<% …
Run Code Online (Sandbox Code Playgroud)

edit nested-forms multiple-models ruby-on-rails-2

9
推荐指数
2
解决办法
8040
查看次数

jQuery对rails上ruby中的部分数据执行的默认操作

我正在尝试以下方面.主窗体中的客户选择选项.当用户选择主表单中的客户时,他的账单应出现在所有后续部分中.

我正在使用ryan bates的nested_form gem来获取部分内容.代码如下.

<%= simple_nested_form_for @money_receipt do |f| %>

    <div class="customers"><%= f.collection_select :customer_id, Customer.all, :id, :name, options ={:prompt => "-Select a Customer"}, :style=>'width:210px;'%></div><br />
    /*rest of the code*/

    <%= f.link_to_add "Add line items", :money_receipt_line_items %>
Run Code Online (Sandbox Code Playgroud)

在局部内部

<div class="bill">
<%= f.collection_select :customer_bill_id, CustomerBill.all,:id,:id, {:prompt => "Select Customer bill"}, :style => 'width:202px;' %></div><br />

/*rest of the code*/
Run Code Online (Sandbox Code Playgroud)

上面的jQuery代码如下

jQuery(document).ready(function(){
   jQuery(".customers select").bind("change", function() {
      var data = {
        customer_id: jQuery(this).val()  
      }
      jQuery.getJSON(
         "/money_receipts/get_bills",
        data, function(data){
      var result = "",a,x;
      var b …
Run Code Online (Sandbox Code Playgroud)

jquery ruby-on-rails getjson nested-forms ruby-on-rails-3.2

9
推荐指数
1
解决办法
202
查看次数

如何在一个轨道中处理多个模型?

我有以下型号

class Survey < ActiveRecord::Base
  has_many :survey_sections
  accepts_nested_attributes_for :survey_sections
end

class SurveySection < ActiveRecord::Base
  belongs_to :survey
  has_many :questions
  accepts_nested_attributes_for :questions
end

class Question < ActiveRecord::Base
  belongs_to :survey_section
  has_many :answers
  belongs_to :question_group
  accepts_nested_attributes_for :question_group
  accepts_nested_attributes_for :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
end

class QuestionGroup < ActiveRecord::Base
  has_many :questions
end
Run Code Online (Sandbox Code Playgroud)

我的控制器:

 def new
    @survey = Survey.new
    survey_section = @survey.survey_sections.build
    survey_section.questions.build
  end

 def create
    @survey = Survey.new(survey_params)
    if @survey.save
      redirect_to @survey, notice: 'Super'
    else
      render 'new'
    end
  end

 def survey_params
      params.require(:survey).permit(:title, …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails nested-forms

9
推荐指数
1
解决办法
7387
查看次数

rails simple_nested_form_for fields_for错误的参数数量

所以我正在使用rails 3.1构建一个表单

<%= simple_nested_form_for(@person, :url => collection_url, :html=>{:multipart => true}) do |f| %>
  <%= render :partial => "form", :locals => { :f => f } %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

但部分中的这一行导致了问题:

<h2>Badges</h2> 
<ul id="certifications">
// this following line is raising the error "wrong number of arguments (4 for 3)"
<%= f.fields_for :certifications do |certification_form| %> 
    <%= render :partial => 'certification', :locals => { :f => certification_form } %>
<% end %>
</ul>
<%= f.link_to_add "Add a Badge", :certifications %>
Run Code Online (Sandbox Code Playgroud)

所以这是模型:

class …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails actionview nested-forms nested-attributes simple-form

8
推荐指数
2
解决办法
4166
查看次数