在erb中添加div的innerHTML [英] Add the innerHTML of a div within erb

查看:58
本文介绍了在erb中添加div的innerHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用用户ID的链接.这保存在一个名为NameID的div中.控制器等都已正确设置,但我要做的就是能够传递NameID.innerHTML(这是一个数字)作为people.id表示的数字.我该怎么办?

I have a link that takes an id of a user. This is kept in a div called NameID. The controller etc is all set up correctly but all I need to do is to be able to pass the NameID.innerHTML (which is a number) as the number that people.id represents. How can I do this?

index.html.erb (图中未显示)

<div class="container-fluid">
    <div class="row">
          <div class="col-md-12">
          <div class="row">
                <div class="col-md-4">

                    <p><a class="btn btn-lg btn-primary" id="BtnMessageNode" href="/messages/new">Start conversation</a></p>

                <h2>NameID of Node</h2>
                <div id="NameID_Div"></div>

          </div>
    </div>
</div>
</div>
</div>

<% page_header "Users" %>


<ul>
  <% @peoples.each do |people| %>
    <li>
      <strong><%= people.name %></strong>
      <% unless current_user == people %>
        <%= link_to 'Send message', new_message_path(to: people.id), class: 'btn btn-default btn-sm' %>
      <% end %>
    </li>
  <% end %>
</ul>

messages_controller.rb

messages_controller.rb

class MessagesController < ApplicationController
  before_action :authenticate_user!

  def new
    @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
  end

  def create
    recipients = User.where(id: params['recipients'])
    conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
    flash[:success] = "Message has been sent!"
    redirect_to conversation_path(conversation)
  end
end

推荐答案

我认为没有简单的方法将您的 NameID.innerHTML 嵌入到ERB中.因此,我建议改用一些自定义jQuery.首先,在您的链接中添加一个ID:

I don't think there's an easy way to embed your NameID.innerHTML into ERB. So I would suggest using some custom jQuery instead. First, add an id to your link:

<%= link_to 'Send message', new_message_path(to: people.id), id: 'sendMessage', class: 'btn btn-default btn-sm' %>

然后在脚本内使用jQuery:

Then use jQuery inside your script:

$("#sendMessage").click(function(e){
  e.preventDefault();
  var nameId = $("#NameID_Div").html();
  // Then just assign the proper URL of the new_message_path with the proper NameID
  location.assign('/message/' + nameId + '/new');
});

这篇关于在erb中添加div的innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆