表单不显示三重嵌套资源

时间:2013-06-26 17:18:19

标签: ruby-on-rails forms

我知道资源应该只嵌套一次,但是我有两个模型可以进入三重嵌套水域,这使得事情变得比它们需要的更复杂,但是我没有办法在这两个模型中避免它箱子...这样:

嵌套的工作原理如下:group> navbar> links

我在获取要呈现的链接表单时遇到问题:

- simple_form_for new_group_navbar_link_path(@group, @navbar, @link) do |f|
  %fieldset.well.pleft80.edit
    = f.input :method_name
    = f.input :text
    = f.input :button

  .form-actions
    = f.submit nil, :class => 'btn btn-primary pull-right btn-large'

navbar belongs_to groups and link belongs_to navbar

控制器:     class LinksController< ApplicationController的       before_filter:fetch_group       before_filter:fetch_navbar       before_filter:fetch_link,仅限:[:show,:edit,:update,:destroy]

  def show
  end

  def new
    @link = Link.new
  end

  def create
    @link = @navbar.links.build(params[:link])

    if @link.save
      redirect_to @navbar, notice: 'link was successfully updated.'
     else
     render :new
    end
 end

 def edit
   @image = @link.build_image unless @link.image
 end

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

  def destroy
    @link.destroy
    redirect_to navbar_path(@navbar)
  end

  private

  def fetch_group
    @group = Group.find(params[:group_id])
  end
  def fetch_navbar
    @navbar = Navbar.find(params[:navbar_id])
  end
  def fetch_link
    @link = @navbar.links.find(params[:id])
  end
end

我有什么简单的东西可以忽略吗?

1 个答案:

答案 0 :(得分:1)

只需替换

- simple_form_for new_group_navbar_link_path(@group, @navbar, @link) do |f|

= simple_form_for new_group_navbar_link_path(@group, @navbar, @link) do |f|