Rails 3.1 - 不能编辑和创建相同的部分嵌套资源

时间:2011-12-19 21:03:16

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我有一个管理部分,在管理员里面,我有,决赛选手和图像。 我正在使用,ruby 1.8.7-p249和Rails 3.1.2

# model image.rb
class Admin::Image < ActiveRecord::Base
    belongs_to :finalist
    ...
end

# model finalist.rb
class Admin::Image < ActiveRecord::Base
    has_many :images
    ...
end

我的ImagesController:

def new
  @admin_finalist = Admin::Finalist.find(params[:finalist_id])
  @admin_image = @admin_finalist.images.build

  respond_to do |format|
    format.html # new.html.erb
    format.json { render :json => @admin_image }
  end
end

def edit
  @admin_finalist = Admin::Finalist.find(params[:finalist_id])
  @admin_image = @admin_finalist.images.find(params[:id])
end

def create
  @admin_finalist = Admin::Finalist.find(params[:finalist_id])
  @admin_image = @admin_finalist.images.new(params[:admin_image])

  respond_to do |format|
    if @admin_image.save
      format.html { redirect_to admin_finalist_images_path(@admin_finalist) }
      format.json { render :json => @admin_image, :status => :created, :location => @admin_image }
    else
      format.html { render :action => "new" }
      format.json { render :json => @admin_image.errors, :status => :unprocessable_entity }
    end
  end
end

def update
  @admin_finalist = Admin::Finalist.find(params[:finalist_id])
  @admin_image = @admin_finalist.images.find(params[:id])

  respond_to do |format|
    if @admin_image.update_attributes(params[:admin_image])
      format.html { redirect_to admin_finalist_images_path(@admin_finalist) }
      format.json { head :ok }
    else
      format.html { render :action => "edit" }
      format.json { render :json => @admin_image.errors, :status => :unprocessable_entity }
    end
  end
end

我的图片_form.html.erb:

<%= form_for([@admin_finalist, @admin_image]) do |f| %>
    <% if @admin_image.errors.any? %>
    <div id="error_explanation">
        <h2><%= pluralize(@admin_image.errors.count, "error") %> prohibited this admin_image from being saved:</h2>

        <ul>
            <% @admin_image.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
            <% end %>
        </ul>
    </div>
    <% end %>

    <div class="field">
        <%= f.label :file %>
        <%= f.file_field :file %>
    </div>
    <%- unless @admin_image.new_record? || !@admin_image.file? -%>
    <div class="field">
        <%= f.label :remove_file, "Remover?" %>
        <%= f.check_box :remove_file %>
    </div>
    <%- end -%>
    <div class="field">
        <%= f.label :url %>
        <%= f.text_field :url, :placeholder => "http://" %>
    </div>
    <div class="field">
        <%= f.label :title %>
        <%= f.text_field :title %>
    </div>
    <div class="actions">
        <%= image_submit_tag("button/save.png") %>
        <%= link_to image_tag("button/back.png"), admin_finalist_images_path %>
    </div>
<% end %>

一切看起来都很完美,但是当我试图在 admin / finalists / 1 / images / new 中将图片添加到决赛时,我会收到此错误。

Showing .../app/views/admin/images/_form.html.erb where line #5 raised:

undefined method `admin_finalist_admin_images_path' for #<#<Class:0x102da0068>:0x102d9a870>

所以,我试图告诉rails使用特定网址:

<%= form_for([@admin_finalist, @admin_image], :url => admin_finalist_image_path(@admin_finalist.id, @admin_image)) do |f| %>

可悲的是,这项工作仅用于编辑。当我尝试编辑时,我收到此错误消息:

No route matches {:controller=>"admin/images", :finalist_id=>1, :id=>nil, :action=>"show"}

如果我想创作,我需要做:

<%= form_for([@admin_finalist, @admin_image], :url => admin_finalist_images_path) do |f| %>

如果我尝试编辑,我会收到此消息:

No route matches [PUT] "/admin/finalists/1/images"

我不知道该怎么办......我尝试了一切都没有成功,如果有人能帮助我,我将不胜感激。

修改

问题是我的模型是命名空间。所以,我重写所有的应用程序来解决这个问题,那就是正常运行。

[]单曲

0 个答案:

没有答案