【问题标题】:Destroy/Show an array element with link_to function in Rails在 Rails 中使用 link_to 函数销毁/显示数组元素
【发布时间】:2015-09-18 15:45:58
【问题描述】:

我有一个带有数组列(“Product_list”)的模型,并且希望在用户选择数组元素时通过控制器删除/显示数组元素在视图中。

*Delete 会给出确认提示,并从同一视图中删除选定的数组元素。

*Show 将引导用户到另一个页面,其中将显示与用户选择的数组元素关联的另一个数据表中的产品详细信息)

我只是把??????????在 link_to 行中,因为我不知道可以为数组元素使用什么路径。我还从控制器中排除了 SHOW 和 DESTROY 操作,因为我只是不知道该放什么。

Index.html.erb

<% @product.product_list.each do |list| %>
  <%= link_to(list) do %>
  <div class="thumbnail">
    <div class="caption">
      <p><%= list %></p>
      <%= link_to 'Show Details', ????????? %>
      <%= link_to 'Destroy', ??????????, data: { confirm: 'Are you sure?' } %>
    </div>
  </div>
  <% end %>
<% end %>

Products_controller.rb

def index
  @product = Product.find(current_user.product)
  rescue ActiveRecord::RecordNotFound
end

def new
  @product = Product.new
end

def create
  @product = Product.new(product_params)
  @product.save
  Product.find_by_sql(["UPDATE products SET product_list = array_append(product_list, ?) WHERE user_id = ?", params[:product][:product_list], current_user]);
end

型号

create_table "products", force: :cascade do |t|
  t.string   "product_list", default: [], array: true
  t.integer  "user_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

add_index "products", ["user_id"], name: "index_products_on_user_id", using: :btree

【问题讨论】:

    标签: ruby-on-rails arrays postgresql


    【解决方案1】:

    试试

    <%= link_to 'Show Details', product_path(id: @product.id, array_element: list) %>
    <%= link_to 'Destroy', product_path(id: @product.id, array_element: list), data: { confirm: 'Are you sure?' } %>
    

    在你的控制器中显示和销毁,检查参数:array_element。

    【讨论】:

    • 感谢您的回答!但它给出了一个错误:“没有路由匹配 {:action=>"show", :array_element=>"", :controller=>"products", :product_id=>24} 缺少必需的键:[:id]"
    • 也许我没有在控制器中设置参数 :array_element ... 用户选择的数组元素应保存到该参数,但我不确定“@product.product_list.each 是否 |列表|"在视图中被传递给控制器​​,以便“列表”可以在控制器中使用......
    【解决方案2】:

    了解铁路路线

    当你使用 erb link_to 方法时,你基本上是在创建一个普通的链接,和使用一样:

    &lt;a href="/"&gt;&lt;/a&gt;

    但不是指示正常的 url 路径 (在正常链接的情况下,url 目标将在 href 内),您需要将其声明为 rails 路径,声明您的您的 routes.rb 文件中的 rails 路径。

    如果你想知道你的 rails 路径是什么,你需要运行:

    rake routes

    在您的终端中,该命令将打印您当前的 rails 路线。

    这是一个输出示例:

    如您所见,URI Pattern 是普通路径,但是如果看到前缀,则可以看到rails路径,例如:

    new_user_session 执行 devise/sessions#new/users/sign_in

    要使用该 Rails 路径,您需要在前缀末尾包含“路径”。

    例子:

    new_user_session_path

    所以最终的结果是:

    &lt;%= link_to "Sign in", new_user_session_path %&gt;

    CRUD 和控制器

    CRUD 代表 CreateReadUpdateDestroy

    这些操作是不言自明的,您可以在控制器中编写它们。

    例如,如果您的索引视图中有一个产品列表,那么这就是 products_controller 的代码:

    def index
      @products = Product.all
    end
    

    如果您想在索引中显示Product.all 返回的每个元素(并存储在变量@products 中),您只需在索引视图中编写以下内容:

    <% @products.each do |f| %>
      <%= link_to "#{f.name}", f %>
      </br>
    <% end %>
    

    在上面的代码中,rails 会自动识别出你想要转到 ruby​​ 块中每个元素的实例,在本例中是 f,所以你只需要写 f 而不是 prefix_path(记得吗? )

    现在,这将打印所有产品名称并使它们成为相应路径 (url) 的链接(感谢 link_to),并显示 @products 变量的每个实例(由 Product.all 返回) 您需要在您的 products_controller.rb 文件的 show 方法中编写以下代码:

    def show
      @product = Product.find(params[:id])
    end
    

    将每个产品引用到正确的 id,(/products/1/ ... /102/) 等等。

    所以现在当你点击每个实例时,它会渲染通过它的 id 找到的对应产品(参见 show 方法),现在,如果你想删除一个特定的产品,很简单,将这段代码添加到你的 products_controller 中:

    def destroy
      @product = Product.find(params[:id])
      if @product.destroy
        flash[:notice] = "Product was deleted successfully."
        redirect_to products_path
      else
        flash[:error] = "There was an error deleting the product. Please try again."
        render :show
      end
    end
    

    注意:为了使 flash 消息正常工作,您需要在应用程序中启用它们,但因为它超出了问题的范围,我现在不打算讨论它。

    如你所见,代码可读性强,一目了然,删除产品时会破坏产品,显示一个flash消息,然后渲染正确的路径。

    在您的 show.html.erb 视图文件中,您需要具有以下代码才能删除记录(在本例中为产品,因为 show 方法根据其 id 返回产品),这里是示例代码:

    <%= "#{@product.name}" %>
    </br>
    <%= "#{@product.description}" %>
    
    <%= link_to "Delete product", @product, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this product?' } %>
    

    这将从数据库中销毁产品实例(如果您单击链接),因为这会在您的 products_controller 中触发销毁操作。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      相关资源
      最近更新 更多