我知道资源应该只嵌套一次,但我有两个模型浸入三重嵌套水域,这使得事情比它们需要的更复杂,但在这两种情况下我看不到避免它的方法。 。所以:
嵌套的工作方式如下: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
控制器:类 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
我忽略了一些简单的事情吗?