I have some simple code for a thumbs up/thumbs down functionality.
Clicking “agree” sends an ajax count to increase the agree field by one,
clicking disagree increases the “disagree” count by one. The text
returned is both values with the updated one shown as “oldval
+1”
Is there any good way to make this pretty? The most I can think of is a
boolean argument specifying agree/disagree, but that doesn’t really make
things any simpler :-/
This look pretty good! I have to set up a bunch of migrations before I
can test, so I’ll just ask now; what do I do with the Tweet class?
(Still a bit of a newbie here!) I notice that you don’t say self.agree
as the method name - this means that they’re only added for the scope
the one request, right?
This look pretty good! I have to set up a bunch of migrations before I
can test, so I’ll just ask now; what do I do with the Tweet class?
(Still a bit of a newbie here!) I notice that you don’t say self.agree
as the method name - this means that they’re only added for the scope
the one request, right?
nope, they are normal instance methods. I assume you are using something
like DataMapper or ActiveRecord to handle your database access.
So in your model class Tweet (I assume that’s what it’s called) you add
these
two methods. Then they can be called on all the Tweet instances.
BTW, don’t you have to call tweet.save or similar some time in your
code?
So for anyone curious, here’s how I wound up doing it:
(no haml just because I’m too busy learning rails and ruby already
It probably could be made more dry, but this is pretty good and has the
benefit of being written. I haven’t included the controller, because
its a bit more complex, dealing with different databases of anonymous
and non-anonymous users…
Hi, Peter,
If you have that many <% … %> all in a row, and are placing that much
logic into your view, then you are probably ripe for a helper In the
file
app/helpers/tweet_helper.rb you can declare a method like
Which cleans up your view code, you can then assign the values with
something like
<% if tweet.votedOn != nil -%>
<% agree , disagree = agree_and_disagree(tweet) %> <%= agree %>
– <%= disagree %>
<% else -%>
…
Also, to make your code more readable and intuitive, you can take
if tweet.votedOn == 1 #agree
elsif tweet.votedOn == 0 #disagree
elsif tweet.votedOn == 2 #neutral
And create methods for these in your model (or the tweet’s class,
however
you have it defined). Something like
class Tweet < ActiveRecord::Base
…
def agree?() votedOn == 1 end
def disagree?() votedOn == 0 end
def neutral?() votedOn == 2 end
end
Then your helper code can be just
if tweet.agree?
elsif tweet.disagree?
elsif tweet.neutral?
You can, of course, name these whatever you think makes them the most
readable and is the most memorable. This means that you don’t have to
remember that they agree if votedOn is equal to some internally defined
value, it helps keep those values inside the class, and just provides
intuitive, self-descriptive methods as an interface. After you get that
class made, you should ideally never have to know that 1 means agree,
and so
on. That can all be handled internally, making your class easier to work
with. (If you are working with this class a lot, you’ll thank yourself
later
for establishing a nice set of methods to encapsulate internal logic).
Also, FWIW, if you don’t like passing arguments in the URL, you can
define a
route like
/tweets/agree/:id/agree
/tweets/agree/:id/disagree
By declaring them as members in your routes and having methods in your
controller for them to invoke. If you are interested in that, here is a
wonderful guide on routing
just wanted to make sure the end result works I know how many futile
hours of bug hunting such small mistakes can cause. Had enough myself.
No criticism of your code was intended.