简体   繁体   中英

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). 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. I've read this instructional on rendering: http://guides.rubyonrails.org/layouts_and_rendering.html but couldn't find what I was looking for. 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. Any ideas?

@road is a instance variable of the controller. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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