Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Insert data from one table to another in Rails 3
    text
    copied!<p>I have a User table with id , name , e-mail etc...</p> <p>i want to pass the current logged user id into another table Post , so that each post contain corresponding user id.</p> <p>controller side </p> <pre><code>**app/controller/post_controller.rb** class PostController &lt; ApplicationController def index @post=Post.new @posts=Post.all end def create @post=Post.new(params[:post]) respond_to do |format| if @post.save format.html { redirect_to :controller=&gt;"post" ,:action=&gt;"index" } format.json { render json: @post, status: :created, location: @post } #redirect_to :controller=&gt;"post" ,:action=&gt;"index" else format.html { redirect_to :controller=&gt;"home" ,:action=&gt;"index" } format.json { render json: @post, status: :created, location: @post } end end end </code></pre> <p>view part </p> <pre><code>**app/views/post/questions.html.erb** &lt;%=form_for :post do |f| %&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; Title:&lt;%=f.text_field :title %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;h3&gt;Question&lt;/h3&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;%=f.text_area :body %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Tag:&lt;%=f.text_field :tag %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;h3&gt;&lt;%= f.submit "Post Your Question" %&gt;&lt;/h3&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;%end%&gt; </code></pre> <p>Post table contain userid , title ,body ,tag</p> <p>i have stored the user id value in session when user successfully logs in........</p> <p>please tell me how to add id field into Post table.....</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload