我正在尝试从我的帐单显示中创建代理。我无法在表单上显示验证错误,我只能刷新它们。
我试图只用<button onclick="onClick ()">Click Me</button>
...
onClick () {
const kleuren = document.querySelector ( [mySelectorByIDorClass, etc.] );
console.log ( kleuren.checked );
}
来做表格,以渲染而不是重定向,以表格形式传递帐单ID ...但是我不明白。
如何创建代理并返回带有表单中验证错误的票据显示视图?
bills_Controler.rb
@agent
agents_controller.rb
def show
@bill = Bill.find(params[:id])
@agent = @bill.agent || Agent.new
respond_to do |format|
format.html
format.pdf do
render pdf: @bill.to_filename
end
format.zip { send_zip }
end
end
_bill.html.erb
def create
@agent = Agent.new
@bill=Bill.find(params[:bill_id])
if build_agent(agent_params).save
flash[:notice] = t('agent.saved')
redirect_to bill_path (params[:billid])
else
#flash[:error] = @agent.errors
#render 'bills/show'
redirect_to bill_path(@bill), :flash => { :error => @agent.errors.full_messages.join(', ') }
end
end
_agents.html.erb
<%= render 'bills/show/agents'%>
bill.rb
<%= simple_form_for [@bill, @agent] do |f| %>
<%= f.input :name, wrapper_html: { class: 'medium' } %>
<%= f.input :surname, wrapper_html: { class: 'medium' } %>
<%= f.input :phone_number, wrapper_html: { class: 'medium' } %>
<%= f.button :submit, class: 'button btn-main btn btn-primary'%>
<%end%>
agent.rb
belongs_to :agent, inverse_of: :bills
has_many :bills, inverse_of: :agents, dependent: :restrict_with_error
validates :name ,presence: true ....
答案 0 :(得分:0)
为了在字段中显示错误,您需要对包含错误的同一对象进行render
show
操作:
def show
# show action code goes here
end
def create
@agent = Agent.new(agent_params)
@bill = Bill.find(params[:bill_id])
if @agent.save
# validation fails and
# the @agent object contains errors
@bill.agent = @agent
flash[:notice] = t('agent.saved')
redirect_to bill_path(params[:bill_id])
else
render :new
# because the new action contains a form
end
end