Mam stronke ze zdjeciem pod ktorym moge wyswietlac i ukrywac komentarze za pomoca Ajaxa:
show.html.erb w kontrolerze photo
…
<%= link_to_remote “Pokaż komentarze:”, :update => ‘comments’,
:url => {:controller => ‘comment’, :action => ‘comments_show’}, :failure => “alert(‘Pojawił się błąd.’)”,
:complete => “$(‘comments’).show(); $(‘show_comments’).hide();$(‘hide_comments’).show();”, :with => “‘photo_id=#{@photo.id}’” %>
<%= link_to_remote “Ukryj komentarze:”, :update => ‘comments’,
:url => {:controller => ‘comment’, :action => ‘comments_show’ }, :failure => “alert(‘Pojawił się błąd.’)”,
:complete => “$(‘comments’).hide(); $(‘show_comments’).show();$(‘hide_comments’).hide();” %>
…
<%= link_to_remote “Pokaż komentarze:”, :update => ‘comments’,
:url => {:controller => ‘comment’, :action => ‘comments_show’}, :failure => “alert(‘Pojawił się błąd.’)”,
:complete => “$(‘comments’).show(); $(‘show_comments’).hide();$(‘hide_comments’).show();”, :with => “‘photo_id=#{@photo.id}’” %>
<%= link_to_remote “Ukryj komentarze:”, :update => ‘comments’,
:url => {:controller => ‘comment’, :action => ‘comments_show’ }, :failure => “alert(‘Pojawił się błąd.’)”,
:complete => “$(‘comments’).hide(); $(‘show_comments’).show();$(‘hide_comments’).hide();” %>
Plik wyswietlajacy wszystkie komentarze
_comments_show.rhtml
<% @comments.each do |com| %>
<% if @users[com.user_id-1].nil? -%>
Konto usunięte.
<% else -%>
<%= @users[com.user_id-1].screen_name %>
<% end -%>
<%= com.body %>
<% if (@user.id == com.user_id) or (@user.root) -%>
<%= link_to "Usuń komentarz ", :action => “delete”, :controller => “comment”, :comment_id => com.id %>
<% end -%>
<% end %>
<%= com.body %>
<% if (@user.id == com.user_id) or (@user.root) -%>
<%= link_to "Usuń komentarz ", :action => “delete”, :controller => “comment”, :comment_id => com.id %>
<% end -%>
w kontrolerze comment do wyswietlania mam funkcje
def comments_show
@comments = Comment.find_all_by_photo_id(params[:photo_id])
@users = User.find :all
@user = User.find(session[:user_id])
render :partial => “comments_show”
end
i tu jest na razie ok.
Teraz chce moc edytowac komentarz i robie tak:
w _comments_show.rhtml dodaje
…
<%= link_to "Edytuj komentarz ", :action => “edit”, :controller => “comment”, :comment_id => com.id %> /
<%= link_to "Usuń komentarz ", :action => “delete”, :controller => “comment”, :comment_id => com.id %>
…
a w kontrolerze comments_controller dodaje taka metode:
def edit
@comment = Comment.find(params[:comment_id])
if param_posted?(:comment)
if @comment.update_attributes(:comment)
redirect_to :controller => ‘photo’, :action => ‘show’, :id => @comment.photo_id
else
flash[:notice] = “Nie udało się zmienić komentarza”
redirect_to :controller => ‘comment’, :action => ‘edit’, :comment_id => @comment.photo_id
end
end
end
i tworze formularz do edytujacy komentarz:
<% form_for :comment do |form| %>
<%= error_messages_for “comment” %>
<%= form.text_area :body, :size => “30x5” %>
<%= form.submit “Aktualizuj” %>
<% end %>
<%= form.text_area :body, :size => “30x5” %>
<%= form.submit “Aktualizuj” %>
I jak wyswietle ten komentarz i zmienie go, edytuje ale nie koniecznie text_area, w params mi wysyla, ale dostaje komunikat
Couldn’t find Comment without an ID
Parameters:
{“authenticity_token”=>“32cdb94170d766e3ff69469647b6c0b2cebc5729”,
“comment”=>{“body”=>“aaaa”},
“commit”=>“Aktualizuj”}
Prawdopodobnie to jest jakiś głupi błąd, ale nie mogę sobie z tym poradzić.
Proszę o pomoc.