【问题标题】:rails strong parameters not work correctlyrails strong 参数无法正常工作
【发布时间】:2017-04-13 11:46:15
【问题描述】:

我有一个 Rails 应用程序。我在保存表单时遇到了奇怪的问题

这是我的票模型。

class Ticket < ApplicationRecord
  belongs_to :user
  has_many :ticketissues , inverse_of: :ticket

  accepts_nested_attributes_for :ticketissues, :reject_if => lambda { |a| a[:body].blank? }

end

这是售票模式

    class Ticketissue < ApplicationRecord

      belongs_to :user
      belongs_to :ticket

     validates :body, presence: true
    end

this is ticket controller

class TicketsController < ApplicationController
  before_action :set_ticket, only: [:show, :edit, :update, :destroy]

  # GET /tickets
  # GET /tickets.json
  def index
    @tickets = Ticket.all
  end

  # GET /tickets/1
  # GET /tickets/1.json
  def show
  end

  # GET /tickets/new
  def new
    @ticket = Ticket.new
  end

  # GET /tickets/1/edit
  def edit
  end

  # POST /tickets
  # POST /tickets.json
  def create
    @ticket = Ticket.new(ticket_params)
    @ticket.user_id = current_user.id
    @ticket.ticketissues.build

    respond_to do |format|
      if @ticket.save
        format.html { redirect_to @ticket, notice: 'Ticket was successfully created.' }
        format.json { render :show, status: :created, location: @ticket }
      else
        format.html { render :new }
        format.json { render json: @ticket.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /tickets/1
  # PATCH/PUT /tickets/1.json
  def update
    respond_to do |format|
      if @ticket.update(ticket_params)
        format.html { redirect_to @ticket, notice: 'Ticket was successfully updated.' }
        format.json { render :show, status: :ok, location: @ticket }
      else
        format.html { render :edit }
        format.json { render json: @ticket.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tickets/1
  # DELETE /tickets/1.json
  def destroy
    @ticket.destroy
    respond_to do |format|
      format.html { redirect_to tickets_url, notice: 'Ticket was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_ticket
      @ticket = Ticket.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def ticket_params
      params.require(:ticket).permit(:subject, :subsubject, :user_id, ticketissues_attributes: [ 
        :body, :id, :_destroy] )
      #params.require(:ticket).permit!
    end
end

而我的看法是这样的

<%= f.input :subject , collection: [ "تغییر اطلاعات کسب و کار",
                                     "تغییر اطلاعات یک کوپن",
                                     "سایر موارد"] %>

    <%= f.input :subsubject %>
   <!-- <%= f.association :user %> -->
  </div>

      <%= f.simple_fields_for :ticketissue do |p| %>
        <%= p.input :body  %>
      <% end %>

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

但是当我想创建票时,表单不会保存到数据库 我得到这个错误:

在 2017-04-11 23:52:33 +0430 开始为 127.0.0.1 发布 POST "/tickets"

Processing by TicketsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"fsl6nTe0PjmBKpeuh16BRFlYOw0MB93LEYDVEAl6TtT/uu/LwGTA0P2q0bRxIxBUqHqZINXHntrZLt7MuCG84Q==", "ticket"=>{"subject"=>"تغییر اطلاعات کسب و کار", "subsubject"=>"lk", "ticketissue"=>{"body"=>"lkjkjkjkjkkjkj"}}, "commit"=>"Create Ticket"}
Unpermitted parameter: ticketissue

但是当我使用控制台和这个命令时:

Ticket.create(subject: 'test' , subsubject: 'ticket test' , ticketissues_attributes: [{body: "[some thing" }] )

一切正常,所有数据都保存。

用于阅读和帮助的容器。

【问题讨论】:

  • 将您的强参数从 ticketissues_attributes: 更改为 ticket_issue:

标签: ruby-on-rails ruby-on-rails-5 nested-form-for ruby-on-rails-5.1


【解决方案1】:

你必须在这里使用复数

= f.simple_fields_for :ticketissues do |p|

【讨论】:

  • 我以前做过,但我不知道为什么当我使用 :ticketissues 全身区域(归档)消失。并仅使用主题和子主题(无正文区域)进行提醒。
  • 这可能是因为@ticket.ticketissuesnil,因此不会出现任何问题。你在newedit 中使用这个吗?
猜你喜欢
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多