ruby on rails - How do I link my bids to posts? -


i'm having trouble linking bids posts. have created post_id column in bids table (via adding resources:posts in migration).

i want able go /posts/:id/new_bid create new bid post. currently, can create new bid going /posts/2/new_bid (for example), still says 'post_id: nil' in database.

routes

root 'static_pages#home' 'add' => 'posts#new' 'posts' => 'posts#index' '/posts/:id/new_bid' => 'bids#new'  resources :posts resources :bids  

bid controller

class bidscontroller < applicationcontroller  def new     @bid = bid.new     @post = post.find(params[:id]) end  def show     @bid = bids.find(params[:id]) end  def index     @bid = bids.all end  def create      @bid = bid.new(bid_params)     @post = post.find(params[:post_id])     if @bid.save         redirect_to root_path         flash[:notice] = 'bid received!'     else         render 'new'     end end  def bid_params     params.require(:bid).permit(:price, :company_name, :company_street, :company_city,                                 :company_zip, :company_phone, :company_email, :post_id) end  end 

bid model

class bid < activerecord::base     belongs_to :post end 

post model

class post < activerecord::base     has_many :bids end 

schema

 activerecord::schema.define(version: 20150704152313)    create_table "bids", force: :cascade |t|     t.datetime "created_at",     null: false     t.datetime "updated_at",     null: false     t.decimal  "price"     t.integer  "post_id"   end    add_index "bids", ["post_id"], name: "index_bids_on_post_id"  create_table "posts", force: :cascade |t|    t.datetime "created_at", null: false    t.datetime "updated_at", null: false    t.string   "fromstreet"    t.string   "fromcity"  end  end 

bids form:

 <%= form_for(@bid) |f| %>    <div style='float: left; width: 50%;'>    <div class="row">         <div class="col-md-6 col-md-offset-3">     <%= f.label "price" %>    <%= f.text_field :price, class: "form-control" %> <br></br> <%= f.submit "submit bid", class: "btn btn-primary" %> </div> </div> </div>  <% end %> 

can ?

when add validation tests bids model such as:

validates :price, presence: true valid_email_regex = /\a[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :company_email, presence: true,                  format: { with: valid_email_regex} 

i following error when enter bid violates 1 of validation tests:

 undefined method `id' nil:nilclass  

and line:

  <%= f.hidden_field :post_id, :value => @post.id %>  

is highlighted in red.

try hidden_field in form.

<%= f.hidden_field :post_id, :value => @post.id %> 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -