views:

362

answers:

2

I was using some image cropping example that I found online and now I got confused. There is actually no "crop" method in my controller. Instead (following the guide) I put a

 render :action => 'cropping', :layout=> "admin"

In my create method. That renders a page the view called cropping.html.erb . It works fine but I have no idea how to link or render that page otherwise, like if I wanted to hit a URL directly or press a button to recrop an image. Should I actually create a crop method in my controller and hook it up via routing if I want to be able to do this, or is there a way within my view to link to the same place that renders the cropping action?

Sorry about the confusion :) It doesn't help that the first version of the tutorial did have a cropping method and he removed it!! Any explanation on why one method is better over the other would be great. Thanks!!

A: 

The best way to do this depends on how you intend to be using the cropping template, and the associated controller logic. You'll find it useful to read up on the render documentation before proceeding.

  • If you're only ever going to use the cropping template one way. With the same controller logic that independent of the referring action (As in; Not part of a form submission). Then you should be defining a new action and route. It's your choice of whether you want to make a named route or just add a new one to the resource definition in routes.rb

    Depending on how you define your route you could do a link_to "Cropping", cropping_url

  • If you're going to be rendering it from multiple controllers each needing different preparation before rendering the template.

    render :template => 'path/template_name'
    

    Where path is the relative path from TEMPLATE_ROOT (RAILS_ROOT/app/views unless otherwise defined) and template name is the filename without the trailing .html.erb/.rhtml

  • If you want to render cropping as a part of another view, you can make it a partial.

    To make it a partial just rename the file to '_cropping.html.erb'. Now it can be called from any view with

    <%=render :partial => 'path/partial_name' %>
    

    Again, path is the TEMPLATE_ROOT relatave path to your partial. And partial_name is the file name of the partial, after omitting the leading underscore and trailing .html.erb or .rhtml.

Note: In either of the solutions involving a path to a template, the path can be omitted if the calling controller matches the path. Ie: if the template path is 'users/cropping.html.erb' called from the UsersController.

EmFi
A: 

In your case you would normally name the file create.html.erb which is where rails will look for the file by default. Writing code like:

render :action => 'viewname'

usually happens if you want to render one file in one case and a different one in another.

David