Christian Hernandez
Christian Hernandez

Reputation: 39

Rails link_to link fills out form on page

Rails 4.0 Ruby 2.0

I am trying to create a link that fills out a textarea field on the current page with data that I already have.

Here is my code for the view (that doesn't work)

<% if feed_item.in_reply_to %>
<%= link_to "reply", mentions_path(:content => "@#{feed_item.user.username}") %>
<% end %>

All I want is @#{feed_item.user.username} to be entered into a textarea called micropost_content

Any help or something that'll point me in the right direction would be great

--C

EDIT:

Everyone seems to be confused with the @...I'm putting the @ because I want it to display on the page as

@usernmae

Since the username is stored as username in the DB.

It's not because I'm trying to insert an "instance variable" :-)

Can't this be done with Javascript?

This better explains it:

enter image description here

Upvotes: 1

Views: 221

Answers (3)

Almaron
Almaron

Reputation: 4147

Things to do and check (just in case):

  • check that mentions_path is routing to the correct controller and action, where you want the form
  • remove the @ from the evaluated string and check the params[:content] value in the controller, it should contain the correct username
  • if you are using a form_for helper in the view
    • then assign that content to the appropriate field of the model, that you are building the form for
  • else
    • assign that content to an instance variable like @username and then use text_area_tag :field_name, @username

Ok, if you need that on the same page just do something like

$(document).ready -> 
  $("#reply_link_id").click ->
    username = $('#username_link_id').html()
    $('#textarea_id').val(username).focus()
    return false

Upvotes: 2

Christian Hernandez
Christian Hernandez

Reputation: 39

I got the behavior I was looking for by doing this

<%= link_to "reply", '#', :onclick => "document.getElementById('micropost_content').value='@#{feed_item.user.username} ';return false;" %>

Although now I am looking for a way to put the cursor there

Upvotes: 2

MZaragoza
MZaragoza

Reputation: 10111

on the new page you can check if the value that you are passing exist and added to the value of the field that you want populated

Upvotes: 0

Related Questions