【问题标题】:Assign Value Rails Button分配值 Rails 按钮
【发布时间】:2013-06-25 14:22:47
【问题描述】:

此时我需要为我的数据库中的列分配一个值 我正在使用 Rails 4.0

我在视图中有这个链接:

link_to 'Finalizar', note_finalizar_path(note), class: 'btn btn-success btn-mini'

我在控制器中有一个这样的方法:

def finalizar
    @note = Note.where(:id => Note.id).first
    if @note.update_column(:estado, false)
        redirect_to notes_path, :notice => "OK"
    else
        redirect_to notes_path, :notice => "NO"
    end
end

问题是,当我点击按钮时,我得到了这个错误:

undefined method `id' for #Class:0xa675070>

抱歉,我的英语不是我的母语,感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails ruby button ruby-on-rails-4


    【解决方案1】:

    你可能需要这样的东西:

    链接

    link_to 'Finalizar', note_finalizar_path(note), class: 'btn btn-success btn-mini'
    

    控制器

    def finalizar
      @note = Note.find(params[:note_id])
      @note.update_attribute(:estado, false)
      if @note.estado
        redirect_to notes_path, :notice => "OK"
      else
        redirect_to notes_path, :notice => "NO"
      end
    end
    

    【讨论】:

    • 我照你说的做了,但现在的问题是找不到没有 ID 的笔记
    • 发布你的日志,我会调查它,服务器日志,我也在link_to编辑了我的答案
    • 日志的开头是这个 Started GET "/notes/5/finalizar" for 127.0.0.1 at 2013-06-25 10:02:29 -0500 [1m[36mActiveRecord::SchemaMigration Load (0.4ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m 由 NotesController#finalizar 处理为 HTML 参数:{"note_id"=>"5"} 完成 404 未在 17ms 内找到 ActiveRecord::RecordNotFound - 无法'找不到没有 ID 的注释:activerecord (4.0.0.rc2) lib/active_record/relation/finder_methods.rb:266:in `find_with_ids'
    • 在控制器中使用@note = Note.find(params[:note_id])
    猜你喜欢
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多