链接时缺少必需的键

时间:2014-09-14 17:03:20

标签: ruby-on-rails ruby-on-rails-4

所以,我有一个应用程序,有电影制片厂,电影和剪辑。每个工作室都有一个相关的电影,每部电影都有相关的片段。此内容仅由网站管理员上传。现在,用户能够从电影中查看各种剪辑,并且每次用户观看剪辑时,都会创建“印象”。我正在使用印象派宝石来跟踪每个剪辑的视图。这与我的下一点有关。

在管理员后端我想让管理员选择一个剪辑并选择一个日期范围,然后当他们点击过滤器时,它将显示在该给定范围内查看剪辑的所有用户。

我以表格格式工作,但是,我无法显示实际电影或剪辑的链接,我能够用np显示指向Studio的链接。

我做错了什么?

<% if @impressions %>
<table>
  <tr>
    <th>Studio</th>
    <th>Film</th>
    <th>Clip</th>

  </tr>
<% @impressions.each do |impression| -%>
<% @clip = Clip.where(id: impression.impressionable_id).first %>
<% @user = User.where(id: impression.user_id).first %>

    <tr>
    <th>
    <%= link_to @clip.film.studio.name,  saas_admin_studio_path(@clip.film.studio) %> < --- works fine!
    </th>
    <th>
    <%= link_to @clip.film.name, saas_admin_studio_film_path(@clip.film) %> <--- doesn't work (see error #1)
    </th>
    <th>
    <%=  link_to @clip.name, saas_admin_studio_film_clips_path(@clip) %> <--- doesn't work (see error #2)
    </th>
<% end -%>
<% end -%>

错误#1

ActionController::UrlGenerationError in SaasAdmin::Statistics#search
Showing /Users/me/Sites/multi/app/views/saas_admin/statistics/index.html.erb where line #58 raised:

No route matches {:action=>"show", :controller=>"saas_admin/films", :format=>nil, :id=>nil, :studio_id=>#<Film id: 5, name: "blab", sort_name: "blab", description: "<p>bla</p>", short_description: "bla", meta_data: "bla", studio_id: 4, active: true, trackable: true, regions: nil, created_at: "2014-07-25 01:21:48", updated_at: "2014-07-25 01:21:48", poster_file_name: "ABeautifulMind-ManySolutions.png", poster_content_type: "image/png", poster_file_size: 331462, poster_updated_at: "2014-07-25 01:21:46", permalink: "blab", amazon_link: "">} missing required keys: [:id]

错误#2

    ActionController::UrlGenerationError in SaasAdmin::Statistics#search
Showing /Users/me/Sites/multi/app/views/saas_admin/statistics/index.html.erb where line #60 raised:

No route matches {:action=>"index", :controller=>"saas_admin/clips", :film_id=>nil, :format=>nil, :studio_id=>#<Clip id: 1, name: "test", sort_name: "test", permalink: "test", description: "<p>test</p>", duration: nil, film_id: 5, active: true, trackable: true, view_count: nil, status: nil, thumbnail_remote_file: "", created_at: "2014-07-25 01:22:12", updated_at: "2014-07-25 01:22:12", thumbnail_file_name: "ABeautifulMind-ManySolutions.png", thumbnail_content_type: "image/png", thumbnail_file_size: 331462, thumbnail_updated_at: "2014-07-25 01:22:11", impressions_count: 5, position: 1>} missing required keys: [:film_id]

我做错了什么?

谢谢!

修改:

Studio.rb:

class Studio < ActiveRecord::Base

  # attr_accessible :name, :code, :active, :trackable
  after_save ThinkingSphinx::RealTime.callback_for(:studio)

  has_many :films, :dependent => :destroy
  has_many :clips, :through => :films

end

Film.rb:

class Film < ActiveRecord::Base
  after_save ThinkingSphinx::RealTime.callback_for(:film)

  belongs_to :studio
  has_many :clips, :dependent => :destroy
  has_many :film_countries
  has_many :countries, :through => :film_countries, :source => :fcountry
 end

Clip.rb:

class Clip < ActiveRecord::Base
  after_save ThinkingSphinx::RealTime.callback_for(:clip)

  is_impressionable :counter_cache => true, :unique => :request_hash

  the impressionist gem is tracking all of the views for us. And what do u want to render ?

  a list of results, between the two dates of  usofe rwhat of users who viewed the clip u selected

  belongs_to :film
  has_one :video, :dependent => :destroy
  has_many :playlist_item
 end

编辑2:

路线:

sort_saas_admin_studio_film_clips POST     /admin/content/studios/:studio_id/films/:film_id/clips/sort(.:format)     saas_admin/clips#sort
       saas_admin_studio_film_clips GET      /admin/content/studios/:studio_id/films/:film_id/clips(.:format)          saas_admin/clips#index
                                    POST     /admin/content/studios/:studio_id/films/:film_id/clips(.:format)          saas_admin/clips#create
    new_saas_admin_studio_film_clip GET      /admin/content/studios/:studio_id/films/:film_id/clips/new(.:format)      saas_admin/clips#new
   edit_saas_admin_studio_film_clip GET      /admin/content/studios/:studio_id/films/:film_id/clips/:id/edit(.:format) saas_admin/clips#edit
        saas_admin_studio_film_clip GET      /admin/content/studios/:studio_id/films/:film_id/clips/:id(.:format)      saas_admin/clips#show
                                    PATCH    /admin/content/studios/:studio_id/films/:film_id/clips/:id(.:format)      saas_admin/clips#update
                                    PUT      /admin/content/studios/:studio_id/films/:film_id/clips/:id(.:format)      saas_admin/clips#update
                                    DELETE   /admin/content/studios/:studio_id/films/:film_id/clips/:id(.:format)      saas_admin/clips#destroy
            saas_admin_studio_films GET      /admin/content/studios/:studio_id/films(.:format)                         saas_admin/films#index
                                    POST     /admin/content/studios/:studio_id/films(.:format)                         saas_admin/films#create
         new_saas_admin_studio_film GET      /admin/content/studios/:studio_id/films/new(.:format)                     saas_admin/films#new
        edit_saas_admin_studio_film GET      /admin/content/studios/:studio_id/films/:id/edit(.:format)                saas_admin/films#edit
             saas_admin_studio_film GET      /admin/content/studios/:studio_id/films/:id(.:format)                     saas_admin/films#show
                                    PATCH    /admin/content/studios/:studio_id/films/:id(.:format)                     saas_admin/films#update
                                    PUT      /admin/content/studios/:studio_id/films/:id(.:format)                     saas_admin/films#update
                                    DELETE   /admin/content/studios/:studio_id/films/:id(.:format)                     saas_admin/films#destroy
                 saas_admin_studios GET      /admin/content/studios(.:format)                                          saas_admin/studios#index
                                    POST     /admin/content/studios(.:format)                                          saas_admin/studios#create
              new_saas_admin_studio GET      /admin/content/studios/new(.:format)                                      saas_admin/studios#new
             edit_saas_admin_studio GET      /admin/content/studios/:id/edit(.:format)                                 saas_admin/studios#edit
                  saas_admin_studio GET      /admin/content/studios/:id(.:format)                                      saas_admin/studios#show
                                    PATCH    /admin/content/studios/:id(.:format)                                      saas_admin/studios#update
                                    PUT      /admin/content/studios/:id(.:format)                                      saas_admin/studios#update
                                    DELETE   /admin/content/studios/:id(.:format)                                      saas_admin/studios#destroy

1 个答案:

答案 0 :(得分:1)

如果你看错了,他们会说:

  

缺少必需的密钥:[:id]

     

缺少必需的密钥:[:film_id]

所以问题是您的路由是嵌套的,并且您没有传递完整信息以形成正确的路由。试试这些链接:

<%= link_to @clip.film.name, saas_admin_studio_film_path(@clip.film.studio, @clip.film) %>
<%= link_to @clip.name, saas_admin_studio_film_clips_path(@clip.film.studio ,@clip.film) %>