在form_for RoR中显示来自DB的图像

时间:2015-07-19 01:55:59

标签: ruby-on-rails ruby ruby-on-rails-3 form-for

我正在尝试创建一个更新表单,用于修改(添加或删除)与产品相关的图像。每个产品都有很多图像,每个图像都属于一个产品。 当用户想要修改产品时,想法是显示与产品相关的所有图像,并且用户决定是添加更多还是删除它们。我的问题是我不知道如何使图像出现。我只能让它显示链接。

edit.html.erb

<%- model_class = Product -%>
<div class="page-header">
  <h1><%=t '.title', :default => [:'helpers.titles.edit', 'Edit %{model}'], :model => model_class.model_name.human.titleize %></h1>
</div>
<%= render :partial => 'form' %>

_form.html.erb

<%= form_for @product, :html => { :class => "form-horizontal product" } do |f| %>

        <% if @product.errors.any? %>
        <div id="error_expl" class="panel panel-danger">
          <div class="panel-heading">
            <h3 class="panel-title"><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h3>
          </div>
          <div class="panel-body">
            <ul>
            <% @product.errors.full_messages.each do |msg| %>
              <li><%= msg %></li>
            <% end %>
            </ul>
          </div>
        </div>
      <% end %>

      <div class="control-group">
        <%= f.label :name, :class => 'control-label' %>
        <div class="controls">
          <%= f.text_field :name, :class => 'form-control' %>
        </div>
        <%= error_span(@product[:name]) %>
      </div>


      <div class="control-group">
        <%= f.label :price, :class => 'control-label' %>
        <div class="controls">
          <%= f.text_field :price, :class => 'form-control' %>
        </div>
        <%= error_span(@product[:price]) %>
      </div>
      <div class="control-group">
        <%= f.label :stock, :class => 'control-label' %>
        <div class="controls">
          <%= f.text_field :stock, :class => 'form-control' %>
        </div>
        <%= error_span(@product[:stock]) %>
      </div>
      <div class="control-group">
        <%= f.label :tipo, :class => 'control-label' %>
        <div class="controls">
          <%= f.text_field :tipo, :class => 'form-control' %>
        </div>
        <%= error_span(@product[:tipo]) %>
      </div>
      <p> <hr> </p>



    <%= f.fields_for :images do |builder| %>
      <%= render 'image_fields', f: builder %>
    <% end %>
    <%= link_to_add_fields "Add Question", f, :images %>


    <p> <hr> </p>

      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                products_path, :class => 'btn btn-default' %>

    <% end %>

_image_fields.html.erb

<fieldset>

  <%= f.label :link, "Imagen" %><br />
  <div class="col-xs-6 col-md-3">
    <a href="#" class="thumbnail">
      <img src="<%= Here I want to introduce the link %>" alt="...">
    </a>
  </div>
  <%= f.check_box :_destroy %>
  <%= f.label :_destroy, "Remove Imagen" %>
</fieldset>

products_controller.rb

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  before_action :build_image, only: [:edit]
private
# Use callbacks to share common setup or constraints between actions.
def set_product
  @product = Product.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def product_params
  params.require(:product).permit(:name, :price, :stock, :tipo, images_attributes: [ :link ])
end

def build_image
  @images = @product.images.build
end
end

0 个答案:

没有答案
相关问题