0

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 (graph not shown)

<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

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
1
  • please paste your complete code Commented Jul 11, 2015 at 12:30

1 Answer 1

1

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' %>

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');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Abraar. The code doesnt work. I think the sendmessage output needs to be instead of the people.id. The people.id is only defined as part of a loop from the @peoples variable from the controller. If I can put the jquery result into here then I wont need to use the people variable at all. How can I put the jquery output so that its passed as part of the to: parameter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.