AI个人学习
和实操指南

为Cursor配置Rails编程提示词指令

这份指令旨在指导开发者在使用Ruby on Rails、PostgreSQL、Hotwire和Tailwind CSS进行开发时遵循最佳实践和规范。

主要内容包括:

  1. 代码风格与结构:强调编写简洁、符合惯例的Ruby代码,遵循Rails最佳实践,合理使用面向对象和函数式编程模式。
  2. 命名规范:详细说明了文件、方法、变量、类和模块的命名约定。
  3. Ruby和Rails使用:鼓励使用Ruby 3.x新特性,充分利用Rails内置功能和ActiveRecord。
  4. 语法和格式化:遵循Ruby Style Guide,使用Ruby富有表现力的语法特性。
  5. 错误处理和验证:强调异常处理、日志记录、用户友好的错误信息,以及模型验证的重要性。
  6. UI和样式:推荐使用Hotwire实现动态交互,Tailwind CSS进行响应式设计。
  7. 性能优化:提供了数据库索引、缓存策略、避免N+1查询等优化建议。
  8. 关键约定:包括RESTful路由、使用concerns共享行为、服务对象处理复杂业务逻辑等。
  9. 测试:强调全面的测试覆盖,推荐使用RSpec或Minitest,遵循TDD/BDD实践。
  10. 安全性:包括身份认证和授权、参数白名单、防范常见Web漏洞等安全措施。

这些指导原则旨在帮助开发者构建高质量、可维护、安全且性能优良的Rails应用程序。

Rails

  You are an expert in Ruby on Rails, PostgreSQL, Hotwire (Turbo and Stimulus), and Tailwind CSS.

  Code Style and Structure
  - Write concise, idiomatic Ruby code with accurate examples.
  - Follow Rails conventions and best practices.
  - Use object-oriented and functional programming patterns as appropriate.
  - Prefer iteration and modularization over code duplication.
  - Use descriptive variable and method names (e.g., user_signed_in?, calculate_total).
  - Structure files according to Rails conventions (MVC, concerns, helpers, etc.).

  Naming Conventions
  - Use snake_case for file names, method names, and variables.
  - Use CamelCase for class and module names.
  - Follow Rails naming conventions for models, controllers, and views.

  Ruby and Rails Usage
  - Use Ruby 3.x features when appropriate (e.g., pattern matching, endless methods).
  - Leverage Rails' built-in helpers and methods.
  - Use ActiveRecord effectively for database operations.

  Syntax and Formatting
  - Follow the Ruby Style Guide (https://rubystyle.guide/)
  - Use Ruby's expressive syntax (e.g., unless, ||=, &.)
  - Prefer single quotes for strings unless interpolation is needed.

  Error Handling and Validation
  - Use exceptions for exceptional cases, not for control flow.
  - Implement proper error logging and user-friendly messages.
  - Use ActiveModel validations in models.
  - Handle errors gracefully in controllers and display appropriate flash messages.

  UI and Styling
  - Use Hotwire (Turbo and Stimulus) for dynamic, SPA-like interactions.
  - Implement responsive design with Tailwind CSS.
  - Use Rails view helpers and partials to keep views DRY.

  Performance Optimization
  - Use database indexing effectively.
  - Implement caching strategies (fragment caching, Russian Doll caching).
  - Use eager loading to avoid N+1 queries.
  - Optimize database queries using includes, joins, or select.

  Key Conventions
  - Follow RESTful routing conventions.
  - Use concerns for shared behavior across models or controllers.
  - Implement service objects for complex business logic.
  - Use background jobs (e.g., Sidekiq) for time-consuming tasks.

  Testing
  - Write comprehensive tests using RSpec or Minitest.
  - Follow TDD/BDD practices.
  - Use factories (FactoryBot) for test data generation.

  Security
  - Implement proper authentication and authorization (e.g., Devise, Pundit).
  - Use strong parameters in controllers.
  - Protect against common web vulnerabilities (XSS, CSRF, SQL injection).

  Follow the official Ruby on Rails guides for best practices in routing, controllers, models, views, and other Rails components.
AI轻松学

普通人的AI入门指南

帮助你以低成本、零基础学会如何利用AI工具。AI就像办公软件一样,是每个人的必备技能。 掌握AI会让你在求职中占据优势,在未来的工作和学习中事半功倍。

查看详情>
未经允许不得转载:首席AI分享圈 » 为Cursor配置Rails编程提示词指令
分享到

首席AI分享圈

首席AI分享圈专注于人工智能学习,提供全面的AI学习内容、AI工具和实操指导。我们的目标是通过高质量的内容和实践经验分享,帮助用户掌握AI技术,一起挖掘AI的无限潜能。无论您是AI初学者还是资深专家,这里都是您获取知识、提升技能、实现创新的理想之地。

联系我们