[英]passing an id from one controller to another - RAILS 5
How can I automatically place/pass the product_id in the input field? 如何自动在输入字段中放置/传递product_id?
i have 2 tables products & capturepages in my 我有2个桌子产品和Capturepages
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 意见/ 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 它们被定向到Capturepage表单页面
views/capturepages/_form.html.erb 视图/ 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
捕获了product_id,但product_id输入为空:
As you are using simple_form
, you should place value
inside input_html
. 在使用
simple_form
,应将value
放在input_html
。 Below code should fix your problem 下面的代码应该可以解决您的问题
<%= f.input :product_id, input_html: { value: @product_id } %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.