我的应用程序中的模型
class Category < ActiveRecord::Base
has_many :dishes, :dependent => :destroy
end
class Dish < ActiveRecord::Base
attr_accessible :assets_attributes
belongs_to :category
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
end
class Asset < ActiveRecord::Base
belongs_to :dish
has_attached_file :asset, :styles => { :large => "640x480", :medium => "300x300>", :thumb => "100x100>" }
end
在 Gemfile
source 'http://rubygems.org'
gem 'rails', '3.0.11'
gem 'sqlite3', '1.3.3'
gem 'paperclip'
应用程序/控制器/dishes_controller.rb
class DishesController < ApplicationController
def new
@category = Category.find(params[:id])
@dish = @category.dishes.new(:category_id => params[:id])
@dish.assets.build
@title = "Create dish"
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @category }
end
end
def create
@category = Category.find(params[:category_id])
@dish = @category.dishes.create(params[:dish])
respond_to do |format|
format.html {
flash[:success] = "Dish created successfully!"
redirect_to(@category) }
format.json { render :json => @category }
end
end
end
应用程序/视图/类别/show.html.erb
<p><%= notice %></p>
<h1> Category Details </h1>
...
<hr />
<h2>Dishes</h2>
<table>
<tr>
...
</tr>
<%= render @category.dishes %>
</table>
<%= link_to 'New Dish', newdish_path(:id => @category.id) %>
应用程序/视图/菜肴/new.html.erb
<h1>New Dish</h1>
<%= render 'form' %>
应用程序/视图/菜肴/_form.html.erb
<%= form_for([@category, @dish]), :html => { :multipart => true } do |f| %>
...# Here the fields for dishes table will come
<div class="field">
<h4>New Files</h4>
<% f.fields_for :assets do |asset_fields| %>
<% if asset_fields.object.new_record? %>
<p>
<%= asset_fields.file_field :asset %>
</p>
<% end %>
<% end %>
<h4>Old Files</h4>
<% f.fields_for :assets do |asset_fields| %>
<% unless asset_fields.object.new_record? %>
<div class="thumb">
<%= link_to image_tag(asset_fields.object.asset.url(:thumb)), asset_fields.object.asset.url(:original) %>
<%= asset_fields.check_box :_destroy %>
</div>
<% end %>
<% end %>
</div>
...
<div class="actions">
<%= f.submit %>
</div>
<% end %>
尝试运行这些代码时出现错误
SyntaxError in Dishes#new
Showing /home/ragunathjawahar/Desktop/PROJECT/karaikudi-before-send/app/views/dishes/_form.html.erb where line #1 raised:
/home/ragunathjawahar/Desktop/PROJECT/karaikudi-before-send/app/views/dishes/_form.html.erb:1: syntax error, unexpected tASSOC, expecting keyword_end
...r([@category, @dish]), :html => { :multipart => true } do |f...
... ^
/home/ragunathjawahar/Desktop/PROJECT/karaikudi-before-send/app/views/dishes/_form.html.erb:1: syntax error, unexpected keyword_do_block, expecting keyword_end
...ml => { :multipart => true } do |f| @output_buffer.safe_conc...
... ^
/home/ragunathjawahar/Desktop/PROJECT/karaikudi-before-send/app/views/dishes/_form.html.erb:51: syntax error, unexpected keyword_ensure, expecting $end
如何解决这个问题呢?
提前致谢...