下面是我的代碼,但我的問題是在我的_form.html.erb,當我使用form_for方法爲我的方法更新之一,它的工作原理,但是當我想創建一個新的數據,它失敗了。ruby on rails新方法失敗不顯示
家居控制器
class HomeController < ApplicationController
def index
@inputs = Person.all
end
def new
@input = Person.new
end
def create
@input = Person.new(input_params)
respond_to do |x|
if @input.save
x.html {redirect_to :action => 'index'}
else
x.html {render :action => 'new'}
end
end
end
def show
@input = Person.find(params[:id])
end
def edit
@input = Person.find(params[:id])
end
def update
@input = Person.find(params[:id])
respond_to do |x|
if @input.update(input_params)
x.html {redirect_to :action => 'index'}
else
x.html {render :edit}
end
end
end
def destroy
@input = Person.find(params[:id])
@input.destroy
respond_to do |x|
x.html {redirect_to :action => 'index', notice: 'data was delete successfully'}
end
end
private
def input_params
params.require(:person).permit(:name, :weight, :height, :color, :age)
end
end
_form.html.erb
<%= form_for @input, url: {:action => "update"} do |person| %>
<div class="field">
<%= person.label :name %><br>
<%= person.text_field :name %>
</div>
<div class="field">
<%= person.label :weight %><br>
<%= person.number_field :weight %>
</div>
<div class="field">
<%= person.label :height %><br>
<%= person.number_field :height %>
</div>
<div class="field">
<%= person.label :color %><br>
<%= person.text_field :color %>
</div>
<div class="field">
<%= person.label :age %><br>
<%= person.number_field :age %>
</div>
<div class="actions">
<%= person.submit %>
</div>
<% end %>
的routes.rb
resources:home
root 'home#index'
index.html.erb:
<p id="notice"><%= notice %></p>
<h1>Listing</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th> Weight</th>
<th> Height</th>
<th> Color</th>
<th> Age</th>
<th colspan="4"></th>
</tr>
</thead>
<tbody>
<% @inputs.each do |person| %>
<tr>
<td><%= person.name %></td>
<td><%= person.weight %></td>
<td><%= person.height %></td>
<td><%= person.color %></td>
<td><%= person.age %></td>
<td><%= link_to 'Show',home_path(person.id) %></td>
<td><%= link_to 'Edit', edit_home_path(person.id) %></td>
<td><%= link_to 'Destroy', home_path(person.id), method: :delete, data: {action: 'are you sure?'} %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Test', new_home_path %>
,最後的錯誤是什麼截圖:
它當我點擊「編輯」和「更新」
新的更新,刪除URL {動作後沒有問題: '更新'} 這個問題上來
在您的屏幕截圖,您呼叫'不update'行動'show'。你也可以顯示你的'show.html.erb'嗎? –
對不起,這是新&創建方法失敗未顯示 – ThugForever
@ ThugForever嘗試刪除'url:{:action =>「update」}'Rails足夠聰明,知道它是否應該執行POST(創建)或PUT(更新)。 – miligraf