开发者

Link_to_remote with Nested Routes

开发者 https://www.devze.com 2022-12-20 02:17 出处:网络
I\'m trying to figure out how to structure my path on a link_to_remote tag to accommodate nested routes. I have an article model that belongs to a group model, and the article has votes associated wit

I'm trying to figure out how to structure my path on a link_to_remote tag to accommodate nested routes. I have an article model that belongs to a group model, and the article has votes associated with it (using the Vote_Fu plugin). I created the code for articles first and it worked, but in the process of adding the group model and updating my paths for everything, the link below is now broken. I know it's looking for new_question_path which won't work anymore, but I can't figure out what to replace it with.

<%= link_to_remote "+(#{@article.votes_for})",   
  :update=>"vote", 
  :url => { :controller=>"articles",
            :action=>"vote",  
            :id=>@article.id,  
            :vote=>"for"},
            :html => { :class  => "up" } %>

Any help would be awesome. Thanks!

UPDATE:

Looks like the issue was in my routes. I have a vote method in my articles controller, but it didn't开发者_C百科 know to look for that. I changed my routes.rb file to this:

group.resources :articles, :member => { :vote => :get }

Looks like the problem might be solved.


Yeah, that :member option in your router will expose the vote action as a valid path. But I think you might also want to consider adding a Vote model and then saying Article has_many :votes.

You would then have a VotesController and your router would have map.resources :articles, :has_many => :votes, allowing for urls such as /articles/1/votes etc.

You might consider it overkill to have a separate model for Votes, but over the life of your app it may prove useful to track who voted for what when, etc.

Then again if your site is very small, you may never need that functionality. This is the kind of tradeoff you have to consider though when modeling your domain. Using RESTful resources just highlights the fact that you're breaking from the norm :)

0

精彩评论

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