[英]has_one, belongs_to What am I doing wrong?
I have Users and each user sets a Goal. 我有用户和每个用户设置目标。 So a goal belongs_to a user and a user has_one: goal.
因此,目标属于用户和用户has_one:目标。 I am trying to use form_for in the view to allow the user to set their goal up.
我试图在视图中使用form_for以允许用户设置他们的目标。
I figured it would be like microposts (which is straight from Hartl's tutorial) just with singular instead of plural. 我认为它就像微博(这是直接来自Hartl的教程),只是用单数而不是复数。 I've looked at tutorials and questions on here for this issue and tried numerous different things and I just can't get it working.
我已经在这里查看了关于这个问题的教程和问题,并尝试了许多不同的东西,但我无法让它工作。
I got the form_for actually working and apparently pointing to the correct route, but I'm getting the below error and I have no clue what it means: 我得到了form_for实际工作,显然指向正确的路线,但我得到以下错误,我不知道这意味着什么:
undefined method `stringify_keys' for "Lose Weight":String 未定义方法`stringify_keys'表示“减肥”:字符串
GoalsController GoalsController
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
Goal Model 目标模型
class Goal < ActiveRecord::Base
attr_accessible :goal, :pounds, :distance, :lift_weight
belongs_to :user
validates :user_id, presence: true
end
User Model 用户模型
class User < ActiveRecord::Base
has_one :goal, :class_name => "Goal"
end
_goal_form (which is a modal on the Users#show) _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>
Routes.rb 的routes.rb
resource :goal, only: [:create, :destroy, :new]
try this 尝试这个
# in your user model
accepts_nested_attributes_for :goal
write above code in your user model, and 在您的用户模型中写上面的代码,和
for select tag try to use from this link 对于select标签,请尝试使用此链接
http://shiningthrough.co.uk/Select-helper-methods-in-Ruby-on-Rails
I expect the stringify_keys
error is down to the way you've listed your options in the goal field, they need to be grouped up to be treated as one argument. 我希望
stringify_keys
错误与你在目标字段中列出选项的方式有关,它们需要被分组以作为一个参数。
Along with using accepts_nested_attributes_for :goal
as suggested by Dipak, you will want to have a nested form. 除了使用Dipak建议的
accepts_nested_attributes_for :goal
,您还需要一个嵌套表单。
form_for @user do |form|
fields_for @goal do |fields|
fields.select :goal, ['Lose Weight', 'Exercise More', 'Eat Better']
The save action is now in the context of a user, so the attributes that come through will include a portion for the goal fields: 保存操作现在位于用户的上下文中,因此通过的属性将包括目标字段的一部分:
user =>{goal_attributes => {:goal => 'Eat Better'}}
You can save these attributes by updating the user: 您可以通过更新用户来保存这些属性:
@user.update_attributes(params[:user])
As an aside, your "new" action should be @goal = Goal.new
, just creating a new goal does nothing, you need to assign it to a variable for it to make it to the page. @goal = Goal.new
,你的“新”动作应该是@goal = Goal.new
,只是创建一个新的目标什么都不做,你需要将它分配给一个变量,让它进入页面。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.