There are 2 tables, User and Post, and on the user’s page I’m trying to make form_for to enter a post and further output to the screen. my profile_controller.rb

 def show @user = current_user @post = current_user.posts.build end 

Here is the post_controller.rb :

 before_action :set_post, only: [:show, :edit, :update] respond_to :html, :js def index @post = Post.all end def show end def new @post = Post.new end def edit end def create @post = current_user.posts.new(post_params) respond_to do |format| if @post.save format.html { redirect_to @post, notice: 'Post was successfully created.' } format.js{} format.json { render :show, status: :created, location: @post } else format.html { render :new } format.json { render json: @post.errors, status: :unprocessable_entity } end end end def update respond_to do |format| if @post.update!(post_params) format.html { redirect_to @post, notice: 'Post was successfully updated.' } format.json { render :show, status: :ok, location: @post } else format.html { render :edit } format.json { render json: @post.errors, status: :unprocessable_entity } end end end private def set_post @post = Post.find(params[:id]) end def post_params params.require(:post).permit(:comment,:user_id,:created_at) end 

routes.rb:

  devise_for :users, controllers: {registrations: 'registrations'} root "welcome#index" # путь к profile/index get "/profile" => "profile#show", as: :profile 

And the actual form on which he swears, c variable @post show.html :

  <%= form_for @post do |f| %> <div class="one"> <%= f.label :comment %><br /> <%= f.text_field :comment, autofocus: true%> </div> <div class="btn-link"><br /> <%= f.submit "Send" %> </div> 

There are suspicions that these are methods in post_controller that do not work properly and send me to post_path, but I don’t take what it is.

  • routes.rb fstudiyu! - anoam
  • changed, added routes in question - holden

1 answer 1

No routes for PostController .

Need to add in routes.rb

 resources :post 

Here is the documentation for more fine-tuning.

UPD: How, quite rightly, they suggest, for resource controllers, you need to choose plural names. Routes are created this way, but FormBuilder may not appreciate this. So it makes sense to rename the controller class to PostsController , respectively, the file in which it lies renamed to posts_controller.rb .
Rots, then take the form:

 resources :posts 
  • There is still a little trouble with the fact that in Rails it is customary to use the plural in the controller name and the resources argument. I first corrected the answer, and then I noticed that there was also such a hole in the question. - D-side
  • @ D-side, thanks. Completed. - anoam