我遇到了一個我無法解決的問題。我做了兩個模型;一個名爲Film的模型是父模型,另一個稱爲Review的模型是子模型。我在子模型上得到了驗證條件,但它不顯示在視圖上。來自子模型Ruby on Rails的錯誤驗證
電影模式
class Film < ApplicationRecord
has_many :reviews
validates_presence_of :filmtitle, presence: true
validates_presence_of :filmdescription, presence: true
validates_presence_of :filmdirector, presence: true
validates_presence_of :filmrating, presence: true
validates_presence_of :filmstarname, presence: true
end
審查模式
class Review < ApplicationRecord
validates :rating, presence: true
validates :commenter, presence: true
validates :body, presence: true
belongs_to :film
end
查看控制器
class ReviewsController < ApplicationController
def create
@film = Film.find(params[:film_id])
@review = @film.reviews.create(review_params)
redirect_to film_path(@film)
end
private
def review_params
params.require(:review).permit(:commenter, :body, :rating)
end
end
電影show.html.erb
<% if @film.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@film.errors.count, "error") %></h2>
<ul>
<% @film.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_for([@film, Review.new]) do |f| %>
<p>
<%= f.label :commenter %><br>
<%= f.text_field :commenter, :placeholder => 'Your name' %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body, :placeholder => 'Your comment' %>
</p>
<p>
<%= f.label :rating %><br>
<%= f.select :rating, ['1 Star', '2 Stars', '3 Stars', '4 Stars', '5 Stars'] %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
電影控制器
class FilmsController < ApplicationController
before_action :set_film, only: [:show]
# GET /films
# GET /films.json
def index
@films = Film.all.paginate(page: params[:page], per_page: 30)
@reviews = Review.new
end
# GET /films/1
# GET /films/1.json
def show
end
# GET /films/new
def new
end
# GET /films/1/edit
def edit
end
# POST /films
# POST /films.json
def create
end
# PATCH/PUT /films/1
# PATCH/PUT /films/1.json
def update
end
# DELETE /films/1
# DELETE /films/1.json
def destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_film
@film = Film.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def film_params
params.require(:film).permit(:filmtitle, :filmdescription)
end
end
route.rb
Rails.application.routes.draw do
resources :films do
resources :reviews
end
resources :rentals
resources :buys
resources :admin
resources :adminrentals
resources :adminfilms
resources :logins
resources :admins_login
resources :games
get '/adminCool' => 'admins_login#index'
get '/adminlogin' => 'admins_sessions#new'
post '/adminlogin' => 'admins_sessions#create'
get '/adminlogout' => 'admins_sessions#destroy'
get '/adminsignup' => 'admins#new'
post '/admins' => 'admins#create'
get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
get '/logout' => 'sessions#destroy'
get '/signup' => 'users#new'
post '/users' => 'users#create'
get '/cool' => 'logins#index'
end
你爲什麼有紅寶石上軌-3的Ruby-on-rails-它標記4,以及ruby-on-rails-3.2 ......這是你在所有版本上遇到的問題嗎?你的控制器的實際代碼在哪裏? – David
上面提到了兩個控制器。每次它擊中渲染功能時,它都會引導我到http:// localhost:3000/films/18/reviews地址。它假設回到http:// localhost:3000/films/18。我使用了Gaston給出的代碼(見下文)。 – javacoder