如何設置路徑?我的應用程序中出現與路徑有關的錯誤
我收到以下錯誤,當我運行我的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>:            <%= f.text_field :name %> </br></br>
<b><i>Address</b></i>:         <%= f.text_area :address %> </br></br>
<b><i>City</b></i>:    <%= 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助手。
練習#show route正在等待參數中的id,您如何通過表單重定向到此路由?任何link_to助手? –
'.../practice/123'。 123是你的練習編號。 –
你如何從瀏覽器訪問索引路由? – Mohanraj