0

I want to make a form for submitting the info needed to create an instance of model X and save it to the database, but I have a slight problem: I know how to add form fields like text areas and what not and then how to make those values accessible when the create method is called, but what if I want to also send make a value that is not part of the form accessible? For example, what if I want to be able to access some text in a <div> of my html document and send that to the create method for model X (so it can be stored in like a content variable or something)? How do I do that?

Kvass
  • 8,294
  • 12
  • 65
  • 108

2 Answers2

0

Short answer is you can't - to send data to the server, it has to be in an form field.

Longer answer - there are ways around this and you have several options - put the text in a textarea and style it to look like an div, or put it in a hidden field when the template is created, or use javascript to copy it into a hidden field ... all depends on what you are trying to do really. Perhaps you could give some more detail?

chrispanda
  • 3,204
  • 1
  • 21
  • 23
  • Yeah -- basically when the user highlights text I am copying that text from the document and dumping into a div on my page (that part works). Then I want to create a new Excerpt model when the user clicks the Create button on the page, and I want the model to have its "content" attribute be set to the text that I had dumped into the div. – Kvass Jun 29 '11 at 20:18
  • so when you copy the content to the div, can you also copy the content into a hidden field in the form, and then pick that up when the form is submitted to the server? – chrispanda Jun 29 '11 at 20:26
  • I imagine it's possible but I don't know how to do that. – Kvass Jun 29 '11 at 20:26
  • so how are you copying the text that the user highlights into the div? Are you just using javascript, or jquery, or something else? – chrispanda Jun 29 '11 at 20:28
  • It says here: http://guides.rubyonrails.org/form_helpers.html that "Hidden inputs are not shown to the user, but they hold data like any textual input. Values inside them can be changed with JavaScript." I need to know how to do this. – Kvass Jun 29 '11 at 20:34
  • ok - so I've been using jquery so long for this kind of thing I'll probably give you poor advice if i try and write pure js - but have a look at http://stackoverflow.com/questions/4342229/copy-selected-text-from-one-textarea-to-another which does the sort of thing you are trying to do – chrispanda Jun 29 '11 at 20:35
  • Basically you just put a field in your template - like you would for a text field - but instead of f.text_field you put f.hidden_field. Give it an id, and then use javascript to paste your text into the value attribute. Look at the docs for hidden_field and look at the html it generates and you'll soon work it out – chrispanda Jun 29 '11 at 20:42
0

In your model you want to use attr_accessor

What you can do is use attr_accessor which creates a virtual attribute that you can use in the model but is discarded after that.

For example:

app/models.post.rb

class Post < ActiveRecord::Base

  attr_accessor :some_variable 

  #now you can access 'some_variable' this variable in the model
  #try Post.some_variable in the console

end

app/views/posts/_form.html.erb

<div id="form"> 

      <div class="field">
        <%= f.label :some_variable  %> 
        <%= f.text_field :some_variable  %>
      </div>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
</div>
thenengah
  • 42,557
  • 33
  • 113
  • 157
  • Sorry -- I really appreciate the detailed response but I couldn't follow all of that -- can you try to make the answer more specific to my issue (I wrote a more detailed description of what I'm trying to do in the comment to the previous answer) – Kvass Jun 29 '11 at 20:20
  • Ok. Let me know if that makes sense. I was just trying to give a real example of how I did what you are trying to do. You cannot get variables from a div unless you you javascript, but that is not what you should be doing. And if you really feel that you need to do that you need to explain why a div and not a form input value. – thenengah Jun 29 '11 at 20:28