如何显示新创建资源的flash消息?

时间:2021-05-11 15:10:14

Lets say I have a Product model and my controller allows the creation of 5 new products all at once. What I do right now is render back to the same page but what I want to do is also render in the flash which products have been created. So it would say:

假设我有一个产品型号,我的控制器可以同时创建5个新产品。我现在所做的是渲染回同一页面,但我想要做的还是在flash中渲染哪些产品已经创建。所以它会说:

Successfully created: Milk, Soup, Cheese, Bread, Candy

成功创造:牛奶,汤,奶酪,面包,糖果

instead of

Successfully created Products

成功创建产品

How would this be done?

这怎么办?

2 个解决方案

#1


2  

Something like this should work:

像这样的东西应该工作:

redirect_to :back, notice: "Succesfully created: #{@created_products.map(&:name).join(', ')}"

Assuming @created_products is an array of the products you just created and they each have an attribute called name.

假设@created_products是您刚刚创建的产品数组,它们每个都有一个名为name的属性。

Oh and of course you need this in your html:

哦,当然你需要在你的HTML中:

<div class="notice"><%= notice %></div>

#2


1  

Just do this in your controller. You probably have a create action where you actually create these objects, and they problem have a name attribute or something, right? So when you create the objects, save them in an array, then use the map and join methods to put them together. Something like this:

只需在控制器中执行此操作。你可能有一个创建动作,你实际创建这些对象,他们问题有一个名称属性或东西,对吧?因此,在创建对象时,将它们保存在数组中,然后使用map和join方法将它们组合在一起。像这样的东西:

def create
    successful = []

    # loop through the parameters
        obj = MyModel.new(...)
        if obj.save
            successful << obj
        end
    # end loop

    flash[:notice] = "Succesfully created: #{successful.map(&:name).join(', ')}"

    redirect_to my_model_path
end

Map runs the method you pass in, so name, on each of the memebrs of the array and returns an array whose contents are the results of that method: in other words, you get an array of all their names. Join puts them together separated by whatever string you put in. So you'd get something like "Milk, cheese".

Map运行您传入的方法,因此在数组的每个memebrs上命名并返回一个数组,其内容是该方法的结果:换句话说,您将得到一个包含所有名称的数组。加入将它们放在一起,用你放入的任何字符串分开。所以你会得到类似“牛奶,奶酪”的东西。

#1


2  

Something like this should work:

像这样的东西应该工作:

redirect_to :back, notice: "Succesfully created: #{@created_products.map(&:name).join(', ')}"

Assuming @created_products is an array of the products you just created and they each have an attribute called name.

假设@created_products是您刚刚创建的产品数组,它们每个都有一个名为name的属性。

Oh and of course you need this in your html:

哦,当然你需要在你的HTML中:

<div class="notice"><%= notice %></div>

#2


1  

Just do this in your controller. You probably have a create action where you actually create these objects, and they problem have a name attribute or something, right? So when you create the objects, save them in an array, then use the map and join methods to put them together. Something like this:

只需在控制器中执行此操作。你可能有一个创建动作,你实际创建这些对象,他们问题有一个名称属性或东西,对吧?因此,在创建对象时,将它们保存在数组中,然后使用map和join方法将它们组合在一起。像这样的东西:

def create
    successful = []

    # loop through the parameters
        obj = MyModel.new(...)
        if obj.save
            successful << obj
        end
    # end loop

    flash[:notice] = "Succesfully created: #{successful.map(&:name).join(', ')}"

    redirect_to my_model_path
end

Map runs the method you pass in, so name, on each of the memebrs of the array and returns an array whose contents are the results of that method: in other words, you get an array of all their names. Join puts them together separated by whatever string you put in. So you'd get something like "Milk, cheese".

Map运行您传入的方法,因此在数组的每个memebrs上命名并返回一个数组,其内容是该方法的结果:换句话说,您将得到一个包含所有名称的数组。加入将它们放在一起,用你放入的任何字符串分开。所以你会得到类似“牛奶,奶酪”的东西。