プログラミング初学者です。
rails 6を用いて、DM機能がついたアプリケーションを開発しているのですが、form_withのメソッドについてわからないことがあります。
form_withはメソッドのデフォルトの値がpostだと思うのですが、roomを新しく作るために作ったform_withのメソッドがなぜかgetになってしまい、下記のエラーが出てしまいます。
No route matches [GET] "/rooms"
以下が関係していると考えているコードになります。
app/views/users/show.html.erb
<% provide(:title, @user.name) %> <div class="row"> <aside class="col-md-4"> <section class="user_info"> <h1> <%= gravatar_for @user %> <%= @user.name %> <% if @user.id == current_user.id %> <% else %> <% if @is_room == true %> <%= link_to "チャットへ", room_path %> <% else %> <%= form_with(model: @room, local: true) do |f| %> <%= fields_for @entry do |e| %> <%= e.hidden_field :user_id, value: @user.id %> <% end %> <%= f.submit "チャットを始める"%> <% end %> <% end %> <% end %> </h1> </section> <section class="stats"> <%= render 'shared/stats' %> </section> </aside> <div class="col-md-8"> <%= render 'follow_form' if logged_in? %> <% if @user.microposts.any? %> <h3>投稿数(<%= @user.microposts.count %>)</h3> <ol class="microposts"> <%= render @microposts %> </ol> <%= will_paginate @microposts %> <% end %> </div> </div>
app/controllers/users_controller.erb
class UsersController < ApplicationController before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :following, :followers] before_action :correct_user, only: [:edit, :update] before_action :admin_user, only: :destroy def index @users = User.paginate(page: params[:page]) @search = User.ransack(params[:q]) end def show @user = User.find(params[:id]) @microposts = @user.microposts.paginate(page: params[:page]) @current_user_entry = Entry.where(user_id: current_user.id) @user_entry = Entry.where(user_id: @user.id) unless @user.id == current_user.id @current_user_entry.each do |cu| @user_entry.each do |u| if cu.room_id == u.room_id then @is_room = true @room_id = cu.room_id end end end if @is_room else @room = Room.new @entry = Entry.new end end end def new @user = User.new end def create @user = User.new(user_params) if @user.save @user.send_activation_email flash[:info] = "メールが送信されました。アカウントを認証してください。" redirect_to root_url else render 'new' end end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if @user.update(user_params) flash[:success] = "アカウント編集が完了しました" redirect_to @user else render 'edit' end end def destroy @user = User.find(params[:id]) @user.destroy flash[:success] = "#{@user.name}さんを削除しました" redirect_to users_url end def search @search = User.ransack(params[:q]) @search_users = @search.result.page(params[:page]) end def following @title = "Following" @user = User.find(params[:id]) @users = @user.following.paginate(page: params[:page]) render 'show_follow' end def followers @title = "Followers" @user = User.find(params[:id]) @users = @user.followers.paginate(page: params[:page]) render 'show_follow' end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end def correct_user @user = User.find(params[:id]) redirect_to(root_url) unless current_user?(@user) end def admin_user redirect_to(root_url) unless current_user.admin? end end
config/routes.rb
Rails.application.routes.draw do get 'password_resets/new' get 'password_resets/edit' get 'sessions/new' root 'static_pages#home' get '/about', to:'static_pages#about' get '/contact', to:'static_pages#contact' get '/signup', to:'users#new' get '/login', to:'sessions#new' post '/login', to:'sessions#create' delete '/logout', to:'sessions#destroy' get '/micropost_search', to:'microposts#search' get '/user_search', to:'users#search' resources :users do member do get :following, :followers end end resources :users resources :account_activations, only: [:edit] resources :password_resets, only: [:new, :create, :edit, :update] resources :microposts, only: [:create, :destroy] resources :relationships, only: [:create, :destroy] resources :messages, only: [:create] resources :rooms, only: [:create, :show] end
以下が参考にした記事になります。
https://qiita.com/nojinoji/items/2b3f8309a31cc6d88d03
ご教授お願いいたします。
あなたの回答
tips
プレビュー