【问题标题】:Creating a general index when using nested routes使用嵌套路由时创建通用索引
【发布时间】:2013-07-07 03:41:00
【问题描述】:

我是 Rails 新手,也是第一次使用 pgsql。我的项目设置方式要求在没有艺术家的情况下无法创作歌曲。为了做到这一点,我使用了嵌套路由,所以 id 仍然存在。我的问题是我正在尝试为所有艺术家和歌曲创建一个索引,而不管艺术家是谁。但在这两种情况下我都会收到以下错误:

No route matches {:action=>"edit", :controller=>"users", :id=>nil}

在我的路线文件中:

resources :users do 
    resources :songs do 
      get 'approve', on: :member
      get 'decline', on: :member
    end
  end 

  resources :songs

在用户控制器中

def index
    @users = User.all 
  end


def edit
    @user = User.find(params[:id])
  end

在歌曲控制器中

def index
        @songs = Song.all 
    end
    def create
        @song = current_user.songs.build(params[:song])
        if @song.save
            #Send confirmation email
            @song.submit
            flash[:success] = "Song created!"
            redirect_to user_path(current_user)
        else
            render 'new'
        end
    end
def edit
        @song = current_user.songs.find_by_id(params[:id])
  end

在用户索引视图中

<%= render @users %>

和用户的局部视图

<li>
    <%= link_to user.name, user %>
    <% if current_user.admin? && !current_user?(user) %>
        |   <%= link_to "delete", user, method: :delete,
                                        data: { confim: "You sure?" } %>
    <% end %>
</li>

Users 和 Songs 索引视图都是相似的,所以我认为发布两者是多余的。

这是我的 development.log 文件的输出

Started GET "/users" for 127.0.0.1 at 2013-07-07 13:26:17 -0500
Processing by UsersController#index as HTML
  [1m[35mUser Load (0.3ms)[0m  SELECT "users".* FROM "users" 
  Rendered users/index.html.erb within layouts/application (0.6ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  [1m[36mUser Load (0.5ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."remember_token" = 'PTlmu8452Oan4mX1SLHOnA' LIMIT 1[0m
  Rendered layouts/_header.html.erb (3.9ms)
Completed 500 Internal Server Error in 62ms

ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"users", :id=>nil}):
  app/views/layouts/_header.html.erb:15:in `_app_views_layouts__header_html_erb___3568227543968708446_70195568870980'
  app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___4424773992888194134_70195568771540'


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)

索引与 postgresql 的工作方式不同吗?还是我在这里犯了错误或遗漏了什么?我从来没有遇到过 sqlite3 的这个问题,但就像我说的,这是我第一次使用 pgsql。我需要帮助,谢谢!

【问题讨论】:

  • 上面代码中的indexes 是controllers 上的actions 并且与数据库中的索引正交(无论引擎如何) )。
  • 好的@Johnsyweb,那我为什么会收到这个错误?我不认为我在这里做错了什么,所以它应该可以正常工作。
  • 只需阅读开发日志:错误来自第 15 行 (app/views/layouts/_header.html.erb:15) 上名为 _header 的部分。如果您还需要更多帮助,请将其粘贴。

标签: ruby-on-rails ruby ruby-on-rails-3 postgresql nested-routes


【解决方案1】:

您似乎正在尝试链接到没有 :id 参数的编辑页面,女巫不正确。

它应该是向右的路线:编辑操作,带有这样的 :id 参数,

<%= link_to "Edit user", edit_user_path(user.id) %>

【讨论】:

  • 如果您没有查看编辑链接,则不应引发路由错误路由到编辑链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 2011-08-01
  • 2017-03-08
  • 2015-07-01
  • 1970-01-01
  • 2013-08-19
相关资源
最近更新 更多