0

I have a Movie Model and an Actor Model:

class Movie < ActiveRecord::Base

  mount_uploader :poster, PosterUploader

  has_many :actors

  accepts_nested_attributes_for :actors

end


class Actor < ActiveRecord::Base

  belongs_to :movie

end

The Movie controller:

class MoviesController < ApplicationController

  before_filter :set_movie, only: [:show, :edit, :update, :destroy]

  def index
    @movies = Movie.all
  end

  def show
    @movie.actors.build
  end

  def new
    @movie = Movie.new
    @movie.actors.build
  end

  def create
    @movie = Movie.new(movie_params)
    if @movie.save
      flash[:notice] = 'Movie Successfully Created'
      redirect_to movie_path(@movie)
    else
      flash.now[:alert] = 'Invalid Movie Form'
      render 'new'
    end
  end
.
.
.
  private

  def set_movie
    @movie = Movie.find(params[:id])
  end

  def movie_params
    params.require(:movie).permit(:title, :genre, :description, :duration, :release, :poster, actors_attributes: [:name])
  end

end

and the show view:

<div id="show_movie_container">
  <p><%= image_tag(@movie.poster_url, size: "300x400") %></p>
  <p><%= @movie.title %> (<%= @movie.release %>)</p>
  <p><%= link_to 'Edit', edit_movie_path %> | <%= link_to 'Delete', movie_path, method: :delete, data: { confirm: 'Are You sure you want to delete this movie?' } %></p>
  <p>Duration: <%= @movie.duration %></p>
  <p>Genre: <%= @movie.genre %></p>
  <p>Actors: <%= @movie.actors.name %></p>
  <p>Description: <%= @movie.description %></p>
</div>

The problem is every time I try to display the Actors on the view, I keep getting 'Actors: Actor' instead of the name I typed in when I submitted the form. When I look at my server logs I don't understand where the "0" is coming from in the actors_attributes hash:

"actors_attributes"=>{"0"=>{"name"=>"Christian Bale"}}

Not sure if that has anything to do with the problem.

When I go into the console and type 'm = Movie.last', and then type 'm.actors' I can successfully get back:

#<Actor:0x007fcd2890ab58> {
                 :id => 9,
               :name => "Christian Bale",
                :bio => nil,
        :filmography => nil,
         :created_at => Wed, 09 Sep 2015 16:02:55 UTC +00:00,
         :updated_at => Wed, 09 Sep 2015 16:02:55 UTC +00:00,
           :movie_id => 9
    }

Just can't get the same back in the view.

I've been stuck on this for a couple hours and would greatly appreciate any help provided, thanks!

0

1 Answer 1

2

If I'm not wrong, you need to iterate through @movie.actors like below and display the actors name

<% @movie.actors.each do |actor| %>
  <p>Actors: <%= actor.name %></p>
<% end %>
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it worked! Thanks Pavan. Do you have any idea why i'm getting two <p>Actors</p> tags now in my view? Very strange.
@Banner Its not strange. It is because I kept it inside the do loop. If you want a single Actors, place it above the do loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.