【问题标题】:Ruby On Rails Basic Show method failed stillRuby On Rails Basic Show 方法仍然失败
【发布时间】:2016-01-26 08:06:02
【问题描述】:

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

我的 home_controller.rb 类 HomeController

  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%>

我的 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 %>

我的edit.html.erb

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

我得到的错误是:

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

【问题讨论】:

  • 您的 link_to 似乎是错误的。你可以这样做: inside index.html.erb
  • 嗨,Sachin,感谢您的帮助,我解决了那些路线问题,但是,我遇到了 show.html.erb 本身的另一个问题。我会在最后发布图片。据我了解,显示后是调用里面的索引动作。该人将进入显示操作,并且对于每个输入,我都会读取属性,但是,事实并非如此,调用 inputs.name 或 person.name 都是错误的
  • 错误提示 Home 中的 NoMethodError#show undefined method 'name' for nil:Nilclass[]

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4


【解决方案1】:

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

【讨论】:

  • 大家好,我是 ror 的新手,只是在使用 link_to 方法例如我有一个控制器主页和操作 include:index,show 这是我使用链接到的方式: 但是,结果无效,在我的 routes.rb 中,我有资源:home root 'home#index' 我的问题是如何有效地使用 link_to 而不会出现这样的问题,应该怎么做我在乎的时候写这个??
  • 错误是 NameError in Home#show undefined local variable or method `index_home_path' for #:0x007f4916b10ba0>
  • 你需要给它&lt;%= link_to 'Back', home_path%&gt;
  • home_path 到底是什么意思?我认为 index_home_path 更有意义,它与 controller: 'home', action: 'index' 相同,它将按钮指向索引页面。我只是困惑
  • 实际上我尝试了你的代码,它有点工作,但它并没有让我回到索引页面而是仍然留在显示页面上
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 2014-06-03
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多