Routing Error No route matches [POST] "/posts/new"となります。
user 1: 対 post Nの関係を持たせuserに紐づいたpostを作成したいです。
posts_controller
def create
current_userはapplication.html.erbに定義してある
@user = current_user
@post = @user.posts.build(post_params)
if @post.save
redirect_to current_user_url
else
render :new
end
end
post/new view
<h2>ポストアクション</h2> <%= form_with model: @post,local: true do |f| %> <%= f.label :'写真' %> <%= f.file_field :picture ,class:'form-control' %> <%= f.label :'説明' %> <%= f.text_area :description ,class:'form-control' %> <%= f.submit '作成', class: 'btn btn-success' %> <% end %>routes
resources :users do
resources :posts, only: [:create,:new]
end
resources :posts
model
class Post < ApplicationRecord
belongs_to :user
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
end
class User < ApplicationRecord
has_secure_password
has_many :posts
end
このようなコードを書いております。
初歩的なことですが分からないので教えていただけると幸いです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/05 06:57