Rails: How can I access a model's "errors" from a separate controller? -
i have resource called "rating" (which has standard actions: create
, update
, destroy
). have form resource rendered in controller (called moviescontroller
).
how can access ratingscontroller
's @rating
within moviescontroller
?
the 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 |format| if @rating.save format.html redirect_to movie_path(id: @rating.rottentomatoes_id), notice: 'rating created.' end else format.html { redirect_to movie_path(id: @rating.rottentomatoes_id) } end end end def update respond_to |format| if @rating.update(rating_params) format.html redirect_to movie_path(id: @rating.rottentomatoes_id), notice: 'rating updated.' end else format.html { redirect_to movie_path(id: @rating.rottentomatoes_id) } end end end def destroy @rating.destroy respond_to |format| format.html redirect_to movie_path, notice: 'rating 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
and 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
and moviecontroller
's show
form:
<%= simple_form_for @rating |f| %> <% if @rating.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@rating.errors.count, "error") %> prohibited rating beingness saved:</h2> <ul> <% @rating.errors.full_messages.each |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 %>
pass errors moviecontroller:
format.html { redirect_to movie_path(id: @rating.rottentomatoes_id, errors: @rating.errors) }
and in moviecontroller show
method:
if(params[:errors]) @errors = params[:errors] end
then replace @rating.errors
@errors
in film show view.
ruby-on-rails ruby-on-rails-4
No comments:
Post a Comment