0

你好

我是 Rails 的新手,我想启动一个应用程序。首先我尝试了一些简单的事情,我猜想通过这个例子将 2 个模型与 has_many-belongs_to 关系联系起来:example

这个例子效果很好,所以如果我启动控制台,我可以创建一个父母和他的孩子但是当我继续我的本地时,当你显示父母时,缺少添加孩子的视图。

所以我尝试创建部分和嵌套资源,但它不起作用,rails 说我从父级到子级的编辑链接无效!

这是我的代码:

子模型 ->

class Child < ActiveRecord::Base
attr_accessible :name, :parent_id
belongs_to :parent
end

父模型->

class Parent < ActiveRecord::Base
attr_accessible :name
has_many :children
end

Child controller ->
    class ChildrenController < ApplicationController
    before_filter :load_parent
    # GET /children
    # GET /children.json
    def index
       @children = @parent.children.all
       respond_to do |format|
            format.html # index.html.erb
            format.json { render json: @children }
       end
    end

  # GET /children/1
  # GET /children/1.json
  def show
    @child = @parent.children(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @child }
    end
  end

  # GET /children/new
  # GET /children/new.json
  def new
    @child = @parent.children.new

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

  # GET /children/1/edit
  def edit
    @child = @parent.children.find(params[:id])
  end

  # POST /children
  # POST /children.json
  def create
    @child = @parent.children(params[:child])

    respond_to do |format|
      if @child.save
        format.html { redirect_to [@parent, @child], notice: 'Child was successfully created.' }
        format.json { render json: @child, status: :created, location: @child }
      else
        format.html { render action: "new" }
        format.json { render json: @child.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /children/1
  # PUT /children/1.json
  def update
    @child = @parent.children.find(params[:id])

    respond_to do |format|
      if @child.update_attributes(params[:child])
        format.html { redirect_to [@parent, @child], notice: 'Child was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @child.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /children/1
  # DELETE /children/1.json
  def destroy
    @child = @parent.children.find(params[:id])
    @child.destroy

    respond_to do |format|
      format.html { redirect_to parent_children_path(@parent) }
      format.json { head :no_content }
    end
  end

  private 
      def load_parent 
    @parent = Parent.find(params[:parent_id]) 
      end 

end

'_child.html.erb' ->

<%= div_for child do %> <h3>
<%= child.name %>
<span class='actions'>
<%= link_to 'Delete', [@parent, child], :confirm => 'Are you sure?', :method => :delete %>
</span> 
</h3>

父 show.html.erb ->

<h3>Chlidren</h3>
<div id="children">
    <%= render @parent.children %>
</div>

<%= render :file => 'children/new' %>

<% end %>
<%= link_to 'Edit', edit_parent_child(@parent, child), :method => :edit %>

孩子 show.html.erb ->

<p id="notice"><%= notice %></p>

<p>
      <b>Name:</b>
      <%= @child.name %>
</p>

<p>
  <b>Parent:</b>
  <%= @child.parent_id %>
</p>


<%= link_to 'Edit', edit_parent_child_path(@parent, @child) %> |
<%= link_to 'Back', parent_children_path(@parent) %>

也许我做得不好,我不知道..谢谢帮助我度过这个难关!

托马斯

4

0 回答 0