You may be accessing an API
, but I still think your code needs to be structured better:
#config/routes.rb
resources :movies
#app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def new
@movie = Movie.new
@movies = [[get data for Movies -- from API?]]
end
def create
@movie = Movie.new movie_params
@movie.save
end
private
def movie_params
params.require(:movie).permit(:id, :title, :rating, :release_date)
end
end
Then your form is as follows:
#app/views/movies/new.html.erb
<%= form_for @movie do |f| %>
<%= f.text_field :title %>
<%= f.text_field :rating %>
<%= f.collection_check_boxes :id, @movies, :id, :name %>
<%= f.submit %>
<% end %>
Currently, your flow is not conventional; it doesn't build and object properly and doesn't protect against mass assignment.
Everything in Rails/Ruby is object orientated, meaning that when you "create" a new record, you're building a new object. Might sound trivial, but it's the core of the entire Rails framework.
Your comment of "How do I proceed in adding the title, rating and release date of the movie" demonstrates the ramifications of not adhering to it.
In short, you need new
,create
actions, both referencing a new Movie
object, which you can then populate the with the params sent from your views.
API
The only exception you should make to the above would be if your flow is dependent on API
callbacks, like through a billing system or something.
If you want to retain your current flow (IE call add_tmdb
), you'll want to make sure you're populating the attributes of your Movie
object as follows:
#app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def add_tmdb
@movie = Movie.new
@movie.id = params[:movie_id]
@movie.save
#or....
@movie = Movie.new({id: params[:movie_id]})
@movie.save
end
end