如何在另一个视图中显示用户点击的值
我是 Ruby on Rails 新手。我有一个“状态”表。我有一个视图(例如视图 1)列出一个国家/地区的所有州。当用户单击状态名称时,他/她将进入另一个视图(例如视图 2),其中包含所选状态的详细信息。
我正在尝试使用 find
方法。但只能将状态名称(在视图 1 中)链接到视图 2。 我是否需要将状态名称(从视图 1)传递给变量并在第二个视图(即视图 2)中使用该变量?
这是在索引视图中
<% @cds.each do |cd| %>
<h5><%= link_to cd.name :action=>:lwstlevel%></h5>
这是控制器。
def index
@cds = Cd.all
end
def lwstlevel
@x = Cd.find(params[:name])
end
这是 lwstlevel 视图中的
<h2>Welcome to <%= @x.name %> </h>
<p> <%= @x.dscr %> </p>
Cd 是我的“状态”表。在索引视图中,如果用户单击任何状态名称,他必须导航到 lwstlevel 视图,该视图应显示该状态的详细信息。 希望这就足够了。我尝试了几件事,但都没有给出预期的结果。
I am new to Ruby on Rails. I have a 'state' table. I have a view (say View 1) to list down all states in a country. When the user clicks on a state name he/she is taken to another view (say view 2) with the details of the selected state.
I am trying to use find
method. But could only link up the state name (in View 1) to View 2.
Do i need to pass the state name (from view 1) to a variable and use the variable in the second view i.e, view 2?
This is in Index view
<% @cds.each do |cd| %>
<h5><%= link_to cd.name :action=>:lwstlevel%></h5>
This is the controller .
def index
@cds = Cd.all
end
def lwstlevel
@x = Cd.find(params[:name])
end
This is the in lwstlevel view
<h2>Welcome to <%= @x.name %> </h>
<p> <%= @x.dscr %> </p>
Cd is my 'state' table. In index view if the user clicks on any state name he has to be navigated to lwstlevel view which should display details of that state.
hope this will suffice.I tried several things but none gave the desired results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改
为
和
将
名称更改为 id 是 id 是成员操作的默认值,但是您可以将其保留为名称。
Change
to
and
to
Change name to id is id is the default for a member action, you could keep it as name however.