开发者

Is there a shortcut for sending AJAX commands?

So, My app uses a ton of AJAX, and I find myself doing this a lot:

    respond_to do |format|
      format.js do
        render :update do |pa开发者_运维知识库ge|
          #actual code
        end
      end   
    end

is there a shortcut?


This controller-inlined rendering of content is generally considered bad practice as is breaks the separation of concerns in the MVC architecture of Rails. This is especially so if there is lot of code each update and it occurs in a lot of controllers. That's going to quickly break down and be very difficult to maintain.

The shortest way (code-wise) to render is to take advantage of default view names in your actions like this for example:

def create
  @model = Model.new(...)

  respond_to do |format|
    format.js
  end
end

That's all that's needed to render from a view named create.js.erb. The advantage of using views is that, through partials you can keep the code DRY and easier to maintain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜