Voting Functionality Ruby on Rails - Easy Question - ruby-on-rails

I want to add a "like" functionality to a blog I am creating in Ruby on Rails to let people "thumbs up" a post. Then I would like to be able to display the most liked posts in the side bar. I am new to rails so rely heavily on tutorials and stack overflow. Any good resources or what is this even called. I assumed "voting" for this question.

You should look up link_to_remote and using AJAX with rails. Here is a nice tutorial to get started: http://railsonedge.blogspot.com/2008/03/tutorial-beginning-ajax-with-rails-20.html
The idea would be:
The user clicks on "vote"
It sends an asynchronous message to the controller to save the vote. (use link_to_remote)
the :complete callback does something (eg "thanks for voting!")
the :update changes the voting zone (eg "voted. 6 votes on this")
There are a lot of tutorials on the subject.
EDIT: I wrote that a while ago and since then the best practices have changed. Now please take a look at how to do that using unobstrusive javascript. Good resources include Ryan Bates Bates's railscast #205 and the wikipedia article to understand the principle.
"Unobtrusive JavaScript" is an
emerging technique in the JavaScript
programming language, as used on the
World Wide Web. Though the term is not
formally defined, its basic principles
are generally understood to include:
Separation of functionality (the
"behavior layer") from a Web page's
structure/content and presentation
Best practices to avoid the problems
of traditional JavaScript programming
(such as browser inconsistencies and
lack of scalability)
Progressive enhancement to support
user agents that may not support
advanced JavaScript functionality[

I know this is not exactly what you are looking for, but there is a very good "star-rating" plugin that you could look at for some ideas of what to do. It uses ajax and all that goodie-goodieness ;)

The book Simply Rails 2 has something similar to this (a vote up, vote down sort of thing) and you might want to pick it up just because it is a good book to get you started on things.
Simply Rails 2.

gem thumbs_up for rails 3 or gem vote_fu for rails 2.* applications can help you...

Related

Does Ruby on Rails affect how a web page looks?

Most of the time, whenever I hit a website that looks "bubbly" in nature, and all prettified in those pastel-like colors, I think to myself, "This was probably done with Rails." And, lo and behold, after some digging into the site's information pages I discover this is actually true. So, I pose the question, not knowing much about Rails but enough about Django to understand how the database stuff works:
Does RoR have any display-specific qualities that affect how a web page looks? Or do all RoR devs naturally use the same Adobe tools to make everything look so ubiquitous?
Ruby on Rails is a server side technology, so it doesn't lend any specific quality to the user visible design. That said, it is a "trendy" technology so people who are likely to write their back-end code with RoR are likely to choose a particular "Web 2.0" style for their views.
As a Ruby on Rails developer, I can tell you that most Ruby on Rails developers are passionate about their work and we pay a lot of attention to details when building websites as much backend as front end. Its not just a trend, its a way of thinking and working.
No, it hasn't any display-specific qualities.
The theory is that RoR makes that backend stuff easier, so more time can, and apparently is, spent on the front end stuff.
Its all done with Mirrors. And CSS. :)
Rails is a very popular Web framework, it's just be coincidence that all the ones you've looked at have been rails apps.
What kind of sites have you been looking at to draw this hypothesis?
that's a funny question with a funny description :) ... bubbly!
As a madman, I develop with RoR, it's kind of rule in our area. We learn madness from the beginning, as a result of http://railsforzombies.org...
May wise men follow a wise path!
Short Answer: NO
However...
As a Rails developer I can say that due to the Agile nature of Rails and the speed in which you can develop web applications with Rails I do find myself having more time freed up on a project to spend polishing the user interface. I believe this may be a reason you often see more polished looking Rails sites.
So in my mind I believe your choice of framework can have a direct correlation to the end product that is produced!
Rails does add some stuff to the front end. Like to every html form, it will add a hidden input element authenticity_token.
You can also tell because rails URLs and form actions will never end with suffixes like .aspx or .php or .html or .jsp, and they won't usually append ?query=book&encoding=utf8 like you see on google. And they won't usually have superlong crufties like you see on amazon (eg http://www.amazon.com/Agile-Web-Development-Rails-Ruby/dp/1934356549/ref=sr_1_1?ie=UTF8&qid=1297922135&sr=8-1). Instead Rails prefers simple routing URLs. If amazon were written in rails, you might instead expect amazon.com/books/Agile-Web-Development-Rails-Ruby
So there are ways to spot a Rails app. I expect other web frameworks, especially the ones that emulate rails, would duplicate some or all of these features, so this isn't a sure-fire method, but it helps.

Would Ruby on Rails suit my work..?

I wanted to make a web site with the following basic features- (1)User registration for buyers and sellers. (2)profile pages (3)A buyer should be able to post work and should get profile links of the corresponding seller who has expertise in that work.
As time progresses i would want to add more features to the site.The freelancer sites where user can post jobs and get bids is the best example of the work.
(1)I want my code to be maintainable as i woud be adding features later on. (2)It should be quick to develop. (3)Resources should be available(not the entire thing, atleast in bits and pieces) for the above requirements and should not be tough to find for future enhancements. (4)Design should be decoupled from the buisness logic as i would outsource the design work.
I was thinking of Ruby on Rails for this work as i have experience in the MVC model and RoR looks cool.I am from the mobility domain so i don't know whether RoR will suit my work
Would RoR suit this purpose.If yes where can i find the resources to the above mentioned requirements.
Thanks
Ruby on Rails would be ideal for this type of website.
Check out some of these resources for info on how to use Ruby on Rails:
http://guides.rubyonrails.org/
http://railscasts.com/
http://www.railsforum.com/
I noticed you are already aware of TeachMeToCode, but there is a tag there for all the Rails 3 tutorials, with some blog tutorials and what looks like the beginning of a series on how to build a del.icio.us clone. Since they are in Rails 3, they would be well worth checking out:
http://teachmetocode.com/screencasts/tag/rails-3/
One of the best tutorials:
http://railsforzombies.org
It will let you have your own point of view quickly.
It depends on with which languages you have experience. Any good MVC framework will do the job just fine but if you like Ruby syntax RoR is definitely a good framework to develop this kind of application.

Rails confusion with coding

I'm desperate. I've read and tried many things with rails. The problem isn't that "big", it is just that I'm still getting used to the convention over conf world. Lack of examples, too.
I just want to be able to have a site where you have roles (think admin, normal user, etc) and each of these can view,say, posts, articles. However, the admin obviously has access to update/delete posts,articles. Users only post, things like that.
I can create the authentications, sessions, and all. But I don't know how to present (or refactor) code. Where does the code for the admin go? views?, what about for users?
Would I need to create controllers specifically for admins, and users, others, in order to display the same model objects (articles, posts) that I'll be dealing with? including views?
Thanks
There's a free Railscast that may help you with this called "Authorization with CanCan".
If you feel like parting with some money, Rails 3 in Action also covers this sort of thing in Chapters 6 and 7.
First, watch this screencast: Where administration goes.
Second, watch this one: Restricting Access.
Then this one: Super Simple Authentication.
This should clear up your confusion and help you wrap your mind around "The Rails Way" of doing authentication and administration.
Once your immediate crisis is over, I would STRONGLY recommend you pick up a copy of "Beginning Rails 3". The kind of frustration you're experiencing is normal: when you come from the world of basic HTML plus scripting (javascript and php most likely) it's really hard to "get" the big idea that a framework imposes on your design.
Beginning Rails 3 takes about 2-3 days to work completely through, front to back. When you're done you'll TRULY understand Rails and your work will feel like FUN, not frustration. It's really, really, really, really worth it to take the time to read the book. 2-3 days with the book will save you 2-3 months of mediocre coding and learning by trial and error.
There are lots of examples available. One of the best sources of information is Railscasts. You will find many screencasts on authentication (including the most recent (#250), as chance would have it). You will also find them on authorization, which is the role-based mechanism you describe.
Here's a pretty thorough comparison of many authorization plugins: http://steffenbartsch.com/blog/2008/08/rails-authorization-plugins/
I would also look into cancan (railscast), which is newer than that list and highly regarded.

Ruby on Rails, AJAX examles

I'd like to know what people think what websites are good examples of AJAX with Ruby on Rails at the moment.
I'm learning both and would like to see some good interesting examples of what can be done.
THanks,
Joe
http://haystack.com/
http://rubyonrails.org/applications
These links will open a new horizon for you.
Perhaps grab a copy of http://www.redmine.org/ and look it over. Maybe not the bleeding edge of ajax but there is some good stuff in there all around for rails development. I learned a lot from it.
If you're just getting started and interested in unobtrusive AJAX with jQuery, I have a blog post you might be interested in. Basically it takes a simple scaffold generated site and adds unobtrusive deleting with jQuery step by step - if that sounds like something you'd be interested in feel free to check it out.
Twitter.com is an excellent example of what can be done with AJAX. I believe the site is coded in Ruby on Rails as well.

How: Ruby on Rails to build a basic site [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I'm a compsci student who wants to get learn a little about web development -- I learn best by doing. I know basic html/css/php/javascript/xml, but since Ruby is one of my favourite scripting languages, I figured I'd learn Ruby on Rails.
I'd like to build a basic website for a friend's club at school that just provides information about their organization and services they offer, and have an admin panel on it that contains a very basic inventory system (item, number in inventory, cost -- that's it) in order to learn Ruby on Rails. I'll be hosting it on a computer on campus -- so I don't have to worry about hosting.
This might sound a little silly, but as someone who's never built a website themselves, I was wondering how exactly one goes about it with rails -- like, how do I make a basic layout for the main part of the site -- with things like "Home, About Us, Services, Contact, Club Executive" along the top? Do I have to make it in html and put it in the "view" section? The tutorials I've read on rails (Getting Started With Rails) actually make the basic inventory system seem easy, compared to this part, using a lot of the built in functionality of Rails and scaffolding. The Rails documentation is a tad bit confusing.
The "official" Rails book is quite good if you want to start building Rails applications. link
But actually is something like this:
Create the rails app using rails applicationname
Create the controllers. It seesm that for you one controller is actually enough, name it for example main: ruby script/generate controller main
Now you have a controller in app/controllers, called main_controller.rb. Here you can insert the actions you want this controller to respond. If you don't want the controller to do anything just show the view, then leave the method empty.
class Main < ActionPack::Controllers
def index
end
def about
end
def contact
end
(...)
end
Now you got a controller that will respond to index, about and contact.
Create the views for this controller in app/views/main/index.erb (and others, like about.erb)
You can simply use HTML if you want
Alternatively you can use a layout, that you have to define in app/views/layouts/main.rhtml In this layout use HTML, but wherewer you want to include the view, write <%= yield %>
Example:
<HTML>
<BODY>
<%= yield %>
</BODY>
</HTML>
You can include this layout in the controller by writing layout :main in the class (before the method declarations)
Now if you run ruby script/server in the root of the app you can access the pages you've created. They will be static of course, but this might get you going. You have to add models and some logic to your controllers to advance. I advice you to check the book I linked if you're interested in more, or check the alternatives of rails like merb (http://merbivore.org) which has some nice features and is usually faster, but lacks the maturity of rails.
I picked up the book "Agile Web Development with Rails", and it's excellent. It goes through building an online grocery cart.
You should read about Model-View-Controller architecture if you have not yet done so, as it is the basis for most web frameworks including Ruby on Rails.
It sounds to me like this site might not be the best way to learn Ruby on Rails.
Rails is really great for CRUD applications (applications which allow users to Create, Read, Update, and Delete records in a database). Since your site looks to be all static pages except for the "Contact Us" section (which I'm assuming is a form that sends an email with some kind of confirmation page), you're actually going to find yourself kind of fighting against "The Rails Way."
Ideally in a situation like this, you could just throw all of your static pages into the public/ directory and make a quick Rails scaffold for the "Contact Us" page.
But by doing that, you won't end up with a finished project that resembles a typical Ruby on Rails application, and in the worst case, you might find yourself having to "unlearn" or at least "relearn" a lot of the aspects of Rails programming.
I think building a CRUD application with several resources (the canonical "Rails blog in 15 minutes" is a great start. You'll learn more by practicing Rails conventions and seeing the kind of workflow and application that really allows Rails to shine.
Then when it comes time to build another mostly-static website, you'll know exactly what you'll need to do to go about it.
My 2 cents, anyway.
Start off with Mephisto. It will give a framework to achieve your goal rather fast... otherwise you may simply flounder learning the gazillion things involved in creating the rails website.
With a simple site, I'd go for a Ruby micro-framework. The three I like are: Sinatra, Ramaze or _why's 4k Camping (get the one with the bugs fixed). RoR would be overkill.
I must recommend Ramaze. If you already know Ruby, but don't know Rails yet, Ramaze is better suited to you because it is "closer to home" as far as Ramaze apps being pure(r) Ruby.
For your DB access, you get a choice of ORMs. Sequel is most popular among Ramazers, but there's also DataMapper and M4DBI.
As Alan Alavi already said: You should familiarize yourself with MVC, but that can be done simply by diving in and getting your hands dirty.

Resources