0

这些是我路线的一部分。

   activity_groups GET    /activity_groups(.:format)                                activity_groups#index
                           POST   /activity_groups(.:format)                                activity_groups#create
        new_activity_group GET    /activity_groups/new(.:format)                            activity_groups#new
       edit_activity_group GET    /activity_groups/:id/edit(.:format)                       activity_groups#edit
            activity_group GET    /activity_groups/:id(.:format)                            activity_groups#show
                           PUT    /activity_groups/:id(.:format)                            activity_groups#update
                           DELETE /activity_groups/:id(.:format)                            activity_groups#destroy
                      root        /                                                         main#index

最小视图:

活动组/显示

%h2 Activity Ggroup

%h3= @activity_group.title

= link_to "Edit", edit_activity_group_path(@activity_group)
|
= link_to "Delete", activity_group_path(@activity_group), {confirm: (I18n.t "confirmations.activity_group.delete"), method: :delete}
|
= link_to "Back", activity_groups_path

索引有点复杂:

活动组/索引

.row
    .span8
        %h1.padding_bottom1 Activity Groups

        - if !@activity_groups.empty?

            %ul.span6
                %li.span1 F
                %li.span2 Name
                %li.span2 Actions

            = render @activity_groups
            = render @activities
        - else
            %h2.extra_padding You have no Activity Groups yet. Add one and start managing your time. 

    .span6.padding_top1
        =link_to "Create Activity Group", new_activity_group_path, {class: 'btn'}
        =link_to "Create Activity", new_activity_path, {class: 'btn'}

activity_groups/_activity_group.html.haml

%ul.span6
    %li.span1
        %i.icon-folder-close.folder
    %li.span2
        = activity_group.title
    %li.span2
        PROBLEM

    - if !activity_group.activity_groups.empty?
        - activity_group.activity_groups.each do |activity_group|
            = render partial: 'activity_group', locals: {activity_group: activity_group}

    - if !activity_group.activities.empty?
        - activity_group.activities.each do |activity|
            = render partial: 'activities/activity', locals: {activity: activity}

问题如果我放在link_to "Show", activity_groups_path(activity_group)这里,我的路线会发疯。我的 activity_groups_path 不见了。我不能再访问它了。它试图将我重定向到显示视图而不是索引视图。我真的不明白为什么会这样。有任何想法吗?

[编辑1]

我从一个干净的数据库开始。如果我现在尝试为显示页面放置一个链接,我会得到一个 NameError undefined local variable or methodactivity_group_path'`

[EDIT2]更正了上面的拼写错误

4

2 回答 2

1

您是否将对象传递给您的链接路径?

activity_group_path(@activity_group)
于 2012-10-10T01:02:26.570 回答
1

你提到你在activity_groups_path(activity_group)上面使用过 - 这应该是activity_group_path(activity_group)(组而不是组)。可能是 /an/ 问题,但可能不是唯一的问题。(也许不是唯一的,听起来像您对相同部分的递归调用并将子 activity_groups 传递为 :local 最终可能会使用无效的活动组对象调用路由。只是预感)

顺便说一句,我在 Rails 路线上度过了许多类似的沮丧时光,它总是归结为我所做的一些愚蠢的事情(尽管语法也是罪魁祸首,它只是英语的一个糟糕的近似值)。休息一下,吃点东西,打个盹……那时可能很明显。

于 2012-10-10T01:16:47.077 回答