我有一个名为" Rating" (具有标准操作:create
,update
,destroy
)。我有一个此资源的表单,在另一个控制器(称为MoviesController
)中呈现。
如何从RatingsController
内访问@rating
MoviesController
?
RatingsController
:
class RatingsController < ApplicationController
before_action :set_rating, only: [:update, :destroy]
before_action :authenticate_user!
def create
@rating = Rating.new(rating_params)
@rating.rater = current_user
respond_to do |format|
if @rating.save
format.html do
redirect_to movie_path(id: @rating.rottentomatoes_id),
notice: 'Rating was successfully created.'
end
else
format.html { redirect_to movie_path(id: @rating.rottentomatoes_id) }
end
end
end
def update
respond_to do |format|
if @rating.update(rating_params)
format.html do
redirect_to movie_path(id: @rating.rottentomatoes_id),
notice: 'Rating was successfully updated.'
end
else
format.html { redirect_to movie_path(id: @rating.rottentomatoes_id) }
end
end
end
def destroy
@rating.destroy
respond_to do |format|
format.html do
redirect_to movie_path,
notice: 'Rating was successfully destroyed.'
end
end
end
private
def set_rating
@rating = Rating.find(params[:id])
end
def rating_params
params.require(:rating).permit(:rottentomatoes_id, :rating)
end
end
MoviesController
:
require 'httparty'
class MoviesController < ApplicationController
before_action :authenticate_user!
def show
movie_url = 'http://api.rottentomatoes.com/api/public/v1.0/movies/'
api_key = ''
url = movie_url + params[:id].to_s + ".json?apikey=#{api_key}"
movie_response = HTTParty.get(url)
@movie = JSON.parse(movie_response.body, symbolize_names: true)
@rating ||= (
Rating.find_by(rottentomatoes_id: params[:id], rater: current_user) ||
Rating.new(rottentomatoes_id: params[:id], rater: current_user)
)
end
def search
search_url = 'http://api.rottentomatoes.com/api/public/v1.0/movies.json'
api_key = ''
if request.GET[:title]
title = URI.encode(request.GET[:title])
url = search_url + "?apikey=#{api_key}&q=#{title}&page_limit=10&page=1"
rottentomatoes_response = HTTParty.get(url)
response_dict = JSON.parse(rottentomatoes_response.body, symbolize_names: true)
@movies = response_dict[:movies]
else
@movies = []
end
end
end
MovieController
&#39; show
形式:
<%= simple_form_for @rating do |f| %>
<% if @rating.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@rating.errors.count, "error") %> prohibited this rating from being saved:</h2>
<ul>
<% @rating.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :rottentomatoes_id, as: :hidden %>
<%= f.input :rater_id, as: :hidden %>
<%= f.collection_radio_buttons :rating,
[
[0, '0'],
[1, '1'],
[2, '2'],
[3, '3'],
[4, '4'],
[5, '5'],
[6, '6'],
[7, '7'],
[8, '8'],
[9, '9'],
[10, '10']
],
:first,
:last
%>
<%= f.button :submit %>
<% end %>
答案 0 :(得分:0)
将错误传递回MovieController:
format.html { redirect_to movie_path(id: @rating.rottentomatoes_id, errors: @rating.errors) }
然后在MovieController show
方法中:
if(params[:errors])
@errors = params[:errors]
end
然后在“电影放映”视图中将@rating.errors
替换为@errors
。