1

I am continuing my project on the tmdb api and in the process of saving the result of my query. I created the method add_tmdb to handle this. As for my view, this is what I have:

= form_tag :action => 'add_tmdb' do
  ... stuff here
   %td= check_box_tag "movie_id[]", movie.id
 = submit_tag 'Add selected movie'

My controller is below:

def add_tmdb
    movie = {params[:movie_id] => :id}
    Movie.create!(movie)
    flash[:notice] = "#{params[:movie_id]} was successfully created."
    redirect_to movies_path
end

When I submit the record, I get an active record error. This is what I receive:

unknown attribute '["7555"]' for Movie.

Where do I need to make the change?

Thanks!

2 Answers2

2

The value of params[:movie_id] is a single element array. You're using that value as the key of your hash:

{params[:movie_id] => :id}

If you want to specify a key of :id with a value of params[:movei_id], then your hash is backwards. You need

{ id: params[:movie_id] }
user229044
  • 232,980
  • 40
  • 330
  • 338
  • Ah thank you @meagar. How do I proceed in adding the title, rating and release date of the movie? I have a method that permits the following: def tmdb_params params.require(:search_terms).permit(:id, :title, :release_date, :rating) end – user5415750 Oct 25 '15 at 01:50
0

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
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Thank you @Rich. I needed that nudge. With a little bit of research and further reads, I will be able to fully grasp it. My controller has the new and create methods. What I am struggling with is after the fact that I pass the tmdb result to a view, I need to grab them and save the attributes. I think your API callback you mentioned below is something I need to work on. – user5415750 Oct 25 '15 at 15:08
  • Good to hear you got through it in your head; did you work out the API stuff or not? – Richard Peck Oct 25 '15 at 16:50
  • My thick head you mean? No not yet. I am still figuring out the appropriate calls. – user5415750 Oct 25 '15 at 16:54
  • Tmdb gem. I'll share my hold up in a bit. – user5415750 Oct 25 '15 at 17:31
  • So I continued with using the `add_tmdb` method. I will have to revisit the `new` and `create` method. @Rich, I actually posted a new question. Here is the [link] http://stackoverflow.com/questions/33331649/rails-continuing-to-work-on-check-box-tag?noredirect=1#comment54460252_33331649 – user5415750 Oct 25 '15 at 17:52