Font selector using radio button

T

Tom Dunn

I have looked all around to try and find an answer to this. I have the
Agile book and although it has lots of information, it has relatively
little on radio buttons. I have a website in which I want users to be
able to format their posts in relation to font and color. My goal is to
allow them to click a radio button next to their choice of font and
their choice of color. So far I have been unable to find anything in
the book, on this site, or on the internet to help me out.


Here is my controller:

class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
def index
@posts = Post.find:)all, :eek:rder => "created_at DESC")

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end


# GET /posts/1
# GET /posts/1.xml
def show

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end

# GET /posts/new
# GET /posts/new.xml
def new
@post = Post.new


respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end



# POST /posts
# POST /posts.xml
def create
@post = Post.new(params[:post])


respond_to do |format|
if @post.save
format.html { redirect_to(@post) }
format.xml { render :xml => @post, :status => :created,
:location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status =>
:unprocessable_entity }
end
end
end


end


Here is the part of my view of the post form:


<%= error_messages_for :post %>
<div id="desc">
<% form_for(@post) do |f| %>

#there is other stuff here it just isn't important and is a little
long.
#here is where I want the buttons. I just have no idea how to do it.

</div>



Sorry if this is something very simple. I am new to ruby and thus don't
have much experience. Any help would be greatly appreciated.
 
B

Brian Candler

In your copy of the Agile book, look at the section entitled "working
with non-model fields" (section 22.7 or 23.7 depending on which edition
you have). Replace text_field_tag with radio_button_tag or check_box_tag
as appropriate. Then your controller can read params[:color] or
whatever, and style the response appropriately.

You'll find more info in the Rails API documentation under
ActionView::Helpers::FormTagHelper - go to http://api.rubyonrails.org/

However I'm afraid your question has very little to do with Ruby (the
programming language).

For help with Rails (the web application framework), you would be best
advised to ask your question on a Rails mailing list or forum. There are
some Rails users here, but this is not the primary focus of this mailing
list.

For help with styling web pages, you could ask a CSS mailing list.

If you want the style changes to be reflected immediately when someone
presses a button, you'll want to use Javascript and/or a Javascript
helper library (I prefer jQuery over Rails' default Prototype). Again,
there are separate mailing lists for those.

HTH,

Brian.
 
7

7stud --

Tom said:
I have looked all around to try and find an answer to this. I have the
Agile book and although it has lots of information, it has relatively
little on radio buttons. I have a website in which I want users to be
able to format their posts in relation to font and color. My goal is to
allow them to click a radio button next to their choice of font and
their choice of color. So far I have been unable to find anything in
the book, on this site, or on the internet to help me out.


First, this isn't a forum for rails questions. This is a general ruby
programming forum. rails is a complex piece of software written in
ruby. People who use that complex piece of software often hang out
together and discuss various issues that they are having with the
software. You need to locate one of those places. They are generally
titled "Ruby on Rails" or just "Rails".

Second, your question really has nothing to do with rails nor with ruby
programming in general. rails software executes on "the server side".
A person looking at your web page is on the "client side". In between
the server side and the client side is a bunch of cables commonly called
the internet. The font and color choices that you want to give your
users can all be handled on the client side with javascript and css.
The server side shouldn't be bothered with such mundane details. You
can add a simple onclick javascript function to your radio buttons that
you can use to immediately change any css property, e.g. font size and
colors.

I guess if you want to save the user's choices for the next time they
visit, you also need to set a cookie. Once again, you can do that with
javascript. If you set a cookie with javascript, then all subsequent
requests that the client sends to the server will include the cookie.
Then you can have your rails app read the cookie and send back a page
that has the font and color set according to the choices read from the
cookie.

In any case, that is all stuff you should have learned before ever even
attempting rails. Web programming requires a general knowledge of html,
css, javascript, and at least one server side programming language (e.g.
php, java, python ruby). If someone led you to believe that learning
rails would obviate the need to learn those subjects first, they were
sadly mistaken. You are flying blind without those subjects, and if
any little thing goes wrong with your rails program, you have no skills
to solve the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top