class User
def get_info
"user name and score here"
end
end
user1 = User.new
user2 = User.new
puts user1.get_info
puts user2.get_info
カテゴリー: programming
Ruby on Rails Todoリスト
Ruby on Railsを使用してTodoリストを作成する方法を説明します。まずは、以下の手順に従って新しいRailsアプリケーションを作成しましょう。
- ターミナルを開いて、プロジェクトを保存するディレクトリに移動します。
- 次のコマンドを実行して、新しいRailsアプリケーションを作成します。
rails new todo_list
- 作成したディレクトリに移動します。
cd todo_list
次に、Todoリストの機能を追加します。
- Todoアイテムを保存するためのデータモデルを作成します。
rails generate model TodoItem title:string description:text completed:boolean
- データベースにマイグレーションを実行して、新しいテーブルを作成します。
rails db:migrate
- Todoアイテムを操作するためのコントローラーを作成します。
rails generate controller TodoItems
config/routes.rbファイルを開き、以下のようにルートを設定します。
Rails.application.routes.draw do resources :todo_items root 'todo_items#index' end
app/controllers/todo_items_controller.rbファイルを開いて、アクションを追加します。
class TodoItemsController < ApplicationController before_action :set_todo_item, only: [:show, :edit, :update, :destroy] def index @todo_items = TodoItem.all end def show end def new @todo_item = TodoItem.new end def edit end def create @todo_item = TodoItem.new(todo_item_params) if @todo_item.save redirect_to @todo_item, notice: 'Todo item was successfully created.' else render :new end end def update if @todo_item.update(todo_item_params) redirect_to @todo_item, notice: 'Todo item was successfully updated.' else render :edit end end def destroy @todo_item.destroy redirect_to todo_items_url, notice: 'Todo item was successfully destroyed.' end private def set_todo_item @todo_item = TodoItem.find(params[:id]) end def todo_item_params params.require(:todo_item).permit(:title, :description, :completed) end end
app/views/todo_itemsディレクトリ内に、必要なビューファイル(index、show、new、edit)を作成します。app/views/todo_items/index.html.erbファイルを開いて、Todoアイテムのリストを表示するコードを追加します。
<h1>Todo Items</h1> <%= link_to 'New Todo Item', new_todo_item_path %> <ul> <% @todo_items.each do |todo_item| %> <li> <%= link_to todo_item.title, todo_item %> <% if todo_item.completed %> (Completed) <% else %> (Pending) <% end %> </li> <% end %> </ul>
これで、基本的なTodoリストアプリケーションが完成しました。 rails server コマンドを使用してRailsサーバーを起動し、 http://localhost:3000/todo_items にアクセスしてアプリケーションを確認できます。
You
No template for interactive request TodoItemsController#new is missing a template for request formats: text/html NOTE! Unless told otherwise, Rails expects an action to render a template with the same name, contained in a folder named after its controller. If this controller is an API responding with 204 (No Content), which does not require a template, then this error will occur when trying to access it via browser, since we expect an HTML template to be rendered for such requests. If that’s the case, carry on. ChatGPT
ChatGPT
このエラーは、TodoItemsController#new アクションに対応するビューテンプレートが見つからないために発生しています。Railsは通常、コントローラーと同じ名前のフォルダ内にある同じ名前のビューテンプレートを期待します。
解決するためには、app/views/todo_items ディレクトリ内に new.html.erb という名前のビューテンプレートファイルを作成する必要があります。その後、新しいTodoアイテムを作成するためのフォームをこのファイルに追加します。
例えば、次のようなフォームを追加できます。
erbCopy code
<!-- app/views/todo_items/new.html.erb --> <h1>New Todo Item</h1> <%= form_with(model: @todo_item, url: todo_items_path) do |form| %> <%= form.label :title %> <%= form.text_field :title %> <%= form.label :description %> <%= form.text_area :description %> <%= form.submit 'Create Todo Item' %> <% end %> <%= link_to 'Back', todo_items_path %>
これで、TodoItemsController#new アクションに対するビューテンプレートが追加されました。再度ブラウザでアクセスすると、エラーは解消されるはずです。
python 戻り値
def add(a, b):
return a+b
c = add(1,2)
print(c)
CSS クラスセレクター
index.html
@charset "utf-8";
body {
margin: 0;
}
.container {
width: 400px;
margin: 32px auto 0;
border: 8px solid blue;
}
.box-1 {
width: 100px;
height: 100px;
background-color: pink;
}
.box-2 {
width: 100px;
height: 50px;
background-color: skyblue;
}
.box-3 {
width: 100px;
height: 100px;
background-color: orange;
}
style.css
@charset "utf-8";
body {
margin: 0;
}
.container {
width: 400px;
margin: 32px auto 0;
border: 8px solid blue;
}
.box-1 {
width: 100px;
height: 100px;
background-color: pink;
}
.box-2 {
width: 100px;
height: 50px;
background-color: skyblue;
}
.box-3 {
width: 100px;
height: 100px;
background-color: orange;
}
ruby 配列
scores = [70, 90, 80, 65, 85, 77]
# filtered_scores = scores.filter do |score|
# score >= 80
# end
# p filtered_scores
scores.filter! do |score|
score >= 80
end
p scores
scores.filter! do |score|
score >= 80
end
p scores
ruby 配列
scores = [70, 90, 80]
scores.map! do |score|
score + 5
end
p scores
ruby 文字列の連結
# names = ["Taro", "Jiro", "Saburo"]
names = %W(Taro Jiro Saburo)
p names
# connected_names = names.join
connected_names = names.join(", ")
p connected_names
ruby 変数のスコープ
def triple(num)
result = num * 3
result
end
num = 20
puts triple(10)
puts num
Java タスク管理
package sample;
import java.util.ArrayList;
import java.util.Scanner;
public class TaskManager {
private static ArrayList<Task> tasks = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
boolean running = true;
while (running) {
System.out.println("1. タスクの追加");
System.out.println("2. タスクの削除");
System.out.println("3. タスクの表示");
System.out.println("4. タスクの状態を変更");
System.out.println("5. 終了");
System.out.print("操作を選択してください: ");
int choice = scanner.nextInt();
scanner.nextLine(); // 改行を読み飛ばす
switch (choice) {
case 1:
addTask();
break;
case 2:
deleteTask();
break;
case 3:
displayTasks();
break;
case 4:
changeTaskStatus();
break;
case 5:
running = false;
break;
default:
System.out.println("無効な選択です。");
}
}
scanner.close();
}
private static void addTask() {
System.out.print("タスクの名前を入力してください: ");
String name = scanner.nextLine();
System.out.print("タスクの期限を入力してください (例: YYYY-MM-DD): ");
String deadline = scanner.nextLine();
System.out.print("タスクの優先度を入力してください (高/中/低): ");
String priority = scanner.nextLine();
tasks.add(new Task(name, deadline, priority));
System.out.println("タスクを追加しました。");
}
private static void deleteTask() {
if (tasks.isEmpty()) {
System.out.println("削除するタスクはありません。");
return;
}
System.out.println("削除するタスクを選択してください:");
displayTasks();
System.out.print("削除するタスクの番号を入力してください: ");
int index = scanner.nextInt();
scanner.nextLine(); // 改行を読み飛ばす
if (index < 1 || index > tasks.size()) {
System.out.println("無効な番号です。");
} else {
Task deletedTask = tasks.remove(index - 1);
System.out.println(deletedTask.getName() + " を削除しました。");
}
}
private static void displayTasks() {
if (tasks.isEmpty()) {
System.out.println("タスクはありません。");
} else {
System.out.println("現在のタスク:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
}
}
private static void changeTaskStatus() {
if (tasks.isEmpty()) {
System.out.println("タスクはありません。");
return;
}
System.out.println("状態を変更するタスクを選択してください:");
displayTasks();
System.out.print("状態を変更するタスクの番号を入力してください: ");
int index = scanner.nextInt();
scanner.nextLine(); // 改行を読み飛ばす
if (index < 1 || index > tasks.size()) {
System.out.println("無効な番号です。");
} else {
Task task = tasks.get(index - 1);
System.out.print("新しい状態を入力してください (未完了/進行中/完了): ");
String newStatus = scanner.nextLine();
task.setStatus(newStatus);
System.out.println(task.getName() + " の状態を " + newStatus + " に変更しました。");
}
}
}
class Task {
private String name;
private String deadline;
private String priority;
private String status = "未完了"; // デフォルトの状態は未完了
public Task(String name, String deadline, String priority) {
this.name = name;
this.deadline = deadline;
this.priority = priority;
}
public String getName() {
return name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "タスク名: " + name + ", 期限: " + deadline + ", 優先度: " + priority + ", 状態: " + status;
}
}
ruby 論理演算子
def dividable?(a, b)
# if b != 0
# if a % b == 0
# true
# else
# false
# end
# else
# false
# end
if b != 0 && a % b == 0
true
else
false
end
end
puts dividable?(10, 2) # true
puts dividable?(10, 3) # false
puts dividable?(10, 0)
