简体   繁体   English

从<a/>谁存储在可变ERB中</a>获取href值

[英]Get href value from <a/> who is store in a variable ERB

My entire code : 我的整个代码:

<% query.inline_columns.each do|column| %>
    <% if column.css_classes.to_s != "assigned_to" %>
         <td class="<%= column.css_classes %>"> <%= column_content(column, issue) %> </td>
    <% else %>
         <td class="<%= column.css_classes %>"> <%= test = column_content(column, issue) %> </td>
    <% end %>
<% end %>

I have something like that: 我有这样的事情:

test = column_content(column, issue)

and when i go on the website it give me something like that: 当我进入网站时,它会给我类似的信息:

<a class="user active" href="/users/5">Test</a>

My question is how can I get the value of href ? 我的问题是如何获得href的价值? I tried test.href, test.link_to and test.link but don't work. 我尝试了test.href,test.link_to和test.link,但是不起作用。

And after when we have the href it's possible to only have the id in href ? 之后,当我们拥有href时,就可以仅在href中包含id了? (In the href above it's 5) BUT I don't want to go on page and get the id, I just want to have it so I can load the picture of user in the table. (在上面的href中是5) 但我不想进入页面并获得ID,我只想拥有它,以便可以将用户图片加载到表中。

I'm beginner in ERB language, thank's for help ! 我是ERB语言的初学者,谢谢您的帮助!

EDIT : My solution to get the id : 编辑 :我的解决方案,以获取ID:

test.to_s.split("href=\"").last.to_s.split("\"").first.to_s.split("/").last.to_s

Thank's guy to try to help me ! 谢谢你的家伙试图帮助我!

The Request cycle: 请求周期:

In Rails and in MVC every request follows a cycle: 在Rails和MVC中,每个请求都遵循一个周期:

Let's say you type in http://localhost:3000/users . 假设您输入http://localhost:3000/users

  1. The request hits your Rails server and goes through Rack to your routes. 该请求到达您的Rails服务器,并通过机架到达您的路线。
  2. Rails picks a controller and action which matches the request. Rails选择一个与请求匹配的控制器和动作。
  3. Rails runs your UsersController#index and renders the view. Rails运行您的UsersController#index并呈现视图。
  4. The rendered html is sent back to the client. 呈现的html发送回客户端。

So why am I telling you this? 那我为什么要告诉你呢?

It has nothing to do with ERB . 它与ERB无关。

You simply render the view and send it to the client - all done. 您只需渲染视图并将其发送给客户端即可。 The client can interact by clicking links or submitting forms. 客户可以通过单击链接或提交表单进行交互。 But it's not your actual view communicating back. 但这不是您实际的观点进行交流。

When the user clicks the link it starts a whole new request - it's a whole new process*. 当用户单击链接时,它会启动一个全新的请求-这是一个全新的过程*。

Routes & Params 路线和参数

Routes are kind of like regular expressions - with superpowers. 路线有点像正则表达式-具有超能力。

If you want your rails app to handle the request to /users/1 you need to create a route: 如果您想让Rails应用处理对/users/1的请求,则需要创建一条路由:

 # config/routes.rb
 get `/users/:id`, to: 'users#show'

See the :id part? 看到:id部分? That means we can get the ID passed in the url from params[:id] . 这意味着我们可以从params[:id]获得在URL中传递的ID。

class UsersController
  def show
    @user = User.find(params[:id])
  end
end

Of course writing your routes like that is pretty tedious. 当然,编写这样的路线非常繁琐。 So instead we write: 所以我们写:

resources :users

Try running rake routes from the console to see what it does. 尝试从控制台运行rake routes以查看其作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM