Form_helper and routing

46 views Asked by At

I am new to rails. Currently trying to pull from an api and store the value of each api request into a form for entry into my DB.

My page has a search bar at the top which pulls the form the api and returns the value of each result to the screen with a button prompting to add to the DB.

<ul>
<% @movies.each do |movie| %>
<li><%= movie['Title'] %> | <%= movie['Year'] %> |
<%= form_tag(movies_path, :method => "post") do %> <%=hidden_field :movie, :title, :value =>     movie["Title"] %> <%= hidden_field :movie, :year, :value => movie["Year"] %> <%= hidden_field :movie, :imdb_id, :value => movie['imdbID'] %> <%= submit_tag "Add Movie", :name => nil %> <% end %> </li><br>
<% end %>
</ul>

My controller for both my create a @movie for the index/ search results page is this

class MoviesController < ApplicationController

def index
    if params[:search].present?
        @movies = Omdbapi.search(params[:search])
    else
        render :'/watchlists'
    end
end

def show
    @movie = Omdbapi.more_info(params[:imdb_id])
end

def create
        @movie = Movie.new(movie_params)
    if @movie.save
        redirect_to movies_path(@movie)
    end
end

When I try to hit submit rails throw this error

Missing template movies/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.

Any help would be awesome.

1

There are 1 answers

1
blelump On BEST ANSWER

@movie did not persisted for some reason so that redirect_to hasn't been performed. Add else statement to condition:

   if @movie.save
     redirect_to movies_path(@movie)
   else 
     render action: 'new'
   end