2013-11-03 27 views
0

我是非常新的鐵軌,試圖找出我的方式,但有一個路障,我引用這些rails params的方式有什麼問題?

我想顯示所有的電影從數據庫是一定的評級,基於複選框用戶檢查。下面是複選框和電影臺

-# This file is app/views/movies/index.html.haml 
%h1 All Movies 
= form_tag movies_path, :id => "ratings_form", :method => :get do 
    Include: 
    - @all_ratings.each do |rating| 
    = rating 
    = check_box_tag "ratings[#{rating}]", 1, rating 
    = submit_tag 'Refresh' 
%table#movies 
    %thead 
    %tr 
     %th Movie Title 
     %th Rating 
     %th Release Date 
     %th More Info 
    %tbody 
    - @movies.each do |movie| 
     %tr 
     %td= movie.title 
     %td= movie.rating 
     %td= movie.release_date 
     %td= link_to "More about #{movie.title}", movie_path(movie) 

= link_to 'Add new movie', new_movie_path 

的形式,並且這是電影控制器

class MoviesController < ApplicationController 

    def show 
    id = params[:id] # retrieve movie ID from URI route 
    @movie = Movie.find(id) # look up movie by unique ID 
    # will render app/views/movies/show.<extension> by default 
    end 

    def index 
     @all_ratings = Movie.all_ratings 
    @movies = Movie.where(:ratings) 
    end 

    def new 
    # default: render 'new' template 
    end 

    def create 
    @movie = Movie.create!(params[:movie]) 
    flash[:notice] = "#{@movie.title} was successfully created." 
    redirect_to movies_path 
    end 

    def edit 
    @movie = Movie.find params[:id] 
    end 
    def update 
    @movie = Movie.find params[:id] 
    @movie.update_attributes!(params[:movie]) 
    flash[:notice] = "#{@movie.title} was successfully updated." 
    redirect_to movie_path(@movie) 
    end 

    def destroy 
    @movie = Movie.find(params[:id]) 
    @movie.destroy 
    flash[:notice] = "Movie '#{@movie.title}' deleted." 
    redirect_to movies_path 
    end 
end 

因爲它是我無法弄清楚,爲什麼有沒有顯示出電影,肯定有一些在數據庫。我覺得評論中沒有任何內容,但我不確定是否有任何其他參考方式

回答

0

首先將列評分移至評分。

= form_tag movies_path, :id => "ratings_form", :method => :get do 
    Include: 
    - Movie.all_ratings.each do |rating| 
    = rating 
    = check_box_tag "ratings[#{rating}]", 0, rating 
    = submit_tag 'Refresh' 

並在控制器,你在等級

的陣列
class MoviesController < ApplicationController 

    def index 
    @movies = Movie.where(rating: params[:ratings].keys) 
    end 

    #.... 
end 

一件事發現 - 你送評級PARAMS才把空數組! 簡單的解決這個問題,通過:

class MoviesController < ApplicationController 

    def index 
    if params[:ratings].present? 
     @movies = Movie.where(rating: params[:ratings].keys) 
    else 
     @movies = Movie.all 
    end 
    end 

    #.... 
end 

或做在模型範圍(!它是更好的選擇)

將是巨大的,如果有人添加更多的優雅的解決方案:)

+0

好了,所以這似乎讓我到某個地方,但現在我得到錯誤「SQLite3 :: SQLException:沒有這樣的列:G:SELECT」電影「* FROM」電影「哪裏(G)」當我點擊窗體上的刷新按鈕。它第一次加載罰款,但如果更改複選框,並刷新它似乎崩潰。 – jankyd

+0

我在我的模型中有一個錯字,並且錯過了「:」,您發佈的代碼正常工作!非常感謝 – jankyd

+0

不客氣:) 你有沒有完成rake db:migrate並且你有電影遷移? 請遷移後重新啓動服務器。 – mike90