Ruby on rails - display error message for an associated object

时间:2016-04-04 18:13:12

标签: ruby-on-rails associations

I have the below form written for a movie review. Review is associated with movies. I am not able to display error message for this form. I have written similar code to display error message for validation on movies and that works well.

Code for the 'add new review' form -

<%= form_for([@movie, @movie.reviews.build]) do |f| %>  

    <% if @reviews.errors.any? %>
    <div id="error_explanation">
      <h3>
        <%= pluralize(@reviews.errors.count, "error") %> prohibited
        this review from being saved:
      </h3>
      <ul>
        <% @reviews.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
    <% end %>

  <p>
    <%= f.label :reviewer %><br>
    <%= f.text_field :reviewer %>
  </p>
  <p>
    <%= f.label :comment %><br>
    <%= f.text_area :comment %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

Models -

    MOVIE -

    class Movies < ActiveRecord::Base
    has_many :reviews, dependent: :destroy
    validates_associated :reviews
    validates :title, presence: true
    validates :rating, presence: true, numericality: { greater_than: 0, less_than_or_equal_to: 10 }
end

      REVIEW -

      class Review < ActiveRecord::Base
      belongs_to :movies
      validates :comment, presence: true
      end   

Adding code for reviews_controller.erb

class ReviewsController < ApplicationController
    def create
        @movie = Movies.find(params[:movie_id])
        @review = @movie.reviews.create(review_params)
        redirect_to movies_path(@movie)
    end

    def destroy
        @movie = Movies.find(params[:movie_id])
        @review = @movie.reviews.find(params[:id])
        @review.destroy
        redirect_to movies_path(@movie)
    end

    private
    def review_params
        params.require(:review).permit(:reviewer, :comment)
    end

end

Here is my code for movies_controller.erb

class MoviesController < ApplicationController
    def new
        @movie = Movies.new
    end

    def index
        @movies = Movies.all
    end

    def show
        @movie = Movies.find(params[:id])
    end

    def edit
        @movie = Movies.find(params[:id])
    end

    def create
        @movie = Movies.new(movie_params)
        if @movie.save
            redirect_to @movie
        else
            render 'new'
        end 
    end

    def update
        @movie = Movies.find(params[:id])
        if @movie.update(movie_params)
            redirect_to @movie
        else
            render 'edit'
        end 
    end

    def destroy
        @movie = Movies.find(params[:id])
        @movie.destroy

        redirect_to movies_path
    end

    private 
    def movie_params
        params.require(:movie).permit(:title,:year,:rating,:description)
    end 

end

Please help me identify what is going wrong and how to be able to get the validation message working.

Thanks in advance.

1 个答案:

答案 0 :(得分:1)

You are trying to display error of @review with <% if @reviews.errors.any? %>

But You do not have @reviews in <%= form_for([@movie, @movie.reviews.build) do |f| %>

Better solution, through controller assigning @review = Review.new. In this case it should show you errors, if comment field is empty.

def new
   @review = Review.new
end

def create
    @movie = Movie.find(params[:movie_id])
    @review = @movie.reviews.build(review_params)

    if @review.save
    else
      render 'new'
    end
end

In Form

<%= form_for ([@movie, @review) do |f| %>