开发者

How do I correctly use the date_select model object helper? (Rails)

开发者 https://www.devze.com 2023-04-04 16:10 出处:网络
I\'m trying to design a form that lets the user set the date at which they will go shopping. This form will create a \"List\" model and define the \"date\" attribute. I created this list model with:

I'm trying to design a form that lets the user set the date at which they will go shopping. This form will create a "List" model and define the "date" attribute. I created this list model with:

$ rails generate model List name:string date:date user_id:integer

Right now I only want my form to set the date. Here's my code:

<%= form_for @list do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.date_select :date %>
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

But when I submit the form in my browser, the page tells me that the date field is still blank, even though I submitted the form with a date selected (I've set my list object to validate the presence of the "date" attribute). Why does the form fail to successfully submit the date and create the list? Thanks!

1 error prohibited this list from being saved:

There were problems with the following fields:

    * Date can't be blank

Here's 开发者_Go百科my controller code:

ListsController < ApplicationController
  before_filter :authenticate, :only => [:create, :destroy]

  def create
    @list  = current_user.lists.build(params[:list])
    if @list.save
      flash[:success] = "Shopping List created!"
      redirect_to root_path
    else
      render 'pages/home'
    end
  end


I would suggest 2 things:

1) Don't call the field 'date'. It's a reserved word, and may give conflicts.

2) Use a plugin like calendar_date_select ( https://github.com/timcharper/calendar_date_select ). Your users will love it.

0

精彩评论

暂无评论...
验证码 换一张
取 消