今までは問題なかったのに、急にno method errorがでてきました。
class CommentsController < ApplicationController before_action :set_comment, only: [:show, :edit, :update, :destroy, :upvote] before_filter :authenticate_user! respond_to :html def index if params[:user_id] @user = User.find(params[:user_id]) @comments = @user.comments else @comments = Comment.all end end def show @comment = Comment.find(params[:id]) end def create @song = Song.find(params[:song_id]) @comment = @song.comments.new(comment_params) @comment.user = current_user respond_to do |format| if @comment.save format.html { redirect_to @song, notice: '投稿に成功しました' } format.json { render json: @comment, status: :created, location: @comment } else format.html { render action: "new" } format.json { render json: @comment.errors, status: :unprocessable_entity } end end end def destroy @comment.destroy respond_to do |format| format.html { redirect_to :back, notice: 'コメントを削除しました' } format.json { head :no_content } end end def upvote @comment.upvote_by current_user redirect_to :back end private def set_comment @comment = Comment.find(params[:id]) end def comment_params params.require(:comment).permit(:song_id, :body, :user_id, :image) end end
class SongsController < ApplicationController before_action :set_song, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!, except: [:index, :show] # GET /songs # GET /songs.json def index if params[:user_id] @user = User.find(params[:user_id]) @songs = @user.songs # ここでアソシエーションが生きる else @songs = Song.all end end # GET /songs/1 # GET /songs/1.json def show @song = Song.find(params[:id]) end # GET /songs/new def new @song = current_user.songs.build end # GET /songs/1/edit def edit end # POST /songs # POST /songs.json def create @song = current_user.songs.build(song_params) respond_to do |format| if @song.save format.html { redirect_to @song, notice: 'Song was successfully created.' } format.json { render :show, status: :created, location: @song } else format.html { render :new } format.json { render json: @song.errors, status: :unprocessable_entity } end end end # PATCH/PUT /songs/1 # PATCH/PUT /songs/1.json def update respond_to do |format| if @song.update(song_params) format.html { redirect_to @song, notice: 'Song was successfully updated.' } format.json { render :show, status: :ok, location: @song } else format.html { render :edit } format.json { render json: @song.errors, status: :unprocessable_entity } end end end # DELETE /songs/1 # DELETE /songs/1.json def destroy @song.destroy respond_to do |format| format.html { redirect_to songs_url, notice: 'Song was successfully destroyed.' } format.json { head :no_content } end end def upvote @song = Song.find(params[:id]) @song.upvote_by current_user redirect_to :back end private # Use callbacks to share common setup or constraints between actions. def set_song @song = Song.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def song_params params.require(:song).permit(:title, :body) end end
もしかしたら知らないうちに何か余計なことをかもしれませんが、ついさっきまで普通に動いていたのに不思議です。
rails4を使っていて、たまにこういったことがあるのですが、これはバグか何かなのでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/14 10:27 編集
2017/10/16 00:14
2017/10/16 05:13 編集