passing an id from one controller to another - RAILS 5

passing an id from one controller to another - RAILS 5



How can I automatically place/pass the product_id in the input field?



i have 2 tables products & capturepages in my



schema


products
t.string "name"
t.string "description"

capturepages
t.string "name"
t.string "email"
t.integer "product_id"



models


product.rb
has_many :capturepages

capturepage.rb
belongs_to :product



capturepages controller


class CapturepagesController < ApplicationController
before_action :set_capturepage, only: [:show, :edit, :update, :destroy]

def new
@capturepage = Capturepage.new
@product_id = params[:product_id]
@product = Product.find(params[:product_id])
end

def create
@capturepage = Capturepage.new(capturepage_params)
@product = @capturepage.product
respond_to do |format|
if @capturepage.save
format.html redirect_to @product.affiliatecompanylink, notice: 'Capturepage was successfully created.'
format.json render :show, status: :created, location: @capturepage
else
format.html render :new
format.json render json: @capturepage.errors, status: :unprocessable_entity
end
end
end


private
def set_capturepage
@capturepage = Capturepage.find(params[:id])
end

def capturepage_params
params.require(:capturepage).permit(:name, :email, :product_id)
end
end



views/products.show.html.erb



when the user clicks on the below link:


<%= link_to "buy now", new_capturepage_path(product_id: @product.id), target:"_blank" %>



they are directed to the capturepage form page



views/capturepages/_form.html.erb


<%= simple_form_for(@capturepage) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
<%= f.input :product_id, value: @product_id %>
<%= f.input :name, placeholder: "Your Name", label: false %>
<%= f.input :email, placeholder: "Your Email", label: false %>
</div>

<div class="form-actions">
<%= f.button :submit, "get product" %>
</div>
<% end %>



the url states: http://localhost:3000/capturepages/new?product_id=1
capturing the product_id but the product_id input is empty:


http://localhost:3000/capturepages/new?product_id=1



enter image description here




1 Answer
1



As you are using simple_form, you should place value inside input_html. Below code should fix your problem


simple_form


value


input_html


<%= f.input :product_id, input_html: value: @product_id %>






Pavan - thank you sooo much!!!!

– ARTLoe
Sep 15 '18 at 17:51



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế