Skip to content

Commit 8042011

Browse files
committed
Added form for products
1 parent 5ffedc9 commit 8042011

File tree

8 files changed

+90
-20
lines changed

8 files changed

+90
-20
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
class ProductsController < ApplicationController
2+
before_filter :find_product, :only => [:edit, :update, :destroy]
3+
24
def index
5+
@products = Product.all
6+
end
7+
8+
def new
9+
@product = Product.new
10+
@product.product_pictures.build
11+
end
12+
13+
def create
14+
@product = Product.new(params[:product])
15+
end
16+
17+
def edit
18+
end
19+
20+
def update
21+
@product.update_attributes(params[:product])
22+
end
23+
24+
def destroy
25+
end
26+
27+
protected
28+
def find_product
29+
@product = Product.find(params[:id])
330
end
431
end

app/models/product.rb

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ class Product < ActiveRecord::Base
22
has_many :product_pictures
33

44
attr_accessible :title
5+
6+
accepts_nested_attributes_for :product_pictures
57
end

app/views/product_pictures/index.html.erb

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
<h2>Products picture</h2>
22
<%= form_for ProductPicture.new, :html => { :multipart => true, :id => "fileupload" } do |f| %>
33
<div class="row">
4-
<div class="span16 fileupload-buttonbar">
5-
<div class="progressbar fileupload-progressbar nofade"><div style="width:0%;"></div></div>
6-
<span class="btn success fileinput-button">
7-
<span><%= t('pictures.add_files') %>...</span>
8-
<%= f.text_field :title %>
9-
<%= f.text_area :description %>
10-
<%= f.file_field :picture %>
11-
</span>
12-
<button type="submit" class="btn primary start"><%= t('pictures.start_upload') %></button>
13-
<button type="reset" class="btn info cancel"><%= t('pictures.cancel_upload') %></button>
14-
<button type="button" class="btn danger delete"><%= t('pictures.delete_selected') %></button>
15-
<input type="checkbox" class="toggle">
16-
</div>
4+
<%= f.label :title %><br />
5+
<%= f.text_field :title %>
6+
</div>
7+
<div class="row">
8+
<%= f.label :description %><br />
9+
<%= f.text_area :description, :rows => 5 %>
10+
</div>
11+
<div class="row">
12+
<span><%= t('pictures.add_files') %>...</span>
13+
<%= f.file_field :picture %>
14+
</div>
15+
<br>
16+
<div class="row">
17+
<button type="submit" class="btn primary start"><%= t('pictures.start_upload') %></button>
18+
<button type="reset" class="btn info cancel"><%= t('pictures.cancel_upload') %></button>
19+
<button type="button" class="btn danger delete"><%= t('pictures.delete_selected') %></button>
20+
<input type="checkbox" class="toggle">
1721
</div>
1822
<br>
1923
<div class="row">
20-
<div class="span16">
21-
<table class="zebra-striped"><tbody class="files"></tbody></table>
22-
<div id="loading"> </div>
23-
</div>
24+
<table class="zebra-striped"><tbody class="files"></tbody></table>
25+
<div id="loading"> </div>
2426
</div>
2527
<% end %>
2628
<script>

app/views/products/_form.html.erb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<%= form_for @product, :html => { :multipart => true } do |f| %>
2+
<%= f.label :title %><br />
3+
<%= f.text_field :title %>
4+
5+
<fieldset>
6+
<%= f.fields_for :product_pictures do |picture| %>
7+
<%= picture.label :title %><br />
8+
<%= picture.text_field :title %>
9+
10+
<br />
11+
<%= picture.label :description %><br />
12+
<%= picture.text_area :description, :rows => 2 %>
13+
14+
<br />
15+
<%= picture.label :picture %><br />
16+
<%= picture.file_field :picture %>
17+
<% end %>
18+
</fieldset>
19+
<% end %>

app/views/products/edit.html.erb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Edit Product</h1>
2+
<%= render "form" %>

app/views/products/index.html.erb

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
<h1>Products#index</h1>
2-
<p>Find me in app/views/products/index.html.erb</p>
1+
<h1>Products lists</h1>
2+
<%= link_to "New Product", new_product_path %>
3+
<table>
4+
<tbody>
5+
<tr>
6+
<td>Title</td>
7+
<td>First Image</td>
8+
<td></td>
9+
</tr>
10+
<% @products.each do |product| %>
11+
<tr>
12+
<td><%= product.title %></td>
13+
<td><%= image_tag(product.product_pictures.first.thumb('80x80#').url) if product.product_pictures %></td>
14+
<td>Edit</td>
15+
</tr>
16+
<% end %>
17+
</tbody>
18+
</table>

app/views/products/new.html.erb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>New Product</h1>
2+
<%= render "form" %>

config/routes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
JqueryUploadRails::Application.routes.draw do
22

3-
resources :products, :only => :index
3+
resources :products
44
resources :product_pictures, :only => [:index, :create, :destroy]
55

66
root :to => "products#index"

0 commit comments

Comments
 (0)