导轨 - 如何更新控制器的单一属性 [英] Rails - How to update a single attribute in controller

查看:96
本文介绍了导轨 - 如何更新控制器的单一属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的轨道,努力实现一个简单的任务。我想切换一个布尔属性做上的图像点击。 在我看来,我的链接是这样的:

I am new to rails and trying to achieve a simple task. I want to toggle a boolean attribute "done" on an image click. In my view, my link looks like:

<%= link_to image_tag("done.png"),
    feed_item,
    :controller => :calendars, :action=>:toggle_done,:id=> feed_item.id,
    :title => "Mark as done", :remote=> true, :class=>"delete-icon" %>

我添加了一个路线如下:

I added a route as follows:

resources :calendars do
    get 'toggle_done', :on => :member
end

在控制器中,我创建了一个方法:

in the controller, I have created a method:

def toggle_done
    @calendar = Calendar.find(params[:id])
    toggle = !@calendar.done
@calendar.update_attributes(:done => toggle)

respond_to do |format|
  flash[:success] = "Calendar updated"
  format.html { redirect_to root_path }
  format.js
end

当我点击的形象,没有任何反应我看到了以下错误:

When I click on the image, nothing happens I see the following error:

Started GET "/toggle_done" for 127.0.0.1 at 2010-12-27 13:56:38 +0530
ActionController::RoutingError (No route matches "/toggle_done"):

我相信有一些很琐碎,我在这里失踪。

I am sure there is something very trivial I am missing here.

推荐答案

仅供参考,ActiveRecord的规定切换和切换!方法做的完全一样的名字在一个给定的属性要求。

Just FYI, ActiveRecord provides "toggle" and "toggle!" methods to do exactly as the name claims on a given attribute.

路线 - >

resources :calendars do
  member do
    put :toggle_done
  end
end

查看(确保你使用正确的路由路径和正确的HTTP动词)。 GET不应被用于改变数据库按照RESTful架构。 - >

View (ensure you're using the correct route path and the correct HTTP Verb). GET should not be used for changing the database as per RESTful architecture. ->

<%= link_to image_tag("done.png"), feed_item, toggle_done_calendars_path(feed_item)
    :title => "Mark as done", :remote=> true, :class=>"delete-icon", :method => :put %>

控制器 - >

Controller ->

def toggle_done
  @calendar = Calendar.find(params[:id])
  @calendar.toggle!(:done)

  respond_to do |format|
    flash[:success] = "Calendar updated"
    format.html { redirect_to root_path }
    format.js
  end
end

这篇关于导轨 - 如何更新控制器的单一属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆