我仍然无法更新。我得到“param缺失或价值为空:电影”
我忘了添加我的控制器!遗憾!
我正在尝试将我的课堂练习复制到另一个应用程序中,以便练习。
这个想法是能够将新电影添加到数据库中,并获得更新其信息的选项,并删除它们。我可以添加新内容,但我无法更新或删除它们。
感谢我能得到的任何输入。
的routes.rb
Rails.application.routes.draw do
root "movies#index"
get "/movies", to: "movies#index"
get "/movies/new", to: "movies#new", as: "new_movie"
get '/movies/:id', to: "movies#movie_page", as: "movie_page"
post "/movies", to: "movies#add"
get "/movies/:id/edit", to: "movies#edit", as: "movie_edit"
put "/movies/:id", to: "movies#update"
patch "/movies/:id", to: "movies#update", as: "update_movie"
delete "/movies/:id", to: "movies#delete", as: "delete_movie"
end
控制器
class MoviesController < ApplicationController
def index
@movies = Movie.all
end
def movie_page
@movies = Movie.find(params[:id])
end
def new
@movies = Movie.new
end
def add
@movies = Movie.create(movie_params)
redirect_to movies_path
end
def edit
@movies = Movie.find(params[:id])
end
def update
@movies.update(movie_params)
redirect_to @movies, notice: "Shirt was updated."
end
def delete
@movies = Movie.find(params[:id])
@movies.destroy
# flash[:notice] = "Shirt was deleted."
redirect_to root_path, notice: "Shirt was deleted."
end
def movie_params
params.require(:movie).permit(:title, :description, :year_released)
end
# def set_movie
# @movies = Movie.find(params[:id])
# end
end
表格部分
<%= form_for @movies do |m| %>
<p>
<%= m.label :title %><br>
<%= m.text_field :title %>
</p>
<p>
<%= m.label :description %><br>
<%= m.text_field :description %>
</p>
<p>
<%= m.label :year_released %><br>
<%= m.text_field :year_released %>
</p>
<p>
<%= m.submit %>
</p>
<% end %>
电影页面html(个别电影,标有ID)**我无法更新或删除,没有路线匹配删除。
当我按更新时 - 我得到param缺失或值为空:电影
<h1><%= @movies.title %></h1>
<h2>Released on : <%= @movies.year_released %> </h2>
<p> <%= @movies.description %> </p>
<%= link_to "Update", movie_edit_path(@movies) %>
<%= link_to "Delete", movies_path, method: :delete %
编辑页面*我无法访问此链接。形式是问题
<h1>Edit <%= @movies.title %> Info </h1>
<%= render "form" %>
<%= link_to "Cancel Edit", movie_edit_path(@movies) %>
非常感谢你们
答案 0 :(得分:0)
def update
@movie = Move.find(params[:id])
@movie.update(movie_params)
redirect_to movie_path(@movie)
end
在您的路线上。您只需要resources :movies
你得到的param是空的,因为你必须传入要更新的电影的id。
答案 1 :(得分:0)
主要问题是在使用之前不要从数据库加载变量@movies。
def update
@movies.update(movie_params)
redirect_to @movies, notice: "Shirt was updated."
end
def update
@movies.find(params[:id])
@movie.update(movie_params)
redirect_to @movies, notice: "Shirt was updated."
end
除此之外,你还有大量的重复和相当多的特质。
Rails将这些命名约定用于操作:
除非你有充分的理由不这样做,否则你应该遵循它们。
class MoviesController < ApplicationController
# cuts the duplication
before_filter :set_movie, except: [:new, :index]
def index
@movies = Movie.all
end
# GET /movies/:id
def show
end
# GET /movies/new
def new
@movie = Movie.new
end
# POST /movies
def create
@movie = Movie.create(movie_params)
redirect_to movies_path
end
# GET /movies/edit
def edit
end
# PUT|PATCH /movies/:id
def update
@movie.update(movie_params)
redirect_to @movie, notice: "Shirt was updated."
end
# DELETE /movies/:id
def destroy
@movie.destroy
redirect_to action: :index
end
private
def movie_params
params.require(:movie).permit(:title, :description, :year_released)
end
def set_movie
# Use the singular form when naming a variable with a single record
# failure to do so may result in tarring and feathering
@movie = Movie.find(params[:id])
end
end