views:

77

answers:

2

I have a partial in my rails app that loads the vote form (It's just a select with numbers ranging from 1-5). I'm now making another partial that loads up if the user has already voted it's suppose to be an EDIt partial where you get selected the value that you voted and if you wanted to you could change it. But for some reason is not working, here's the code for it.

#Finds the vote by passing the user and the article  
<%= @vote = Vote.find_vote_by(current_user,@article) %>

#Renders the partial with the vote variable loaded up with the vote that was found
<%= render :partial => "votes/edit_vote", :locals => {:vote => @vote} %>

And this is the partial

<% form_for(vote, :url => {:controller => 'votes', :action => 'edit'}) do |f| %>

<%= error_messages_for :vote %>

<p><%= f.hidden_field :article_id %></p>
<p>
  <%= f.label :value, "Value for the vote: "%>
  <%= f.select :value, {"1" => "1","2" => "2","3" => "3","4" => "4", "5" => "5"}, :selected => vote.value %>
</p>
<p>
   <%= f.submit "Cloud-it!" %>
</p>

<% end %>

But for some reason the vote variable is not containing anything not the article_id, nor the value method, any ideas?

EDIT1: Per request here's what's debug @vote is outputting (It it indeed a sane value)

attributes: 
created_at: 2010-09-02 14:39:04
updated_at: 2010-09-02 14:39:04
id: 1
value: 4
article_id: 1
user_id: 1
attributes_cache: {}

EDIT2

I tried clearing the partial code, in order to output this:

<%= debug @vote%>
<%= @vote.value %>

If i debug @vote it comes out perfect, will all the attributes and such. But whenever i add the second line it, It's not working it tells me that there's no .value, i tried .id, .article and nothing is as if it didn't exist. Any ideas?

EDIT3

Here's the vote by

 named_scope :find_vote_by, lambda {|user,article| {:conditions => {:user_id => user, :article_id => article}}}
A: 

So first thing to fix is this line:

<%= @vote = Vote.find_vote_by(current_user,@article) %>

Should be:

<% @vote = Vote.find_vote_by(current_user,@article) %>

The former is for outputting in ERB and the latter is for executing arbitrary ruby code.

Next, put a line below that like so:

<%= debug @vote %>

And make sure it's a sane value. If not, paste the definition of your Vote class method find_vote_by.

EDIT: In that case it's probably just because using :locals => {...} makes instance variables, so you want @vote in your partial with the form.

thenduks
Sorry that was indeed a type on that ERB output.
Gotjosh
EDIT up there it's still not working.
Gotjosh
Please post the code for the method `Vote.find_vote_by`
thenduks
I have posted it for a while now, any comments?
Gotjosh
I don't receive notifications when you update the question, only when you comment on my answer :)... Very strange. Try two things, to find out if the named scope is the problem, change the first bit of code in your question to `@vote = Vote.first`. If `@vote.value` is still nothing then see about asking for it's class, to make sure it's actually a `Vote`: `<%= @vote.class.name %>`
thenduks
Also when you say you try `@vote.value` and it 'tells you' there's no `.value`, what do you mean exactly? An exception? Paste the actual exception too, if you don't mind.
thenduks
that's it i tried it on the rails console and that's it! The named scope it's actually returning a named scope and not a vote. But how do i fix it?
Gotjosh
Hm, well, it seems to me this might actually be some kind of naming-convention problem. Forget the named scope for a sec and try this: `Vote.find_by_user_and_article( current_user, @article )`
thenduks
That method didn't exist neither look at my answer to see how i fixed it :-). Thank you very much for your help without your help i couldn't had done it.
Gotjosh
+1  A: 

The reason behind it, it's that named scopes actually return named scopes, and you can't access the attributes just like it were a Vote class. I fixed this by changing the way to retrieve the vote and just forgetting about using that named scope. I accomplished it by using:

 <% @vote = current_user.votes.find_by_article_id(@article)%>

which is a Rails method and actually returns a vote class. Then i just passed it to the partial and the magic worked!

Thank you so much to thenduks, without his help i couldn't had done it.

Gotjosh