当表单引用多个模型时,允许在rails中使用params

时间:2016-01-18 06:12:43

标签: ruby-on-rails ruby

我有三个型号。我遇到麻烦的两个(配方和配料)每个都与另一个有<? $user = $_POST['user']; mail("info@ice-cream-showcase.com" , "test" , "user: ".join(",",$user) , "From: $email\n "); ?> 的关系。表格似乎得到了我要求的所有信息,但我似乎无法将该成分的名称属性纳入我允许的参数中。

形式:

has_and_belongs_to_many

食谱控制器中的相关操作:

<%= form_for(@recipe, :url => create_path) do |f| %>
<%= f.label :category %>
<%= f.select :category_id, options_for_select(Category.all.map{|c|[c.title, c.id]}) %>

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

<%= f.label :instruction %>
<%= f.text_area(:instruction, size: "50x10") %>

<%= f.fields_for :indgredient do |i| %>
    <%= i.label :name %>
    <%= i.text_field :name %>
<% end %>

<%= f.submit "Submit" %>

YAML转储给了我什么:

def create
    safe_params = params.require(:recipe).permit(:title, :instruction,  
                                                 :category_id, {ingredient: :name})
    @recipe = Recipe.new(safe_params)
    @recipe.save
    @recipe.ingredients.create(name: safe_params[:name])
    render body: YAML::dump(safe_params)
end

模特代码:

--- !ruby/hash:ActionController::Parameters
title: foo
instruction: bar
category_id: '1'

class Category < ActiveRecord::Base has_many :recipes end class Recipe < ActiveRecord::Base has_and_belongs_to_many :ingredients accepts_nested_attributes_for :ingredients belongs_to :category end class Ingredient < ActiveRecord::Base has_and_belongs_to_many :recipes end 方法会创建一个新成分,但名称为nil。在此先感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

您是否在public static Bitmap ImageSharpen(Bitmap InpImg) { Bitmap sharpenImage = new Bitmap(InpImg.Width, InpImg.Height); int wdth = InpImg.Width; int hght = InpImg.Height; double[,] filter = new double[3, 3]; filter[0, 0] = filter[0, 1] = filter[0, 2] = filter[1, 0] = filter[1, 2] = filter[2, 0] = filter[2, 1] = filter[2, 2] = -1; filter[1, 1] = 9; double factor = 1.0; double bias = 0.0; Color[,] result = new Color[InpImg.Width, InpImg.Height]; for (int x = 0; x < wdth; ++x) { for (int y = 0; y < hght; ++y) { double red = 0.0, green = 0.0, blue = 0.0; Color imageColor = InpImg.GetPixel(x, y); for (int filterX = 0; filterX < 3; filterX++) { for (int filterY = 0; filterY < 3; filterY++) { int imageX = (x - 3 / 2 + filterX + wdth) % wdth; int imageY = (y - 3 / 2 + filterY + hght) % hght; Color imageColor = InpImg.GetPixel(imageX, imageY); red += imageColor.R * filter[filterX, filterY]; green += imageColor.G * filter[filterX, filterY]; blue += imageColor.B * filter[filterX, filterY]; } int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255); int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255); int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255); result[x, y] = Color.FromArgb(r, g, b); } } } for (int i = 0; i < wdth; ++i) { for (int j = 0; j < hght; ++j) { sharpenImage.SetPixel(i, j, result[i, j]); } } return sharpenImage; } 的模型中添加了accepts_nested_attributes_for :ingredients

此外,还有一个gem来处理名为Recipe的嵌套表单。

您可以阅读这篇文章,它正在解释您正在尝试做什么。 https://hackhands.com/building-has_many-model-relationship-form-cocoon/

答案 1 :(得分:1)

首先,将<%= f.fields_for :indgredient do |i| %>更改为<%= f.fields_for :ingredients do |i| %>

并更改newcreate操作,如下所示

def new
  @recipe = Recipe.new
  @recipe.ingredients.build
end

def create
  @recipe = Recipe.new(safe_params)
  if @recipe.save
    redirect_to @recipe
  else
    render 'new'
  end
end

private
def safe_params
  params.require(:recipe).permit(:title, :instruction, :category_id, ingredients_attributes: [:name])
end

答案 2 :(得分:0)

要添加到@Pavan的答案,您必须记住Ruby正在构建对象(它是object orientated语言),并且因此,无论何时传递关联数据,都必须在内存中引用Ruby所拥有的对象

在您的情况下,您尝试通过Ingredient创建新的Recipe个对象:

#app/models/recipe.rb
class Recipe < ActiveRecord::Base
   has_and_belongs_to_many :ingredients
   accepts_nested_attributes_for :ingredients
end

...因此,您需要引用 ingredients

<%= f.fields_for :ingredients do ... %>

-

您还要确保仅在Recipe操作中处理create对象:

def create
    @recipe = Recipe.new safe_params
    @recipe.save
end

private

def safe_params
   params.require(:recipe).permit(:title, :instruction, :category_id, ingredients_attributes: [:name] )
end