RoR - undefined `movie 'method for # <Movie: 0x007fa43a0629e0>

Using postman for POST, the JSON bit that will be created in the database is a piece of code for testing. Receiving an error indicating that the movie method is undefined, but this method is never called.

{"movie": { "title": "Its a Mad Mad Word", "year": "1967", "summary": "So many big stars" } } 

The code is below, and the error is as follows:

undefined method 'movie' for #<Movie:0x007fcfbd99acc0>

Application controller

 class ApplicationController < ActionController::Base protect_from_forgery with: :null_session end 

Controller

 module API class MoviesController < ApplicationController ... def create movie = Movie.new({ title: movie_params[:title].to_s, year: movie_params[:year].to_i, summary: movie_params[:summary].to_s }) if movie.save render json: mov, status: 201 else render json: mov.errors, status: 422 end end private def movie_params params.require(:movie).permit(:title, :year, :summary) end end end 

Model

 class Movie < ActiveRecord::Base validates :movie, presence: true end 

Migration

 class CreateMovies < ActiveRecord::Migration def change create_table :movies do |t| t.string :title t.integer :year t.text :summary t.timestamps null: false end end end 

Routes

 Rails.application.routes.draw do namespace :api do resources :movies, only: [:index, :show, :create] end end 
+5
source share
1 answer

Validations are used to ensure that only valid data is stored in your database, so you should check the movie fields (name, year, summary)

  validates :movie, presence: true 

change it to:

 validates :title, presence: true validates :year, presence: true validates :summary, presence: true 

You can get more information from here.

/ change huanson you can also summarize:

 validates :title, :year, :summary, presence: true 
+4
source

Source: https://habr.com/ru/post/1231982/


All Articles