如何解决二级深层模型上传图像时的错误

时间:2012-04-13 07:03:42

标签: ruby-on-rails

我的应用中的模型

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

如何解决这个问题?

提前致谢...

2 个答案:

答案 0 :(得分:2)

首先纠正此行并告诉

    <%= form_for([@category, @dish], :html => { :multipart => true }) do |f| %>

使用此行创建菜肴

            @dish = Dish.create!(:dishCode => params[:dish][:dishCode] ,:name => params[:dish][:name])

而不是

        @dish = @category.dishes.create(params[:dish])   

但首先检查params [:dish] [:dishCode]给你的价值与否。

冷静.......

答案 1 :(得分:1)

尝试更改此内容:

<%= form_for([@category, @dish]), :html => { :multipart => true } do |f| %>

到此:

<%= form_for([@category, @dish], :html => { :multipart => true }) do |f| %>
是的,你过早关闭了支架。