gpt4 book ai didi

ruby-on-rails - Ruby On Rails Basic Show 方法仍然失败

转载 作者:太空宇宙 更新时间:2023-11-03 18:10:29 25 4
gpt4 key购买 nike

在这段代码中,我想在“显示” View 中放置一个“编辑”按钮。但是由于某些原因,并没有成功

我的home_controller.rb类 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

private

def input_params
params.require(:inputs).permit(:name, :weight, :height, :color, :age)
end
end

我的路由文件只有两行:

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="3"></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>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Test', new_home_path %>

我的 show.html.erb:

<p>
<strong>Name:</strong>
<%= @input.name %>
</p>

<p>
<strong>Weight:</strong>
<%= @input.weight %>
</p>

<p>
<strong>Height:</strong>
<%= @input.height %>
</p>

<p>
<strong>Color:</strong>
<%= @input.color %>
</p>

<p>
<strong>Age:</strong>
<%= @input.age %>
</p>

<% link_to 'Edit', edit_home_path(@input) %>
<%= link_to 'Back', home_index_path%>

我的表单.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 %>

我的 edit.html.erb

<h1>Editing Data</h1>
<%= render 'form' %>
<%= link_to 'Show', home_path %> |
<%= link_to 'Back', home_index_path %>

我得到的错误是: It suppose to give me a Edit button, but actually it did not show up, confused

我的代码 <% link_to 'Edit', edit_home_path(@input) %>将“编辑”按钮指向带有对象@input 的主页/编辑路线,这就是我的理解方式,但它仍然无法正常工作。知道我该如何解决这个问题吗?非常感谢

最佳答案

问题出在您的 show.html.erb 中。您需要将 @person 更改为 @input,因为您在 show 方法中定义了 @input

关于ruby-on-rails - Ruby On Rails Basic Show 方法仍然失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35009257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com