タイトル通りの内容なのですが、DBに保存したデータを編集してボタンを押すと編集されずにDBに新しく追加されてしまいます
どうすればいいのでしょうか?
正しいコードを教えてほしいです
routes
Rails.application.routes.draw do resources :tweets root 'tweets#index' end
tweets_controler
class TweetsController < ApplicationController def index @tweets = Tweet.all end def new end def show @tweet = Tweet.find_by(:id => params[:id]) end def create @tweet = Tweet.new @tweet.day = params[:tweet][:day] @tweet.title = params[:tweet][:title] @tweet.about = params[:tweet][:about] if @tweet.save redirect_to tweets_path else render 'new' end end def edit @tweet = Tweet.find_by(:id => params[:id]) end def update @tweet = Tweet.find_by(:id => params[:id]) if @tweet.update(:id => params[:id]) redirect_to tweets_path else render 'edit' end end def destroy @tweet = Tweet.find_by(:id => params[:id]) @tweet.destroy redirect_to tweets_path end end
index
<div class="container"> <% @tweets.each do |tweet| %> <h2><%= tweet.day %></h2> <h1><%= tweet.title %></h1> <%= link_to '[Show]', tweet %> <%= link_to "[Delete]", tweet_path(tweet.id), method: :delete, data: { confirm: "are you sure?" } %><br> <hr> <% end %> <%= link_to "投稿する", '/tweets/new', class: "btn btn-primary" %> </div>
new
<div class="container"> <% require "date" %> <% now = (Date.today).jisx0301 %> <div class="form-horizontal"> <%= form_for Tweet.new do |f| %> <%= f.label :day, "日付を入力" %><br> <%= f.text_field :day, value: now, :class => "form-control" %><br> <%= f.label :title, "タイトルを入力" %><br> <%= f.text_field :title, :class => "form-control" %><br> <%= f.label :about, "内容を入力" %><br> <%= f.text_area :about, :class => "form-control" %><br> <%= f.submit "投稿", :class => "btn btn-primary" %> <%= link_to "Home", tweets_path, class: "btn btn-primary" %> <% end %> </div> </div>
show
<div class="container"> <h1><%= @tweet.title %></h1> <h2><%= @tweet.about %></h2> <%= link_to "編集", edit_tweet_path(@tweet.id), class: "btn btn-primary" %> <%= link_to 'Back', tweets_path %> </div>
edit
<div class="container"> <% require "date" %> <% now = (Date.today).jisx0301 %> <div class="form-horizontal"> <%= form_for Tweet.new do |f| %> <%= f.label :day, "日付を入力" %><br> <%= f.text_field :day, value: now, :class => "form-control" %><br> <%= f.label :title, "タイトルを入力" %><br> <%= f.text_field :title,value: @tweet.title, :class => "form-control" %><br> <%= f.label :about, "内容を入力" %><br> <%= f.text_area :about, value: @tweet.about, :class => "form-control" %><br> <%= f.submit "編集", :class => "btn btn-primary" %> <%= link_to "Home", tweets_path, class: "btn btn-primary" %> <% end %> </div> </div>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/12/06 22:38
2016/12/08 01:15