【问题标题】:Route in rails is not working轨道中的路线不起作用
【发布时间】:2015-08-29 13:22:11
【问题描述】:

我使用 devise gem 进行身份验证。在同一个项目中,我使用了专辑模型。但是我的 new_user_album_path 出错了。我认为有用户 ID 问题。请帮助我。

这是 index.html.erb 文件

<%= link_to 'New Article', new_user_album_path() %>
<table>
<tr>
 <th>Title</th>
<th>Description</th>
 </tr>

  <% @albums.each do |album| %>
  <tr>
  <td><%= album.title %></td>
    <td><%= album.description %></td>
  <td><%= link_to 'Show', user_album_path(album.user,album) %></td>
 <td><%= link_to 'Edit', edit_user_album_path(album.user,album) %></td>
  <td><%= link_to 'destroy', user_album_path(album.user,album),
    method: :delete,
    data: { confirm: 'Are you sure?' } %></td>
   </tr>

   <% end %>
   </table>



     this is albums_controller file

   class AlbumsController < ApplicationController
     before_action :authenticate_user!

   def index
    @albums = current_user.albums.all

       end

        def show
         @album = current_user.albums.find(params[:id])
       end

    def new
    @album = current_user.albums.new
   end

    def edit
    @album = current_user.albums.find(params[:id])
    end

   def create

   @album = current_user.albums.build(album_params)
   if @album.save
   redirect_to action: 'index'
   else
      render 'new'
   end
  end

     def update
    @album = current_user.albums.find(params[:id])

      if @album.update(album_params)
      redirect_to action: 'show'
      else
      render 'edit'
      end
     end

     def destroy
      @album = current_user.albums.find(params[:id])
      @album.destroy
     redirect_to action: 'index'
     end
    private
    def album_params
      params.require(:album).permit(:title, :description, :user_id)
    end

   end

   it is giving following errror.->


       NoMethodError in Albums#index



    undefined method `users' for nil:NilClass

    Extracted source (around line #2):

       <h1>Your Albums</h1>
    <%= link_to 'New Article', new_user_album_path(@album.users.id) %>
       <table>
       <tr>
          <th>Title</th>
        <th>Description</th>

route.rb file

    devise_for :users
   # The priority is based upon order of creation: first created priority.
 # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
   resources :users do
   resources :albums
   end

   root 'albums#index'

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise


    【解决方案1】:

    当路由设置正确时,问题应该如下:

    new_user_album_path()
    

    请求嵌套资源的路径,并且需要获取父对象,即您的用户:

    new_user_album_path(user_id: current_user.id)
    

    我认为即使这样也行:

    new_user_album_path(current_user)
    

    如果这不起作用, 您应该通过从 shell 调用来检查路由是否像您认为的那样

    rake routes 
    

    在输出中查找new_user_album_path

    • 真的存在吗?
    • 所有名称部分的单数和复数是否相同?
    • 需要什么参数?

    【讨论】:

    • (user_id: current_user.id)
    • 不工作
    • 您可以通过对对您有帮助的答案投票来感谢我(以及其他帮助您的 stackoverflow 用户)。如果问题解决了,您可以“接受”一个答案,然后其他用户会看到该问题不再对您开放。
    【解决方案2】:

    其他内容可能不正确,但在您的albums#index 中您定义了变量@albums,但在您的链接中您使用了@album

    更新

    &lt;%= link_to("New Article, new_user_album_path(@album.users.id)

    &lt;%= link_to("New Article, new_user_album_path(@albums.users.id)

    收到不同的错误。对您的目标进行更多解释会有所帮助。 :)

    【讨论】:

      【解决方案3】:

      请改成:

      <%= link_to 'New Article', new_user_album_path(current_user) %>
      

      【讨论】:

      • #<:activerecord_associationrelation:0x000000033ddaf8> 的未定义方法“用户”
      • 是的,@albums 是“关系”或专辑列表。该列表没有用户属性,只有单数专辑
      猜你喜欢
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      相关资源
      最近更新 更多