我想運行更新動作,但我無法獲得ID。我以為這是個簡單的問題,但我找不到解決問題的方法。
我以為從edit發送的值會被發送到update action,update action會更新這個值,但我無法得到ID。我一直在看其他文章,看起來它的更新方式是一樣的。
代碼
edit
<! DOCTYPE html>
<html lang="en"/span>>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0" >
<title>Document</title>
</head>
<body>
<%= form_with model: @movie, url: '/admin/movies/: id', method: :put do |form| %>
<%= form.text_field :name %>
<p>年</p>
<%= form.text_field :year %>
<p>影像URL</p>
<%= form.text_field :image_url %>
<p>描述</p>
<%= form.text_area :description %>
<p>顯示</p>
<%= form.text_field :is_showing %>
<%= form.submit"Edit Save"/span>%>
<% end %>
</body>
</html>
controller
class MoviesController < ApplicationController >。
def index
@movie = Movie.all
end @movie = Movie.all
def new
@movie = Movie.new
end @movie = Movie.new
def create
@movie = Movie.new(post_params)
@movie.save!
end。
def edit
@movie = Movie.find(params[:id] )
@movie.save
結束。
def update
@movie = Movie.find(params[:id] )
if @movie.update(post_params)
redirect_to:idnex
else[/span
呈現 :edit
end
end
私有
def post_params
params.require(:movie)。 permit(:id,:name, : year, :image_url, :description, :is_showing )
end end
end end
模型
FactoryBot.define do
工廠 : movie do
sequence(:name) { |n| "TEST_MOVIE#{n}"}。
sequence(:year) { 2021 }
sequence(:description) { "test" }
sequence(:image_url) {"" }
sequence(:is_showing) { 1 }
end end
結束。
路線
Rails.application.routes.draw do
# get 'movies', to: 'movies#index'
獲取'/admin/movies', to: 'movies#index'
獲取 '/admin/movies/new', to: 'movies#new'。
post '/admin/movies/new', to: 'movies#create'。
獲取 '/admin/movies/:id/edit', to: 'movies#edit'。
把'/admin/movies/:id', to: 'movies#update'。
獲取'/admin/movies/:id', to: 'movies#edit'
end
改變form_with方法后出現的錯誤
uj5u.com熱心網友回復:
你真的只需要在這里利用rails的慣例,而不是手動做所有事情。
Rails.application.routes.draw do
范圍 :admin do
資源 :movies do
end end
end end
不要手動輸入每個路由,而是使用的resources宏。此外,Rails使用PATCH HTTP方法而不是PUT進行更新已經有相當長的一段時間了。出于傳統的原因,Rails 也會輸出一個 PUT 路徑,但這在未來可能會發生變化。
現在的形式:
<%= form_with model:/span> [:admin, @movie] do |form| %>
<div class="field" >
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :year %>
<%= form.text_field :year %>
</div>
<div class="field" >
<%= form.label :image_url, "Image URL" %>
<%= form.text_field :image_url %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description %>
</div>
<div class="field" >
<%= form.label :is_showing, "Show" %>
<%= form.text_field :is_showing %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
你不需要指定URL或方法。Rails可以僅僅根據模型來計算出所有這些。使用[:admin, @movie]告訴Rails尋找admin_movie_path。
使用標簽而不是<p>標簽來標記你的輸入,這樣它就可以被螢屏閱讀器等輔助技術訪問。將輸入和標簽組合在一個元素中,可以讓你用CSS來設計它的樣式。
你的控制器中有許多錯字和錯誤:
你的控制器中有許多錯字和錯誤。
class MoviesController < ApplicationController
# GET /admin/movies
def index
# 使用實體變數的復數名稱,因為它有很多電影。
@movies = Movie.all
end = Movie.all
# GET /admin/movies/new
def new
@movie = Movie.new
end @movie = Movie.new
# POST /admin/movies/
def create
@movie = Movie.new(post_params)
# 檢查電影是否被保存并作出相應的反應。
# save!一般不應該在控制器中使用。
if @movie.save
redirect_to :index, notice:'Movie created'.
else
渲染:new
end
end
# GET /admin/movies/1/edit
def edit
@movie = Movie.find(params[:id] )
end end
# PATCH|PUT /admin/movies/1
def update
@movie = Movie.find(params[:id] )
if @movie.update(post_params)
# 這是一個打字錯誤。
redirect_to :index, notice: 'Movie updated'。
else
呈現:edit
end
end
私有
def post_params
params.require(:movie)
.允許(
:id, :name, :year,
:image_url, :description, :is_showing)
)
end end
end end
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/322768.html
標籤:
上一篇:如何解決Ruby的錯誤:`require':無法加載這樣的檔案--《Ruby1.9&2.0編程》第四版
下一篇:Trix富文本編輯器在new.html.erb中作業,但在show.html.erb視圖中顯示代碼,而不是風格化的文本(在瀏覽器中顯示)。
