在rails 4中保存布尔值更改

时间:2016-04-30 20:18:20

标签: ruby-on-rails

我试图从仪表板#home而不是todos#edit更新我的:rails 4中已完成的字段。我已经尝试了PATCH和POST - 没有保存,但是使用POST时,当我单击复选框时,我没有收到404错误,而使用PATCH我执行' PATCH http://localhost:3000/dashboard 404(未找到) &#39 ;.我不确定我必须使用哪一个,因为我看到有人使用POST和一些PATCH。

我的路线

patch 'todos/:id' => 'todos#completed'

我的信息中心#home.html.erb

<%= check_box_tag 'todo[completed]', todo.id, todo.completed, data: { remote: true, url: url_for(controller: :todos, action: :completed, id: todo), method: "PATCH" }, id: todo.id %>
<%= label_tag todo.id, "COMPLETE", :class => 'strikethrough' %>

我的Todo完成行动

  def completed
    if @todo.update_attributes(:completed => params[:completed])
      flash[:success] = "Wowzers."
      redirect_to action: :show
    else
      flash.now[:error] = "Not so wowzers..."
      render :new
    end
  end

我的Rails控制台为PATCH

Started PATCH "/todos/e3309e65a5f225ec696807ee1f5be3" for 80.193.7.142 at 2016-04-30 20:11:42 +0000
Cannot render console from 80.193.7.142! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by TodosController#update as JS
  Parameters: {"todo"=>{"completed"=>"19"}, "id"=>"e3309e65a5f225ec696807ee1f5be3"}
  Todo Load (0.2ms)  SELECT  "todos".* FROM "todos" WHERE "todos"."slug" = ?  ORDER BY "todos"."id" ASC LIMIT 1  [["slug", "e3309e65a5f225ec696807ee1f5be3"]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
DEPRECATION WARNING: You attempted to assign a value which is not explicitly `true` or `false` ("19") to a boolean column. Currently this value casts to `false`. This 
will change to match Ruby's semantics, and will cast to `true` in Rails 5. If you would like to maintain the current behavior, you should explicitly handle the values 
you would like cast to `false`. (called from block in update at /home/g9stuart4/recital/app/controllers/todos_controller.rb:46)
   (0.0ms)  commit transaction
Redirected to http://g9stuart4.koding.io:3000/dashboard
Completed 302 Found in 6ms (ActiveRecord: 0.4ms)


Started PATCH "/dashboard" for 80.193.7.142 at 2016-04-30 20:11:43 +0000
Cannot render console from 80.193.7.142! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

ActionController::RoutingError (No route matches [PATCH] "/dashboard"):
  actionpack (4.2.5.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.3.0) lib/web_console/middleware.rb:20:in `block in call'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.5.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.5.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.5.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.5.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.5.1) lib/rails/engine.rb:518:in `call'
  railties (4.2.5.1) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /home/g9stuart4/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'

非常感谢任何信息。

2 个答案:

答案 0 :(得分:1)

这就是我解决它的方法。

我的控制器

  def completed
    if @todo.update_attribute(:completed, [true, false])
      flash[:success] = "Wowzers."
      redirect_to dashboard_path
    else
      flash.now[:error] = "Not so wowzers..."
      render :new
    end
  end

我的复选框&amp;输入

<%= check_box_tag 'todo[completed]', todo.id, todo.completed, data: { remote: true, url: url_for(controller: :todos, action: :completed, id: todo), method: "POST" }, id: todo.id %>
<%= label_tag todo.id, "COMPLETE", :class => 'strikethrough' %>

我的路线

post 'todos/:id' => 'todos#completed'

我还回滚了我的迁移并添加了null:false和默认值:0到我的复选框,因为当我在修补时,我一直变得很多,我认为这是问题的一部分。

无论如何,复选框现在检查并取消选中并更新数据库。

答案 1 :(得分:0)

可以将自定义路由添加到todos资源路由。

resources :todos do
  member do
    put 'completed'
  end
end

尝试在终端中运行rake路由以查看它产生的内容。

参数hash是嵌套的; 'completed'值嵌套在'todo'中,所以这一行

if @todo.update_attributes(:completed => params[:completed])

应该是

if @todo.update_attributes(:completed => params[:todo][:completed])

您确定可以将网址添加为数据属性吗?我之前没见过,这让我觉得这就是你得到404错误的原因。

我原以为你需要用一个表单标签来包围checkbox-tag,或者使用button_to helper然后你可以设置样式看起来像一个复选框。字体真棒有你可以使用的图标。