简体   繁体   中英

Using a html dropdown to determine user choice in Rails

So I've been building my paste app on my local machine, and I've come across a small problem that has been bugging me. I need to figure out how to do syntax highlighting. In PHP, I could pass the user choice:

<select name="paste_syntax">
<option value="ruby">Ruby</option>
<option value="c">C</option>
<option value="html-xml">HTML/XML</option>
<option value="php">PHP</option>
</select>

and use $_POST to pass the variable chosen here, to highlight the syntax. Eg:

<pre class='mypostvar'>some code</pre>

However, in Rails, I'm not sure how to do this. I was thinking of creating a paste column in the paste table, which is linked to a paste_syntax table, containing all the paste syntax options. The 2 are linked. My question is, should I do this, or is there another way? Help is appreciated! ~Adil

I'd use syntax highlighter and then I wouldn't bother rails for this, but you can change the highlighting by changing class to your code, with some jQuery for example:

# coffeescript
jQuery ->
  $('#syntax').change (ev) ->
    selection = $(ev.target).val()
    $('#code').removeClass().addClass('brush: ' + selection);


-# haml
= select_tag :syntax, options_for_select([['ruby', 'ruby'], ['c', 'c']])
%pre#code{class: 'brush: ruby'}= "def ok \n  puts 'ok'\nend"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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