Ruby on rails data fetch to another controller's view - ruby-on-rails-4

I have two controllers and two view: homepage and joblist.
I have a career.html.erb file and index.html.erb in homepage view. In career.html.erb, I have created a simple HTML form with labels and textboxes.
Homepage controller is a simple one with only view files. joblist controller is scaffolded. I have created jobs as well.
Now I just want my carrer.html.erb file form in homepage view to access and show in label the 'jobtitle' filed value of joblist controller.
Ho can I do that?

It is bit tricky. Whenever user made a request to webpage from browser, rails server map that request to controller actions by using mapping defined in routes.rb file. Routing tells rails application this request is for what controller. Now rails engine instantiate that particular controller and call action method on that controller object. SO NO METHOD OR VARIABLE IS NOT AVAILABLE IN VIEW. You can only get variables from that particular action.
For more details ruby rails web request response lifecycle
Views are rendered by controllers. You set instance variables (variables started by # sign) in controller and use these variables in view. So you need to set variables in same controllers from where you render views.

Related

Wanted to call action of the linking controller in ember js?

{{#link-to "login"}}<span {{action 'loginfunc'}}>Login</span>{{/link-to}}
the above code is in my ember signup template , i have linked to login template.
I wanted to call my 'loginfunc' in my login controller, but the complier is checking 'loginfunc' inside the sign up controller, can you please tell me why, i'm a beginner. someone guide me with this.
Welcome to SO as well as the Ember community.
By default, in Ember, the structure for a page (say, signup) would be like,
signup template: UI markup layout
signup route file: Handles the routing and data fetching part
signup controller file: Handles the data processing and act as backing class
The things you refer to in a template will usually be looked up in the backing "controller" file. Hence, if you refer to an action in the signup template will be looked up in the signup controller.
If you want to call a method in login controller after transitioning to login route, try invoking it inside one of the dedicated hooks/events such as setupController, activate etc., inside the login route file.

Namespacing, sharing models, reusing controller in Ember

I have an Ember 1.10 application with api support for 2 models, Students and Parents, and basic CRUD setup for each model, as well as the ability to read and write messages to/from Students and Parents when visiting their respective /show pages.
I must create an Inbox in which the messages as well as all the other data on Students and Parents is available and must do so without the ability to change or add to the api (i.e. I can' create a Messages endpoint).
Basically, I need to keep the Student and Parent models and controllers as they are, but need to be able to access them from routes namespaced under the Inbox like so:
Students
-inbox
-index
-edit
-show
Parents
-inbox
Inbox
StudentInbox <-- should use StudentsRoute and StudentsController
-index
-show
-edit
ParentInbox
-index
-show
-edit
So that visiting /inbox/students/123 will be using the model defined in the StudentRoute and the actions and properties defined on the StudentController. The InboxStudentsShowRoute should similarly have access to the StudentsShowRoute and Controller.
Instead of copying and pasting the existing code from the Student and Parent routes and controllers, is there a way to proxy actions fired from templates under the Inbox namespace to the relevant models and controllers and maintain the ability to pass in params.
For example, passing in params like ?unread=true while on the InboxStudentsShow route would pass these to the StudentsShowRoute and Controller (and bubble appropriately to it's parent route and controller) or editing a Student from student/inbox/123/edit template would work just as it does on the student/123/edit template?
New to Ember and know there has to be a better way to reuse existing code and proxy actions from a namespace. Any advice would be greatly appreciated!
I'd go here with creating a Mixin which holds same code for Route and specify controllerName, templateName in InboxStudentsShowRoute. Then, you extend from this Mixin in both routes. Model, template, actions, properties will be the same.
For example, passing in params like ?unread=true while on the
InboxStudentsShow route would pass these to the StudentsShowRoute and
Controller (and bubble appropriately to it's parent route and
controller)
I'm not sure what do you mean here by pass these to the StudentsShowRoute and Controller, but CONTROLLER properties changed in one route should remain the same in another route, because controllers are singletons in Ember. I don't know what do you mean by passing query param to another route, because if you transition to other part of your app you change URL and you can't have 2 urls at the same time, so how could query params be passed to another route?
In Ember 1.10 query params are defined in Controllers so I guess behaviour will remain the same.
Example of InboxStudentsShowRoute(move code from StudentsShowRoute to StudentsShowMixin, define controller and template name):
import Ember from 'ember';
import StudentsShowMixin from '../mixins/students-show';
export default Ember.Route.extend(StudentsShowMixin, {
controllerName: 'students/show',
templateName: 'students/show'
});

Replace, not append to, the site's body content in Ember.js

I had the following idea: my page at example.org serves classic HTML from the server. Besides, EmberJS is loaded, too, and waiting to come into action:
as soon as somebody hits an ember route then, for example example.org/#/login, the current should be replaced by what the view renders for it. From then, the whole app should serve as one-page-app.
Is that a good idea? Anyway, I don't know how to get that started. Overriding View's appendTo method or setting the rootElement property as in http://emberjs.com/guides/configuring-ember/embedding-applications/ does not suffice because if that were the body, the view output is just appended thereā€¦
If your entire Ember application requires a user to be logged in, it is valid to have two separate "apps":
A regular non single-page application (server-side using rails, PHP or C#) with a sign up and login pages
A single-page application (i.e. Ember) send as soon as the user hits the login button in your regular app
You will have 2 index.html pages, one for each application (and it's okay to do that!). The URL of the Ember App could be under example.org/app/.... You will need to configure the router of the regular application to server your Ember App for all URLs starting with /app/.
Does that help? :)

render visualforce page using ember.js mvc framework

How can we render visualforce page by using ember.js framework?
How can I bind server(apex controller) data with model so when
template will load then data should be display on page? My server
data is coming in json hierarchical form.
How can we assign more than one json variable to template.
As an answer to 1. check out https://github.com/mickle00/salesforce-emberjs. This repository contains a barebones ember.js app in a visualforce page.
When you build your Ember App in this visualforce page you can use javascript remoting to call your controller methods.
By the way, you'll probably get a better answer on the salesforce stackexchange

Accessing Controller Variables in View

Please see: http://jsfiddle.net/ptmoy2/9RRfJ/
I'm using Ember 1.4 in my app. I can display the controller variables in the view's template, but I can't access the same variable from the view. It comes out as 'undefined' when tried outputing to console.
console.log(this.get('dummy'));
I haven't defined a backing model because I don't really need anything persisted in this particular route.
How do I access controller variables from an associated view?
Use this.get('controller.dummy')