Usando il postino per POST un po' di JSON da creare in un DB, c'è uno snippet di codice con cui testare. Ottenere un errore che dice che il metodo 'filmato' non è definito, ma quel metodo non viene mai chiamato.RoR - metodo non definito `movie 'per # <Film: 0x007fa43a0629e0>
{"movie": {
"title": "Its a Mad Mad Word",
"year": "1967",
"summary": "So many big stars"
}
}
Di seguito si riporta il codice, e l'errore è la seguente:
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
Girl
class Movie < ActiveRecord::Base
validates :movie, presence: true
end
migrazione
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
Itinerari
Rails.application.routes.draw do
namespace :api do
resources :movies, only: [:index, :show, :create]
end
end
non è stato utilizzato come un API, ma quella convalida nel tuo modello sembra pericolosa. –
dal momento che sei un principiante ho davvero bisogno di dire, mi piace il modo in cui la tua domanda sembra! –