4

例如,当用户单击“拍照”时,我们如何使用专门为该特色挑战设置的属性预先填充 create.html.erb 表单,例如 do for 12days 和 do on Tue, Thu

我正在使用reform宝石

#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

因为它现在ChallengeForm对 create.html.erb 没有影响。我们怎样才能让它根据 feature 正确地将默认值插入 create.html.erb :action

4

2 回答 2

2

尝试这个:

挑战控制器

  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

挑战/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 %>

挑战/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 %>

我可能有点跑题了,但你明白了吗?

于 2016-04-21T05:23:00.007 回答
0

您将@challenge 传递给表单,但您可能希望传递表单对象。

为此,在控制器中添加一些代码:

@challenge_form = ChallengeForm.new(@challenge)

然后在视图中:

<%= simple_form_for(@challenge_form, html: { data: { modal: true } })  do |f| %>
于 2016-04-16T21:09:54.957 回答