简体   繁体   English

Rails 3:使用render动作将实例变量从控制器传递到不同的视图

[英]Rails 3: Passing an instance variable from a controller to a different view using a render action

I have a controller (RoadsController) update action which I'm using to call a different view (views/road_surface/crop.html.erb). 我有一个控制器(RoadsController)更新操作,我用它来调用不同的视图(views / road_surface / crop.html.erb)。 To meet other project requirements, I need to keep this file in a different view: 为了满足其他项目要求,我需要将此文件保存在不同的视图中:

def update
  @road = Road.find params[:id]

  if @road.update_attributes params[:road]
    if params[:road][:road_surface].present?
      render "road_surface/crop" #I'd like to pass @road to my road_surface view here
    else
      redirect_to road_path @road.id
    end
  else
    render : action => 'edit'
  end
end

I need to pass the @road instance variable to the road_surface view. 我需要将@road实例变量传递给road_surface视图。 I've read this instructional on rendering: http://guides.rubyonrails.org/layouts_and_rendering.html but couldn't find what I was looking for. 我已经阅读了关于渲染的这个教学: http//guides.rubyonrails.org/layouts_and_rendering.html但找不到我想要的东西。 This seems simple enough but I'm definitely a Rails noob so I think I'm either missing something obvious or going about this the wrong way. 这看起来很简单,但我绝对是一个Rails noob,所以我想我要么缺少一些明显的东西,要么就是错误的方式。 Any ideas? 有任何想法吗?

@road is a instance variable of the controller. @road是控制器的实例变量。 All instance variables from the called controller method are available in the called view. 来自被调用控制器方法的所有实例变量都可在被调用视图中使用。

just do <%= @road.inspect %> in the view and you will see it print the attributes of the model instance. 只需在视图中执行<%= @road.inspect %> ,您将看到它打印模型实例的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM