ruby on rails新方法未能显示

时间:2016-01-27 05:12:45

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

下面是我的代码,但我的问题出在我的_form.html.erb中,当我使用form_for方法进行我的一个方法更新时,它可以工作,但是当我想创建一个新数据时,它失败了。 / p>

家庭控制器

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

最后是错误的截图:

this is happening when i click on 'New Test'

单击“编辑”和“更新”

时没有问题

删除网址{action:'update'}后的新更新 这个问题出现了 enter image description here

having resources: home and resources:people altogether

2 个答案:

答案 0 :(得分:1)

如果您对_form.html.erbnew使用相同的edit,则应在_form.html.erb中执行此操作:

<%= form_for @input do |person| %>

<%= form_for @input, url: {:action => "update"} do |person| %>

您无需添加url: {:action => "update"},因为您在resources中使用了RESTful或routes.rb

在您的第二个屏幕截图中,您似乎没有resources :people

根据您的帖子routes.rb

resources:home
root 'home#index'

你没有:resources :people。在您的路线上添加以下内容:

resources :people

在第三张屏幕截图中,请注意您没有resources :home

的空间

routes.rb

上执行此操作
resources :home
resources :people
root 'home#index'

答案 1 :(得分:0)

添加到OJ1987的答案......

您的问题是form_for会自动从传递的对象中提取网址。在您的情况下,它会期望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

要使用一些“静态”页面(因为您似乎正在尝试使用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