ruby rails 新方法失败未显示
ruby on rails new method failed not show
下面是我的代码,但我的问题出在我的 _form.html.erb 中,当我使用 form_for 方法进行我的方法更新之一时,它有效,但是当我想创建一个新的数据,失败了。
家庭控制器
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 %>
最后是错误内容的屏幕截图:
点击'Edit'没有问题,点击'Update'
删除后的新更新 url{action: 'update'}
这个问题出现
如果您对 new
和 edit
使用相同的 _form.html.erb
,您应该在 _form.html.erb
:
中执行此操作
<%= form_for @input do |person| %>
不是:
<%= form_for @input, url: {:action => "update"} do |person| %>
您不需要添加 url: {:action => "update"}
,因为您在 routes.rb
.
中使用了 RESTful 或 resources
在你的第二张截图中,你似乎没有resources :people
。
基于你的 post routes.rb
:
resources:home
root 'home#index'
您没有:resources :people
。在您的路线上添加以下内容:
resources :people
在您的第三张屏幕截图中,请注意您没有 resources :home
的 space
在您的 routes.rb
上执行此操作:
resources :home
resources :people
root 'home#index'
要添加到 OJ1987
的答案...
您的问题是 form_for
自动从传递的对象中提取 url。在您的情况下,它会期望 people_path
:
<%= form_for @person do |f| %>
... 等同于 ...
<%= form_for @person, as: :person, url: people_path(@person), method: :patch, html: { class: "edit_person", id: "edit_person_45" } do |f| %>
因此,当您使用 resources :home
指令时,您最终会遇到 configuration 个问题。
--
解决方案是将您的 @person
功能放在 PeopleController
:
#config/routes.rb
resources :people #-> url.com/people/new
#app/views/people/_form.html.erb
<%= form_for @person do |f| %>
你应该read about resourceful
routing.
为了让这个与一些 "static" 页面一起工作(你似乎正在尝试使用你的 Home
控制器):
#config/routes.rb
resources :people
root "application#index"
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def home
#app/views/application/home.html.erb
end
end
下面是我的代码,但我的问题出在我的 _form.html.erb 中,当我使用 form_for 方法进行我的方法更新之一时,它有效,但是当我想创建一个新的数据,失败了。
家庭控制器
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 %>
最后是错误内容的屏幕截图:
点击'Edit'没有问题,点击'Update'
删除后的新更新 url{action: 'update'}
这个问题出现
如果您对 new
和 edit
使用相同的 _form.html.erb
,您应该在 _form.html.erb
:
<%= form_for @input do |person| %>
不是:
<%= form_for @input, url: {:action => "update"} do |person| %>
您不需要添加 url: {:action => "update"}
,因为您在 routes.rb
.
resources
在你的第二张截图中,你似乎没有resources :people
。
基于你的 post routes.rb
:
resources:home
root 'home#index'
您没有:resources :people
。在您的路线上添加以下内容:
resources :people
在您的第三张屏幕截图中,请注意您没有 resources :home
在您的 routes.rb
上执行此操作:
resources :home
resources :people
root 'home#index'
要添加到 OJ1987
的答案...
您的问题是 form_for
自动从传递的对象中提取 url。在您的情况下,它会期望 people_path
:
<%= form_for @person do |f| %>
... 等同于 ...
<%= form_for @person, as: :person, url: people_path(@person), method: :patch, html: { class: "edit_person", id: "edit_person_45" } do |f| %>
因此,当您使用 resources :home
指令时,您最终会遇到 configuration 个问题。
--
解决方案是将您的 @person
功能放在 PeopleController
:
#config/routes.rb
resources :people #-> url.com/people/new
#app/views/people/_form.html.erb
<%= form_for @person do |f| %>
你应该read about resourceful
routing.
为了让这个与一些 "static" 页面一起工作(你似乎正在尝试使用你的 Home
控制器):
#config/routes.rb
resources :people
root "application#index"
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def home
#app/views/application/home.html.erb
end
end