Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6946983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:38:07+00:00 2026-05-27T13:38:07+00:00

new.html.erb <h3>Add post</h3> <%= form_tag :controller=>’posts’, :action=>’create’ do %> <%= label :q, :Title %>

  • 0

new.html.erb

<h3>Add post</h3>

<%= form_tag :controller=>'posts', :action=>'create' do %>
    <%= label :q, :Title %>
    <%= text_field :data, :title, :class => :addtextsize %><br/>
    <%= label :q, :Content %>
    <%= text_area  :data, :content, :rows=>10 , :class => :addtextarea %><br/>
    <%= label :q, :Category %>
    <%= select :data, :category_id, @categories_select %><br/>
    <%= label :q, :Tags %>
    <%= text_field :data, :tags, :class => :addtextsize %><br/>
    <%= label :q, :Submit %>
    <%= submit_tag "Add Post" %>
<% end %>

create action of PostController.rb

def create
    @categories_select = Category.all.collect {|c| [ c.category_name, c.id ] }
    @addpost = Post.new params[:data]
    if @addpost.save
        flash[:notice] = "Post has been saved successfully."
        redirect_to posts_path
    else
        flash[:notice] = "Post can not be saved, please enter information."
        render :new
        #redirect_to new_post_path
    end
end

Getting users.id i need to insert it into posts table. How can i do it ?

posts table

                                   Table "public.posts"
   Column    |          Type          |                     Modifiers                      
-------------+------------------------+----------------------------------------------------
 id          | integer                | not null default nextval('posts_id_seq'::regclass)
 title       | character varying(100) | not null
 content     | character varying(500) | not null
 created_at  | date                   | 
 updated_at  | date                   | 
 tags        | character varying(55)  | not null default '50'::character varying
 category_id | integer                | not null default 1
 user_id     | integer                | 
Indexes:
    "posts_pkey" PRIMARY KEY, btree (id)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:38:07+00:00Added an answer on May 27, 2026 at 1:38 pm

    Devise provides a helper method called current_user which you can use to get the authenticated user. So, you could put a hidden text input in your form, like this:

    <%= hidden_field_tag :user_id, current_user.id %>
    

    This will allow it to be passed into your collection of params.

    The other option would be to manually add it in the create method:

    def create
        @categories_select = Category.all.collect {|c| [ c.category_name, c.id ] }
        @addpost = Post.new params[:data]
        @addpost.user_id = current_user.id
        if @addpost.save
            flash[:notice] = "Post has been saved successfully."
            redirect_to posts_path
        else
            flash[:notice] = "Post can not be saved, please enter information."
            render :new
            #redirect_to new_post_path
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def create @addpost = Post.new params[:data] if @addpost.save flash[:notice] = Post has been saved
After inserting new html to DOM,I need to add some listeners to it. But
I have a button that, when pressed, will add a new HTML row. Within
I have question model which has many options. In my question controller new action
I have a form for creating an album like following: # app/views/albums/new.html.erb <h2>Details of
I am trying to write specs for the posts/show.html.erb view of a simple application
I'm using jQuery to change the HTML of a tag, and the new HTML
I have noticed recently, when I apply a template to a new HTML website,
I'm really new to HTML, but I can't find anywhere how to return the
I'm new to HTML coding and I know HTML has some reserved characters for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.