开发者

Setting up routes in RoR v3 with no views, just JavaScript?

This is probably a very simple question. I am new to Rails and have just hit my first major roadblock. I have two controllers, Groups and MembershipRequests. When someone submits a MembershipRequest, the admin of the Group has the option to accept or deny the request. In my MembershipRequests controller, I have two methods: accept and deny. They both work, but now I'm unsure of dealing with my routes.

This is the relevant part of my routes.rb:

resources :groups do
  member do
    get 'members'
    resources :membership_requests do
      member do
        post 'accept'
        post 'deny'
      end
    end
    ...
  end
end

Okay, onto my real question: I want to be able to accept and deny requests without an additional page. I want to use JavaScript to use a button on my MembershipRequests index page where the admin can accept or deny the requests.

Here's my erb code for the accept and deny buttons:

<%= link_to "Accept", :controller => 'membership_requests', :action => 'accept', :confirm => 'Are you sure?' %>

<%= link_to "Deny", :controller => 'membership_requests', :acti开发者_如何学Con => 'deny', :confirm => 'Are you sure?' %>

Clicking either of these links give me the No route matches "/groups/1/membership_requests/1/{accept,deny}" just like I would expect. But I do not know how to get around this. Thanks!


I think, link_to should also have :method => :post because you're mapping it to a custom (non-RESTful) action (that's just an assumption) and :remote => true, to tell rails that this link uses unobtrusive JS.

This screencast might help, or at least give a starting point.

Oh, and yes. As Ryan suggests, you'd better use something like this:

@requests.each do |request|
  link_to "Accept", membership_request_accept_path(request), :confirm => 'Are you sure?', :remote => true
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜