如何使用改革预先填充特色对象? [英] How to use Reform to prepopulate for featured objects?

查看:198
本文介绍了如何使用改革预先填充特色对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,当用户点击拍照时,我们如何使用专门为特色挑战设置的属性预填充create.html.erb表单,例如 12 天,然后做周二,周四



我使用

  #challenges_controller 
def创建
@challenge = Challenge.new(challenge_params)#create用于GET,然后POST
if params [:step] =='2'
@challenge = current_user.challenges.build(challenge_params)
@ challenge.save
redirect_to @challenge
end
end

#challenge / create.html.erb
<%= simple_form_for(@challenge)do | f | %GT;
<%= hidden_​​field_tag:step,2%>
挑战:<%= f.text_field:action%>
Do For:<%= f.number_field:days_challenged,value:10%>
执行时间:<%= f.collection_check_boxes:committed%>
<%end%>

  class ChallengeForm< Reform :: Form 
property:action
property:committed
property:days_challenged

model:challenge

def commited
super || action_to_commited_hash [model.action]
结束

def days_challenged
super || action_to_days_challenged_hash [model.action]
end

def action_to_days_challenged_hash
{
'Run a Mile'=> 30,
'拍照'=> 12
}
结束

def action_to_commited_hash
{
'跑一英里'=> ['Mon','Wed','Fri'],
'拍照'=> ['Tue','Thu']
}
结束
结束

因为现在 ChallengeForm 对create.html.erb没有任何影响。我们如何才能正确地将默认值插入到create.html.erb中,具体取决于:action ?

解决方案

试试这个:
$ b

challenges_controller

  def new 
@form = ChallengeForm.new(Challenge.new)

respond_modal_with @form,location:root_path
end

def创建
挑战= Challenge.new(challenge_params)
@form = ChallengeForm.new(挑战)

如果params [:step] ==' 2'
@form.validate(user_id:current_user.id)
@form.save
redirect_to挑战
结束
结束

挑战/ new.html.erb

 <%= simple_form_for @form,html:{data:{modal:true}},url:'your_challenge_create_path',方法::post do | f | %GT; 

<%= f.text_field:action,placeholder:'输入自定义挑战'%>< br>
或选择特色挑战:
<%= f.collection_radio_buttons:动作,[['跑一英里','跑一英里],['喝16oz水','饮料16oz水'','拍照','拍照'],['最大饮料1','最大饮料1'],['见艾菲尔铁塔','见艾菲尔铁塔'],['写书籍','写书'],['Skydive','Skydive'],['开办企业','创业'],['没有贪睡','没有贪睡',''访问所有50个国家','访问所有50个国家'],['与陌生人交谈','与陌生人交谈'],['尝试新食谱','尝试新食谱'],['快速媒体','Media-fast']],:first,:last%>
<%= f.submit%>
<%end%>

挑战/ create.html.erb

 <%= simple_form_for @form,html:{data:{modal:true}},url:'your_challenge_create_path',方法::post do | f | %GT; 
<%= hidden_​​field_tag:step,2%>
挑战:<%= f.text_field:action%>
对于:<%= f.number_field:days_challenged%>
执行时间:<%= f.collection_check_boxes:committed%>
<%end%>

我可能有点偏离,但您明白了吗?


When a user clicks, for example, "Take a Picture" how can we prepopulate the create.html.erb form with attributes specifically set for that featured challenge, like do for 12 days and do on Tue, Thu?

I am using the reform gem.

#challenges_controller
def new
  @challenge = Challenge.new
  respond_modal_with @challenge, location: root_path
end

#challenges/new.html.erb
<%= simple_form_for(@challenge, html: { data: { modal: true } })  do |f| %>
  <%= f.text_field :action, placeholder: 'Enter a Custom Challenge' %><br>
    Or choose a featured challenge:
  <%= f.collection_radio_buttons :action, [['Run a Mile','Run a Mile'], ['Drink 16oz of Water','Drink 16oz of Water'], ['Take a Picture','Take a Picture'], ['1 Drink Max','1 Drink Max'], ['See Eiffel Tower','See Eiffel Tower'], ['Write a Book','Write a Book'], ['Skydive','Skydive'], ['Start a Business','Start a Business'], ['No Snooze','No Snooze'], ['Visit All 50 States','Visit All 50 States'], ['Talk to a Stranger','Talk to a Stranger'], ['Try a New Recipe','Try a New Recipe'], ['Media-fast','Media-fast']], :first, :last %>
  <%= f.submit %>
<% end %>

#challenges_controller
def create
  @challenge = Challenge.new(challenge_params) #create is being used to GET and then POST
  if params[:step] == '2'
    @challenge = current_user.challenges.build(challenge_params)
    @challenge.save
    redirect_to @challenge
  end
end

#challenges/create.html.erb
<%= simple_form_for(@challenge)  do |f| %>
<%= hidden_field_tag :step, 2 %>
  Challenge: <%= f.text_field :action %>
  Do For: <%= f.number_field :days_challenged, value: 10 %>
  Do On: <%= f.collection_check_boxes :committed %>
<% end %>

class ChallengeForm < Reform::Form
  property :action
  property :committed
  property :days_challenged

  model :challenge

  def commited
    super || action_to_commited_hash[model.action]
  end

  def days_challenged
    super || action_to_days_challenged_hash[model.action]
  end

  def action_to_days_challenged_hash
    {
      'Run a Mile' => 30,
      'Take a Picture' => 12
    }
  end

  def action_to_commited_hash
    {
      'Run a Mile' => ['Mon', 'Wed', 'Fri'],
      'Take a Picture' => ['Tue', 'Thu']
    }
  end
end

As it is now ChallengeForm has had no effect on create.html.erb. How can we get it to properly insert the defaults into create.html.erb depending on the featured :action?

解决方案

Try this:

challenges_controller

  def new
    @form = ChallengeForm.new(Challenge.new)

    respond_modal_with @form, location: root_path
  end

  def create
    challenge = Challenge.new(challenge_params)
    @form = ChallengeForm.new(challenge)

    if params[:step] == '2'
      @form.validate(user_id: current_user.id)
      @form.save
      redirect_to challenge
    end
  end

challenges/new.html.erb

<%= simple_form_for @form, html: { data: { modal: true } }, url: 'your_challenge_create_path', method: :post do |f| %>

  <%= f.text_field :action, placeholder: 'Enter a Custom Challenge' %><br>
  Or choose a featured challenge:
  <%= f.collection_radio_buttons :action, [['Run a Mile','Run a Mile'], ['Drink 16oz of Water','Drink 16oz of Water'], ['Take a Picture','Take a Picture'], ['1 Drink Max','1 Drink Max'], ['See Eiffel Tower','See Eiffel Tower'], ['Write a Book','Write a Book'], ['Skydive','Skydive'], ['Start a Business','Start a Business'], ['No Snooze','No Snooze'], ['Visit All 50 States','Visit All 50 States'], ['Talk to a Stranger','Talk to a Stranger'], ['Try a New Recipe','Try a New Recipe'], ['Media-fast','Media-fast']], :first, :last %>
  <%= f.submit %>
<% end %>

challenges/create.html.erb

<%= simple_form_for @form, html: { data: { modal: true } }, url: 'your_challenge_create_path', method: :post do |f| %>
  <%= hidden_field_tag :step, 2 %>
  Challenge: <%= f.text_field :action %>
  Do For: <%= f.number_field :days_challenged %>
  Do On: <%= f.collection_check_boxes :committed %>
<% end %>

I might be a bit off, but you get the point?

这篇关于如何使用改革预先填充特色对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆