如何在另一个视图中显示用户点击的值

发布于 2024-12-20 18:51:30 字数 774 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-12-27 18:51:30

更改

<%= link_to cd.name, :action => :lwstlevel %>

<%= link_to cd.name, :action => :lwstlevel, :id => cd.name %>

def lwstlevel               
  @x = Cd.find(params[:name])
end

def lwstlevel               
  @x = Cd.find(params[:id])
end

名称更改为 id 是 id 是成员操作的默认值,但是您可以将其保留为名称。

Change

<%= link_to cd.name, :action => :lwstlevel %>

to

<%= link_to cd.name, :action => :lwstlevel, :id => cd.name %>

and

def lwstlevel               
  @x = Cd.find(params[:name])
end

to

def lwstlevel               
  @x = Cd.find(params[:id])
end

Change name to id is id is the default for a member action, you could keep it as name however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文