将信息从视图发送到控制器 rails
Sending information from view into controller rails
我累了。这是我的事件表单视图:
<%= form_for(@event) do |f| %>
<div class="form-group">
<%= f.radio_button :repeat, 'once', checked: true%><br>
<%= f.label :repeat_once, 'once'%><br>
<%= f.radio_button :repeat, 'daily'%><br>
<%=f.label :repeat_daily, 'daily'%><br>
<%= f.radio_button :repeat, 'weekly'%><br>
<%= f.label :repeat_weekly, 'weekly'%><br>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我在控制器中有方法
def create
@event = Event.new(event_params)
@event.user_id = current_user.id
@event.save
if @event.repeat = 'daily'
////
end
if @event.repeat = 'weekly'
//////////
end
redirect_to events_url
//// it's doesn't matter what inside if block
还有我的迁移:
def change
add_column :events, :repeat , :string
end
问题出在 if 块中,因为无论我在视图中的选择如何,它们每次都为真(例如,我选择每周一次,但无论如何应用程序每天一次)代码有什么问题?感谢阅读 :) 等待任何想法
显然,字符串总是真实的。
你的属性应该是布尔值
您应该在 if
语句中使用双等号 ==
。否则,您将 "daily" 分配给 @event.repeat
,而不是检查它是否已设置为 "daily"。
if @event.repeat == 'daily'
我认为你应该使用
if @event.repeat == 'daily'
////
end
instead of
if @event.repeat = 'daily'
////
end
@event.repeat = 'daily' 正在分配 'daily' 字符串重复不检查它使用 @event.repeat == 'daily' 同样适用于其他 if 条件
我累了。这是我的事件表单视图:
<%= form_for(@event) do |f| %>
<div class="form-group">
<%= f.radio_button :repeat, 'once', checked: true%><br>
<%= f.label :repeat_once, 'once'%><br>
<%= f.radio_button :repeat, 'daily'%><br>
<%=f.label :repeat_daily, 'daily'%><br>
<%= f.radio_button :repeat, 'weekly'%><br>
<%= f.label :repeat_weekly, 'weekly'%><br>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我在控制器中有方法
def create
@event = Event.new(event_params)
@event.user_id = current_user.id
@event.save
if @event.repeat = 'daily'
////
end
if @event.repeat = 'weekly'
//////////
end
redirect_to events_url
//// it's doesn't matter what inside if block
还有我的迁移:
def change
add_column :events, :repeat , :string
end
问题出在 if 块中,因为无论我在视图中的选择如何,它们每次都为真(例如,我选择每周一次,但无论如何应用程序每天一次)代码有什么问题?感谢阅读 :) 等待任何想法
显然,字符串总是真实的。
你的属性应该是布尔值
您应该在 if
语句中使用双等号 ==
。否则,您将 "daily" 分配给 @event.repeat
,而不是检查它是否已设置为 "daily"。
if @event.repeat == 'daily'
我认为你应该使用
if @event.repeat == 'daily'
////
end
instead of
if @event.repeat = 'daily'
////
end
@event.repeat = 'daily' 正在分配 'daily' 字符串重复不检查它使用 @event.repeat == 'daily' 同样适用于其他 if 条件