I'm playing around with angular in my rails app. This is not a single page app, i'm just using angular in a few places. I'm having a hard time getting route params from the uri to use in my resources.
For instance, say I'm on the page /users/1/posts/2. How do I get both user_id and id(for post)?
Thanks.
You could configure your angular app to pass the params to the controller your using for that page.
I would make a routes.js file doing something like:
App.config(['$routeProvider', function($routeProvider){
$routeProvider
.when('/your-route', {
controller: 'yourNGController',
resolve: {
params: ['$route', function($route){
params = {user_id: $route.current.params.user_id}
params.merge({post_id: $route.current.params.post_id})
return params
}]
}
})
}])
Then in your controller - "yourNGController' inject 'params' that you resolved in your route. That should get your the info you need into the controller.
If using newer versions of angular you may also need to include the angular-route.js as $routeProvider was removed from the core angular components.
I ended up just using ui-routes which makes this problem a whole lot easier.
I had a similair issue, and it is linked here: Passing Rails ID to Angular
I consider this solution a bit of a hack, so am interested in better approaches.
Related
I'm trying to use pure Rails routes with AngularJS controllers. How would I grab the product id (30) of /products/30 in my Angular controller if the route is being generated by Rails?
app.controller('ProductCtrl', ['$scope', function($scope) {
// grab id param
// and then do something like Product.query({id: id_param});
}]);
I have the same issue as this guy: Rails Route parameters in AngularJS
If you don't want to do something like embedding the ID in a attribute (as discussed in Rails Route parameters in AngularJS) I would probably end up using $location.path() to grab the path and splitting it up as needed.
I want to use angular resource to interact with my rails backend, the build-in $resource service is not fully compatible with rails API, like PUT is not support by default, I have to add custom action "update" with PUT method.
This problem with this approach is I have to add update action for every resource to make angular resource align with the rails API backend.
Is this the good approach to go?
I also found a angular resource wrapper angularjs-rails-resource, which provide a update method with PUT http verb, but it seems like the way how it handle parameters passing is a bit odd. for example, it wrap the parameter with a "undefined" key.
Parameters: {"undefined"=>{"username"=>"xxxx"}, "version"=>"1", "id"=>"88"}
So, the question is what is the best practice to use angular resource with rails API?
If you don't want the overhead of restangular and just want to add the update method to every resource, you can simply add a decorator to the $resource service.
Something like this:
.config(function ($provide) {
$provide.decorator('$resource', function ($delegate) {
//Store the delegate, so we can access it later
var resourceFactory = $delegate;
//Add the actions that you want added to each Resource here
var default_actions = {'update': {method: 'PUT'}};
return function (url, paramDefaults, actions) {
actions = angular.extend({}, default_actions, actions);
return resourceFactory(url, paramDefaults, actions);
};
});
})
Use angular-rails-resource. It is optimized for the Rails philosophy, which means that you don't have to juggle around with request/response format by yourself. And it supports a lot of other cool features, like nested resources, automatic name conversion, etc...
Take a look on restangular. It's really a nice solution for interacting with rest api.
I'm planning to port our current cms (written in PHP) to Rails. All parts do well, except for one: routing.
Like most cms systems, the routing in our cms based on a database with pages, which are linked to modules, controllers and actions. On this approach a user can fully customize or specify it's own urls.
I know that Rails (and most (application) frameworks have the approach of defining routes in a file, but I hope this is possible.
The approach our users should have is:
add new page
select type (news, form, product, ...)
select an item (select which form, blog or product should be displayed)
enter a url for that page
Special the last point (4) is important. A user should be able to add form A to /contact-us, and form B to /clients/register-as-new-client e.g.
On a request the router needs to do a database query with the page url, to find out which controller, task and parameters should be dispatched.
Question has been updated, and i don't think this is a valid answer anymore
we have a similar paging system. we use a routing glob. in routes.rb:
get 'pages/*lookup_path', to: 'pages#show', defaults: { format: 'html' }, as: 'page'
Just parse params[:lookup_path] in PagesController to suit your needs
'http://localhost/pages/users/'
params[:lookup_path] #=> users/
'http://localhost/pages/users/23'
params[:lookup_path] #=> users/23
'http://localhost/pages/people/1'
params[:lookup_path] #=> people/1
Although this solution isn't ReSTful, I think this should solve the issue.
Regardless, Rails uses routes in a file. You cannot change this since the framework heralds "convention over configiuration". All I can do is point you in a direction to minimize this.
There is a catchall route in Rails (on RailsCasts, and on StackOverflow) which you can use to direct all routing to one controller action. You may further customize the routing behaviour in that method
You could also make a route like…
:controller/:action => Controller::Action
…as is done in CodeIgniter, but now your methods have to have names like contact-us and register-as-a-new-client.
Just as you figure out the route when the browser hits the webpage in Rails, how would you resolve it on the server side?
For example I want to return a URL to a RESTful resource called Bookmark in an API call and want to return the 'show' action of it, and I know that:
Bookmark id: 12
Then I want to resolve it to a string:
'/bookmarks/edit/12'
so that I can get this from my Model for example.
How would I go about doing this?
Thanks!
Pretty much everywhere in the views/controllers you can use route helpers to DRY up route references.
In models, you'll need to explicitly call the route helper like so.
Rails.application.routes.url_helpers.edit_bookmark_path(id) # => '/bookmarks/12/edit'
When using the default resourceful route generator method in routes.rb like
resource :bookmarks
I'm not sure I understand - your server is the thing that's making all of those routes work - the client's browser isn't figuring out what the route is - you application is doing it.
The paths to your resources are available as helper methods at all times (at least within the controllers, and views). As such, you should return the string as the body of a response in one of your actions, in the controller that's handling your API calls.
Check your rake routes on the command line, and you'll see a list of them. In the case of your example above, it would likely be called edit_bookmark_path(12)
I want to use ExtDirect for a 3rd party extjs user interface in Rails 3. So I have started to update the active-direct gem to work with Rails 3. Here is the updated version: https://github.com/stonegao/active-direct
At the moment my modified active direct plugin/gem works with models. I'm able to do this in JavaScript:
App.namespace.Project.all({params},callback_function);
That's great.
Now I want to use some special Rails 3 controllers (that act like a service).
In my Extdirect JS is this:
App.mynamespace.MyProject_Controller_V1_workspaceController.getStatus
This response also comes to my extjs router. No I want call this controller action and get the response.
I can't use #app.call(env) with a changed request_uri because I have no match in routes.rb
Is it possible to call this controller action
Specification of extdirect: http://tinyurl.com/4y3nc44
Thanks skeller1
yes, it is!
just do something like this:
status,headers,response=ProjectsController.action("index").call(#env)
so you can call the index method of the ProjectsController.
beware: ExtDirect accepts all types in the HTTP_ACCEPT http header field.
so you can do a
#env['HTTP_ACCEPT'] = "application/json"
before your request to an action to set your wanted response type.