我在视图中使用简单的ajax切换部分,允许用户将消息切换为已读和未读。当我切换时,我失败了。
MessagesController #show找不到没有ID的MessageCopy 应用程序跟踪:messages_controller.rb:“show”中的21(操作的第一行)
show.html.erb
<%= render :partial => "read_unread_buttons" %>
# read_unread_buttons partial
<div id="read_buttons" class="ajaxButtonText">
<% if @message.read == true %>
<%= link_to "Mark Unread",
{:url => {:action => "read_unread_toggle", :id => @message.id},
:update => {:success => "read_buttons", :failure => "Error"}},
{:class=>"messageButton", :id => "markUnread"}, :remote => true %>
<% else %>
<%= link_to "Mark as Read",
{:url => {:action => "read_unread_toggle", :id => @message.id},
:update => {:success => "read_buttons", :failure => "Error"}},
{:class=>"messageButton", :id=>"markUnread"}, :remote => true %>
<% end %>
</div>
# messages controller
def show
@message = current_user.received_messages.find(params[:id])
@message.read = true
@message.save(:validate => false)
render :layout => "messages"
end
def read_unread_toggle
@message = current_user.received_messages.find(params[:id])
@message.read = !@message.read
@message.save(:validate => false)
render :partial => "read_unread_buttons", :locals => {:message => @message, :id => params[:id] }, :layout => false
end
答案 0 :(得分:0)
首先,我建议使用生成的路径url方法生成href,您将在下面的示例中看到这一点。
其次,我假设您的routes.rb看起来像这样:
resources :messages do
member do
put 'read_unread_toggle'
end
end
然后你的read_unread_buttons部分看起来应该是这样的
# read_unread_buttons partial
<div id="read_buttons" class="ajaxButtonText">
<% if @message.read == true %>
<%= link_to "Mark Unread", read_unread_toggle_message_path(@message),
{ :update => { :success => "read_buttons", :failure => "Error" } },
{ :class => "messageButton", :id => "markUnread" }, { :method => :put, :remote => true } %>
<% else %>
<%= link_to "Mark as Read", read_unread_toggle_message_path(@message),
{ :update => { :success => "read_buttons", :failure => "Error" } },
{ :class => "messageButton", :id=>"markUnread" }, { :method => :put, :remote => true } %>
<% end %>
</div>
具体来说,您忘记指定链接应使用的HTTP方法。