right way to do rjs off a method with its own view?

Hi, just looking for "best practice" guidance...

I have a controller method that renders its output with an rhtml view
file. The output can also be updated via an rjs update via an rjs view
file.

I can't work out if it is possible to have action.rhtml AND action.rjs
and have the correct one called depending on the request. SO, I have
done this:

if I have have "my_action.rhtml" and "my_action_update.rjs",
then the controller method is :-

  def my_action
    .... action code...

    render :action => :my_action_update if request.xhr?
  end

This works, but is it the right way, or is there a better way?

Comments appreciated :slight_smile:

John Lane napisał(a):

Hi, just looking for "best practice" guidance...

I have a controller method that renders its output with an rhtml view
file. The output can also be updated via an rjs update via an rjs view
file.

I can't work out if it is possible to have action.rhtml AND action.rjs
and have the correct one called depending on the request. SO, I have
done this:

if I have have "my_action.rhtml" and "my_action_update.rjs",
then the controller method is :-

  def my_action
    .... action code...

    render :action => :my_action_update if request.xhr?
  end

This works, but is it the right way, or is there a better way?

Comments appreciated :slight_smile:
--
Posted via http://www.ruby-forum.com/.

Try "respond_to" - http://api.rubyonrails.com/classes/ActionController/MimeResponds/InstanceMethods.html#M000206.

def action
  ...action code...
  respond_to do |format|
    format.html # renders action.rhtml
    format.js # renders action.rjs
  end
end

Try "respond_to" -

Yes this worked, thanks!

Actually, it didn't work straight off. I discovered it only works if
the files are called "action.html.rhtml" and "action.js.rjs" instead of
"action.rhtml" and "action.rjs". I googled and found that this naming
dependency was introduced with rails 2.0.2.