передача идентификатора от одного контроллера к другому - RAILS 5 - PullRequest
0 голосов
/ 15 сентября 2018

Как я могу автоматически поместить / передать product_id в поле ввода?

У меня есть 2 таблицы продуктов и страницы захвата в моем

схема

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

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

Модель

product.rb
has_many :capturepages

capturepage.rb
belongs_to :product

контроллер страниц захвата

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

вид / products.show.html.erb

когда пользователь нажимает на ссылку ниже:

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

они направлены на страницу формы захвата

Вид / 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 %>

URL-адрес гласит: http://localhost:3000/capturepages/new?product_id=1 захватывает product_id, но ввод product_id пуст:

enter image description here

1 Ответ

0 голосов
/ 15 сентября 2018

Поскольку вы используете simple_form, вы должны поместить value внутрь input_html. Ниже код должен исправить вашу проблему

<%= f.input :product_id, input_html: { value: @product_id } %>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...