简体   繁体   中英

404 for route that exists according to rake routes

I created a member route on a resource called groups in my Rails app.

  resources :groups do
    member { post :vote }
  end 

If I do rake route, it shows that this route exists

   vote_group POST   /groups/:id/vote(.:format)        groups#vote

In Ember, I created a GroupController

App.GroupController = Ember.ObjectController.extend({

    actions: {

    vote: function() {

    $.post("vote_group_path", {
      username: this.get("username"),
      id: this.get("id")
     ....

However, when I click submit on the form, I'm getting a no-route matches error

ActionController::RoutingError (No route matches [POST]"/vote_group_path"):

I'm wondering if this is because I'm not specifying which group through the inclusion of an id. In the template that shows each group, I'm able to display the name and the id

<script type="text/x-handlebars" id="group">

      {{ model.name }}
      {{ model.id }}
      {{ partial 'groups/form'}} 

</script>

but I'm not sure how to include the id for the group as a kind of hidden element in the form (if that's even necessary to make the route work)

<script type="text/x-handlebars" id="groups/_form">
<form class="form-horizontal" {{action "vote" on="submit"}}>
   <div class="controls">
      {{input value=username type="text"}}
  </div>

  <button type="submit" class="btn">follow</button>
</form>
</script>

I know I'll eventually need the group id in the vote action of the groups controller, but I'm not sure if the lack of the id is making the route appear not to exist

 def vote
    @group = Group.find(params[:id])
    ...

The issue is that vote_group_path is a helper method generated by Rails that can only be used inside of the Rails app. Let's say that you have a group with an id of 1 assigned to the some_group variable. Inside of the Rails app, if you call vote_group_path(some_group) it will return the string '/groups/1/vote' . That helper function does not cross the application layer boundary into your JavaScript. In JS you'll want to manually build the full path of '/groups/1/vote' .

Something like :

$.post("/groups/" + this.get('id') + "/vote", {...});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM