我有用户,每个用户都设定了一个目标。因此,一个目标属于一个用户,一个用户拥有一个:目标。我试图在视图中使用 form_for 来允许用户设置他们的目标。
我想这就像微博(直接来自 Hartl 的教程),只是单数而不是复数。我已经查看了有关此问题的教程和问题,并尝试了许多不同的方法,但我无法使其正常工作。
我得到了 form_for 实际工作并且显然指向了正确的路线,但是我收到了以下错误,我不知道这意味着什么:
“减肥”的未定义方法“stringify_keys”:字符串
目标控制器
class GoalsController < ApplicationController
before_filter :signed_in_user
def create
@goal = current_user.build_goal(params[:goal])
if @goal.save
redirect_to @user
end
end
def destroy
@goal.destroy
end
def new
Goal.new
end
end
目标模型
class Goal < ActiveRecord::Base
attr_accessible :goal, :pounds, :distance, :lift_weight
belongs_to :user
validates :user_id, presence: true
end
用户模型
class User < ActiveRecord::Base
has_one :goal, :class_name => "Goal"
end
_goal_form(这是用户#show 上的一个模态)
<div class="modal hide fade in" id="goal" >
<%= form_for(@goal, :url => goal_path) do |f| %>
<div class="modal-header">
<%= render 'shared/error_messages', object: f.object %>
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>What's Your Health Goal This Month?</h3>
</div>
<div class="modal-body">
<center>I want to <%= select(:goal, ['Lose Weight'], ['Exercise More'], ['Eat Better']) %> </center>
</div>
<div class="modal-body">
<center>I will lose <%= select_tag(:pounds, options_for_select([['1', 1], ['2', 1], ['3', 1], ['4', 1], ['5', 1], ['6', 1], ['7', 1], ['8', 1], ['9', 1], ['10', 1]])) %> lbs. this month!</center>
</div>
<div class="modal-footer" align="center">
<a href="#" class="btn" data-dismiss="modal">Close</a>
<%= f.submit "Set Goal", :class => "btn btn-primary" %>
</div>
<% end %>
</div>
路由.rb
resource :goal, only: [:create, :destroy, :new]