jquery - How to render another page (Controller Action + View) inside current view -


let's have userprofilecontroller#show renders user's profile account page. associated views , partials controller under /app/views/user_profile/*

as userprofile page, user should able edit preferences. done clicking preferences link triggers userpreferences#show , displays whole new page rendering views under /app/views/user_preferences/*

my current issue latter page whole separate page have link to. i'd love instead able bring preferences page in modal without leaving user profile page. or maybe want embedded in profile page @ bottom (i.e. doesn't have popup modal - wasn't looking specific answer that).

the issue have no way call whole new controller , action current userprofilecontroller view. there way call new route , have generate whole new page within current page?

the answer i've received far should consider doing same controller, that's overloading userprofilecontroller , action method becoming long , messy.

thanks!

you're thinking wrong; you're not dealing "pages" controller actions , views. subtle important difference:

enter image description here

rails mvc (model view controller) framework. it's not unique in sense, since many rails devs don't have experience elsewhere, have learn "mindset" of mvc first hand.

as can see above, can't render "page" , see data need. need populate view data (done in controller), allowing rails send relevant html etc in browser.

thus, have ask how you're going to:

  1. populate data user_preferences view
  2. render view in browser

partials

the feature you'll need partials.

partials allow call "sub view" in main view. have provide data etc, means can render things such menus , profiles inside other views, reusing same code each time (dry - don't repeat yourself).

partials simple create - prepend underscore name:

#app/views/user_preferences/_menu.html.erb user preferences partial. can call in view make sure have correct data supplied! 

now, important part in how invoke partial:

#app/views/user_profile/index.html.erb <%= render "user_preferences/menu", locals: {user: @user} %> 

you'll able populate @user variable inside users controller:

#app/views/user_profiles_controller.rb class userprofilescontroller < actioncontroller::base    def index       @user = user.find x    end end 

--

ajax

the alternative use ajax , download entire user_preferences view on demand. not recommend this instance.

ajax sends asynchronous (parallel) request server browser. response received appended page. it's used provide snippets of functionality (loading user data etc).

ajax uses javascript, , have done this:

#app/assets/javascripts/application.js $(".some_button").on("click", function() {     $.ajax({         url: "path/to/user_preferences",         success: function(data) { // here }     }); }); 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -