2016-01-27 84 views
1

下面是我的代碼,但我的問題是在我的_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 %> 

,最後的錯誤是什麼截圖:

this is happening when i click on 'New Test'

它當我點擊「編輯」和「更新」

新的更新,刪除URL {動作後沒有問題: '更新'} 這個問題上來 enter image description here

having resources: home and resources:people altogether

+0

在您的屏幕截圖,您呼叫'不update'行動'show'。你也可以顯示你的'show.html.erb'嗎? –

+0

對不起,這是新&創建方法失敗未顯示 – ThugForever

+0

@ ThugForever嘗試刪除'url:{:action =>「update」}'Rails足夠聰明,知道它是否應該執行POST(創建)或PUT(更新)。 – miligraf

回答

0

爲了增加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


得到這個工作的一些「靜態」頁面(因爲它似乎你想與你的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 
1

如果使用相同_form.html.erbnewedit,你應該這樣做在你的_form.html.erb

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

不:

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

你不需要因爲你用的RESTful添加url: {:action => "update"}resources你的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' 
+0

我嘗試刪除網址,它已失敗 – ThugForever

+0

當我失敗時它說什麼? –

+0

查看更新,第二張圖片 – ThugForever