为什么show helper不能导致show.html.erb而是index.html.erb?

时间:2019-07-11 01:34:52

标签: ruby-on-rails

这是引起我麻烦的代码:

  <% @user.goals.each do |goal| %>
    <tr>
      <td><%= link_to goal.text, user_goals_path(@user, goal)%></td>
    </tr>
  <% end %>

我认为user_goals_path(@user,@goal)是正确的,但是显然它一直在访问index.html.erb。

我在控制器中的显示和索引:

    def index
        @user = User.find(params[:user_id])
        @goal = @user.goals
    end

    def show
        @user = User.find(params[:user_id])
        @goal = @user.goals.find(params[:id])
    end

任何帮助将不胜感激。

更新:

感谢您的评论。这是我的路线:

               user_goals GET    /user/:user_id/goals(.:format)                                                           goals#index
                          POST   /user/:user_id/goals(.:format)                                                           goals#create
            new_user_goal GET    /user/:user_id/goals/new(.:format)                                                       goals#new
           edit_user_goal GET    /user/:user_id/goals/:id/edit(.:format)                                                  goals#edit
                user_goal GET    /user/:user_id/goals/:id(.:format)                                                       goals#show
                          PATCH  /user/:user_id/goals/:id(.:format)                                                       goals#update
                          PUT    /user/:user_id/goals/:id(.:format)                                                       goals#update
                          DELETE /user/:user_id/goals/:id(.:format)                                                       goals#destroy

和route.rb:

Rails.application.routes.draw do
  devise_for :users

  resources :user do
    resources :goals
  end

  devise_scope :user do
    authenticated :user do
      root 'home#index', as: :authenticated_root
    end

    unauthenticated do
      root 'devise/sessions#new', as: :unauthenticated_root
    end
  end
end

如评论中所述,user_goal_path(@user,目标)在目标列表中生成错误:

No route matches {:action=>"show", :controller=>"goals", :id=>nil, :user_id=>"1"}, missing required keys: [:id]

更新2:

这可能是问题吗?这是我的架构。根据我的目标,您看到这里有什么问题吗?

  create_table "goals", force: :cascade do |t|
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id"
    t.index ["user_id"], name: "index_goals_on_user_id"
  end

3 个答案:

答案 0 :(得分:0)

user_goals_path(@user, goal)似乎是自动生成的:index操作的路由。

如果要执行:show操作,则可能应该是user_goal_path(@user, goal)才能显示用户目标。

就像卢卡斯K说的那样,发布您的rake routes输出可以帮助进一步诊断。

答案 1 :(得分:0)

对于此路由,仅需要动态传递user_id个参数

           user_goals GET    /user/:user_id/goals(.:format)     goals#index

应该是

<td><%= link_to goal.text, user_goals_path(@user)%></td>
<td><%#= link_to goal.text, user_goals_path(@current_user)%></td>

对于路由,它需要user_idid参数动态传递

            user_goal GET    /user/:user_id/goals/:id(.:format)     goals#show

所以会

<td><%= link_to goal.text, user_goal_path(@user, @goal)%></td>
<td><%#= link_to goal.text, user_goal_path(@current_user, @goal)%></td>

答案 2 :(得分:0)

尝试浅层嵌套资源后,可以使用以下方法从列表移至显示页面:

  <% @user.goals.each do |goal| %>
    <tr>
      <td><%= link_to goal.text, goal_path(@user,goal)%></td>
    </tr>
  <% end %>