2017-09-15 38 views
0

如何設置路徑?我的應用程序中出現與路徑有關的錯誤

我收到以下錯誤,當我運行我的Rails應用程序:

No route matches {:action=>"show", :controller=>"practice"}, missing required keys: [:id] 

這是我index.erb.html文件

<h3>Please fill the following details</h3> 
<hr> 
<%= form_with scope: :welcome , local: true do |f| -%> 
<b><i>Name</b></i>:&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp&nbsp<%= f.text_field :name %> </br></br> 
<b><i>Address</b></i>:&nbsp &nbsp &nbsp &nbsp&nbsp <%= f.text_area :address %> </br></br> 
<b><i>City</b></i>:&nbsp &nbsp <%= f.text_field :city %> </br></br> 
<%= f.submit "Submit", class: "btn-submit" %> 

<% end %> 

這是我的routes.rb文件。

Rails.application.routes.draw do 
    resources :practice 

    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
end 

這是我的控制文件:

class PracticeController < ApplicationController 
    def new 
     @welcome= Welcome.new; 
    end 

    def index 
     @welcome= Welcome.new; 
    end 
    def show 
     @welcome = Welcome.find(params[:id]) 
    end 
    def create 
     @welcome=Welcome.new(params.require(:welcome).permit(:name,:address,:city)) ; 
      if @welcome.save 
      flash[:notice]="Successfully Registered" 
      redirect_to practice_path 
      else 
      flash[:notice]="Error while registering" 
      render:new 
     end 
    end 
end 

這是我show.html.erb文件:

<p> 
 
    <strong>Name:</strong> 
 
    <%= @welcome.name %> 
 
</p> 
 
    
 
<p> 
 
    <strong>Address:</strong> 
 
    <%= @Welcome.address %> 
 
</p> 
 

 
<p> 
 
\t <strong>City:</strong> 
 
\t <%= @welcome.city%> 
 
</p>

我沒有爲show.html.erb文件設置任何link_to助手。

+0

練習#show route正在等待參數中的id,您如何通過表單重定向到此路由?任何link_to助手? –

+0

'.../practice/123'。 123是你的練習編號。 –

+0

你如何從瀏覽器訪問索引路由? – Mohanraj

回答

0

在聲明路由使用複數形式:

# bad 
resources :practice 

# good 
resources :practices 

要重定向到您需要重定向到practices_path索引路徑:

def create 
    @welcome = Welcome.new(practice_params) 
    if @welcome.save 
    redirect_to practices_path, notice: "Successfully Registered" 
    else 
    render :new, notice: "Error while registering" 
    end 
end 

private 

def practice_params 
    params.require(:welcome).permit(:name,:address,:city) 
end 

如果你不是想重定向到新創建的記錄只是通過它到redirect_to

redirect_to @welcome, notice: "Successfully Registered" 

你可以s ee與rails routes的路線的完整列表(在Rails 4中運行bundle exec rake routes代替)

+0

此外,Ruby不像C,Java或PHP。分號(';')僅在需要將多條語句放在同一行時才需要。 – max

+0

它仍然顯示相同的錯誤 – Shabbir

+0

你重定向到什麼路徑? '/ practices'?你修復了'routes.rb'文件嗎? – max

相關問題