I don't know which code is bad and I can't pick up the id.
Please teach me.
controller people_controller.rb
class PeopleController <ApplicationController
layout 'people'
def index
@msg = 'Person data.'
@data=Person.all
end
def show
@msg = "Indexed data."
@data=Person.find (params[:id])
end
def find
@msg='please type search word...'
@people=Array.new
if request.post?then
obj=Person.find params ['find']
@people.pushobj
end
end
View show.html.erb
<h1>People #index</h1>
<p>
<%=@msg%>
</p>
<pre>
<table>
<tr><th>Id</th>
<td><%=@d ata.id%></td>/tr>
<tr><th>Name</th>
<td><%=@d ata.name%></td>/tr>
<tr><th>Age</th>
<td><%[email protected]%></td></tr>
<tr><th>Mail>/th>
<td><%[email protected]%></td>/tr>
</table>
</pre>
View find.html.erb
<h1>People #Find</h1>
<p>
<%=@msg%>
</p>
<table>
<%=form_tag(controller: "people", action: "find") do%>
<tr>th>FIND</th>
<td><%=text_field_tag("find")%>/td>
<tr>
<td><%=submit_tag("Click")%>/td>
</tr>
<%end%>
</table>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Mail>/th>
</tr>
<%@people.eachdo|obj|%>
<tr>
<td><%=obj.id%>/td>
<td><%=obj.name%>/td>
<td><%=obj.age%>/td>
<td><%=obj.mail%>/td>
</tr>
<%end%>
</table>
routes.rb
get'people/:id', to:'people#show'
get 'people/find', to: 'people#find'
post 'people/find', to: 'people#find'
Database db<migrate<hoge_create_people.rb
class CreatePeople<ActiveRecord::Migration [5.0]
def change
create_table:people do|t|
t.text —name
t.integer:age
t.text:mail
t.timestamps
end
end
end
Capturing Views
ruby-on-rails ruby
Error information
app/controllers/people_controller.rb:11:in `show'
and
Parameters
{"id"=>"find"}
It says
Routing rules are first matched to requests when viewed from above, so
get'people/:id', to:'people#show'
get 'people/find', to: 'people#find'
In the order shown in , /people/find
is interpreted as calling people/:id
with the parameter :id
=>"find"
and is routed to the show
action.
© 2024 OneMinuteCode. All rights reserved.