首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在RoR控制器中从html获取数据时出现问题

在RoR控制器中从html获取数据时出现问题
EN

Stack Overflow用户
提问于 2020-05-27 17:53:56
回答 1查看 53关注 0票数 0

我在试着做一个产品推车。我希望当我将产品添加到购物车时,能够选择产品的数量。

这是LineItemsController

代码语言:javascript
复制
    class LineItemsController < ApplicationController
  include CurrentCart
  before_action :set_line_item, only: [:show, :edit, :update, :destroy]
  before_action :set_cart, only: [:create]

  def index
    @line_items = LineItem.all
  end

  def show
  end

  def new
    @line_item = LineItem.new
  end

  def edit
  end

  def create
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product)
    @line_item.quantity = params['q'].to_i

      if @line_item.save
        redirect_to @line_item.cart, notice: 'Item added to cart.'
      else
        render :new
      end
  end

  def update
      if @line_item.update(line_item_params)
        redirect_to @line_item, notice: 'Line item was successfully updated.'
      else
        render :edit
      end
  end

  def destroy
    @cart = Cart.find(session[:cart_id])
    @line_item.destroy
    redirect_to cart_path(@cart), notice: 'Item successfully removed.'
  end

  private
    def set_line_item
      @line_item = LineItem.find(params[:id])
    end

    def line_item_params
      params.require(:line_item).permit(:product_id, :quantity)
    end
end

这是view/line_item/_form.html.erb

代码语言:javascript
复制
<%= simple_form_for(@line_item) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.association :product %>
    <%= f.association :cart %>
  </div>

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

这是view/product/show.html.erb

代码语言:javascript
复制
    <% content_for :body_class, 'bg-light' %>

<section class="section instrument-show">
  <div class="columns">
    <div class="column is-8">
      <%= render 'shared/breadcrumb', category: @product.category %>
      <h1 class="title is-2"><%= @product.title %></h1>

      <!--ul class="pv1">
        <%# if @product.brand? %>
        <li class="inline-block pr3"><%#= @product.brand %></li-->
        <%# end %>

        <!--li class="inline-block pr3"><%#= @product.model %></li-->

        <%# if @product.condition? %>
        <!--li class="inline-block pr3"><%#= @product.condition %></li>
        <%# end %>
      </ul-->

      <div class="feature-image">
        <%= image_tag(@product.image_url(:default).to_s) %>
      </div>

      <div class="content pa4 mt3 bg-white border-radius-3">

      <h3 class="subtitle is-4">Description</h3>
      <%= @product.description %>


      <h3 class="subtitle is-4 pt5">Product Specs</h3>

      <table class="table is-narrow">
        <% if @product.category.present? %>
        <tr>
          <td class="has-text-weight-bold">Category:</td>
          <td><%= link_to @product.category.name, @product.category %></td>
        </tr>
        <% end %>

        <%# if @product.finish %>
          <tr>
            <td class="has-text-weight-bold">Finish:</td>
            <td><%#= @product.finish %></td>
          </tr>
        <%# end %>

        <tr>
          <td class="has-text-weight-bold">Model:</td>
          <td><%#= @product.model %></td>
        </tr>
      </table>
    </div>
    </div>
    <div class="column is-3 is-offset-1">
      <div class="bg-white pa4 border-radius-3">
        <h4 class="title is-5 has-text-centered"><%= number_to_currency(@product.price) %></h4>
        <p class="has-text-centered mb4">Sold by <%#= @product.user.name %></p>

        <form>
          <input type="number" name="q" min="1" max="50" step="1" class="input label">
        </form>


        <%= button_to 'Add to cart', line_items_path(product_id: @product), class: 'button is-warning add-to-cart' %>
      </div>
    </div>
  </div>

  <% if user_signed_in? && current_user.admin? %>
    <%= link_to 'Edit', edit_admin_product_path(@product), class: 'button' %>
  <% end %>
</section>

现在,在这个字符串中,@line_item.quantity = params['q'].to_i来自

代码语言:javascript
复制
<form>
  <input type="number" name="q" min="1" max="50" step="1" class="input label">
</form>

我得到了“零”,我现在非常不知道我应该怎么做才能得到实际的数字。

附注:这是view/line_item/new.html.erb

代码语言:javascript
复制
<h1>New Line Item</h1>

<%= render 'form', line_item: @line_item %>

<%= link_to 'Back', line_items_path %>
EN

回答 1

Stack Overflow用户

发布于 2020-05-27 20:47:20

new.html.erb是输入你的数据,如果你提交了,数据就会流动到你的控制器里面去创建方法,同时show只是展示数据,你不能在show里面输入表单,我建议试着把new.html.erb的q部分放到simple_form_for标签里面,因为它是数字,你可以使用number_field rails tag helper作为示例,名字:q,范围在1之间。50步骤1,下面是可能对您的问题有所帮助的示例

当您从new.html.erb提交时,您将收到params:q作为参数的一部分,并使用create方法保存它

代码语言:javascript
复制
<%= simple_form_for(@line_item) do |f| %>
  <%= f.error_notification %>

  <%= number_field(:q, in: 1.0..50.0, step: 1) %>

  <div class="form-inputs">
    <%= f.association :product %>
    <%= f.association :cart %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62040297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档