Saturday, 15 February 2014

ruby on rails 3 - Is there any issue with using respond_to blocks in controller actions like this? -



ruby on rails 3 - Is there any issue with using respond_to blocks in controller actions like this? -

i'm ajaxifying existing rails 3.2 application, , of requests made client improve done asynchronously. facilitate this, , cutting downwards on rendering time, i'm giving feedback user via alerts. have blank div in application.html.erb file add together alerts needed controller actions so:

def my_async_controller_action @user = user.find(params[:id]) begin #@user.import flash[:notice] = "your info beingness downloaded." rescue exception => bang flash[:alert] = "there error downloading data: #{bang.message}" end respond_to |format| format.js { render 'common/flashes' } end end

and common/flashes file uses jquery append alerts blank div. while works fine, have never seen alerts delivered this, via redirects. there unwritten (or written) rails convention or rule i'm breaking taking approach? also, there way instead respond_with? can't see how 2 different types of rendering single line.

if js file rendering has same name action of controller, don't need respond_to block.

however, since placing js file in mutual folder, reuse among controllers, have explicitly tell rails render file. totally fine.

if action responds js, can utilize brackets instead of do ... end block create 1 liner:

respond_to { |format| format.js }

however, since have specify name of js file, can quite ugly, can omit respond_to block , following:

render 'common/flashes'

this, however, has inconvenient if controller receives html request, search file named flashes.html within mutual folder. homecoming template missing error. if want prepare that, have add constraints format.

however, if action responds html, should declare block this:

respond_to |format| format.html format.js { render 'common/flashes' } end

you don't need format.html, when @ code, can tell responds both html , js requests.

using respond_with block render different files isn't applicable in case, since rendering javascript. respond_with used homecoming resource in various formats (html, json, xml). this article goes little more detail.

ruby-on-rails-3 coding-style ruby-on-rails-3.2 convention-over-configur

No comments:

Post a Comment