【问题标题】:Why is my button_to creating blank entries? Ruby on Rails 3为什么我的 button_to 创建空白条目? Ruby on Rails 3
【发布时间】:2015-05-13 21:13:59
【问题描述】:

更新后不使用params,而是将参数传递到块中

似乎我的参数没有传递到控制器操作中。我正在尝试使用button_tosvc_tickets 表中创建一个条目。

我只在表格中输入了空白条目

这是button_to

<td class='create_service_ticket' style="width: 7%"><%= button_to 'Create Service Ticket',
                                                                          {controller: :svc_tickets,
                                                                           action: 'create',
                                                                           priority_level: 3,
                                                                           summary: event[:signature_name].to_s,
                                                                           description: 'description',
                                                                           closed: 0} %> </td>

SvcTicktsController

  def create
    @svc_ticket = SvcTicket.new(params[:svc_ticket])
    respond_to do |format|
      if @svc_ticket.save
        format.html { redirect_to :back, notice: 'Svc ticket was successfully created.' }
        #format.json { render json: @svc_ticket, status: :created, location: @svc_ticket }
      else
        format.html { render action: "new" }
        format.json { render json: @svc_ticket.errors, status: :unprocessable_entity }
      end
    end
  end

这是来自页面来源

  <form action="/svc_tickets?closed=0&amp;description=description&amp;priority_level=3&amp;summary=drop+-+WP-Admin+attempt" class="button_to" method="post">
    <div>
      <input type="submit" value="Create Service Ticket">
      <input name="authenticity_token" type="hidden" value="qWjFT8JDEUPXceQN3taodnInwerVEiCIKayJKBoEoTs=">
    </div>
  </form>

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2


【解决方案1】:

Rails 3 中没有实现此功能。请查看此方法的规范here。它是在 Rails 4 中实现的。

【讨论】:

    【解决方案2】:

    作为explained in the docs,Rails 3 中button_to 的第二个参数是传递给url_for 的选项哈希,所以它是传递params 的第二个参数:

    button_to('Create Service Ticket', {
      controller: :svc_tickets,
      params: {
        priority_level: 3,
        summary: event[:signature_name].to_s,
        description: 'description',
        closed: 0
      }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 2015-04-16
      相关资源
      最近更新 更多