无法以铁轨形式添加日期

问题描述 投票:0回答:2

我想以rails形式添加日期。例如

def create
@land=current_user.lands.build(land_params)

if @land.save
  session[:land_id][email protected]
  flash[:success]="Success"
  redirect_to lands_path
else
  flash[:error]="Fail!!"
  render 'new'
end
  end

schema.rb

create_table "lands", force: :cascade do |t|
  t.date "DateStart"
  t.date "DateEnd"
end

new.html.erb

<%= form_for @land do |f| %>
    <p>Information</p>
  <%= f.label :Start %><br />
  <%= f.date_select :DateStart, :default => @DateStart, :order => [:month, :day, :year] %>

  <%= f.label :End %><br />
  <%= f.date_select :DateEnd,:default => @DateEnd,:order => [:month, :day, :year]%>
<% end %>

show.html.erb

<p>Start : <%= @land.DateEnd %></p>
<p>End : <%= @land.DateEnd %></p>

Land.controller

def show
    @land= Land.find(params[:id])
end

但我的show.html.erb中没有打印出任何内容。当我检查数据库时,我的DateStart和DateEnd是零。我不知道有什么不对。你能给我一些建议吗?谢谢

ruby-on-rails ruby-on-rails-5
2个回答
1
投票

您可以按照以下方式操作

def create
    @land= Land.new(land_params)
    @land.user = current_user
    if @land.save
       flash[:success] = 'Land was successfully created.'
       redirect_to lands_path #=> or anything
    else
       flash[:error] = 'Land was not created successfully .'
       redirect_to lands_path #=> or anything
    end
end

我认为这是有用的

您确保在控制器标头上添加此before_action :authenticate_user!,无法在未经身份验证的情况下访问用户

希望能有所帮助


1
投票

你可以按照我的回答 -

方法 - 1.与用户和土地模型无关。

lands_controller.rb文件看起来像 -

class LandsController < ApplicationController

  def new
   @DateStart = Date.today 
   @DateEnd = Date.today + 2.days
   @land = Land.new 
  end

  def create
   @land = Land.new(land_params)    
   if @land.save
    session[:land_id] = @land.id
    flash[:success]= "Success"
    redirect_to lands_path
   else
    flash[:error] = "Fail!!"
    render 'new'
  end
 end

 def show
  @land= Land.find(params[:id])
 end

 def land_params
   params.require(:land).permit(:DateStart, :DateEnd)
 end
end

数据库架构文件看起来像 - (schema.rb)

create_table "lands", force: :cascade do |t|
  t.date "DateStart"
  t.date "DateEnd"
end

Lands controller新动作视图文件 - (app / views / lands / new.html.erb)

 <%= form_for @land do |f| %>
  <p>Information</p>
  <%= f.label :Start %><br />
  <%= f.date_select :DateStart, :default => @DateStart, :order => [:month,   :day, :year] %>

  <%= f.label :End %><br />
  <%= f.date_select :DateEnd,:default => @DateEnd,:order => [:month, :day, :year]%>
  <%= f.submit "Submit" %>
<% end %>

Lands查看页面(app / views / lands / show.html.erb)

<p>Start : <%= @land.DateEnd %></p>
<p>End : <%= @land.DateEnd %></p>

方法 - 2.与用户和土地模型的关联。

lands_controller.rb文件看起来像 - (app / controllers / lands_controller.rb)

class LandsController < ApplicationController
 before_action :authenticate_user!

 def new    
   @DateStart = Date.today 
   @DateEnd = Date.today + 2.days
   @land = current_user.lands.build 
 end

 def create
  @land = current_user.lands.build(land_params)    
  if @land.save
    session[:land_id] = @land.id
    flash[:success]= "Success"
    redirect_to lands_path
  else
   flash[:error] = "Fail!!"
   render 'new'
  end
 end

 def show
  @land= Land.find(params[:id])
 end

 def land_params
   params.require(:land).permit(:DateStart, :DateEnd)
 end
end

数据库架构文件看起来像 - (schema.rb)

create_table "lands", force: :cascade do |t|
 t.date "DateStart"
 t.date "DateEnd"
 t.index ["user_id"], name: "index_rooms_on_user_id"
end
create_table "users", force: :cascade do |t|
 t.string   "email",                  default: "", null: false
 t.string   "encrypted_password",     default: "", null: false
 t.string   "reset_password_token"
 t.datetime "reset_password_sent_at"
 t.datetime "remember_created_at"
 t.integer  "sign_in_count",          default: 0,  null: false
 t.datetime "current_sign_in_at"
 t.datetime "last_sign_in_at"
 t.string   "current_sign_in_ip"
 t.string   "last_sign_in_ip"
 t.datetime "created_at",                          null: false
 t.datetime "updated_at",                          null: false    
 t.index ["email"], name: "index_users_on_email", unique: true
 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

用户模型看起来像 - (app / models / user.rb)

class User < ApplicationRecord
  has_many :lands
end

土地模型看起来像 - (app / models / land.rb)

class Land < ApplicationRecord
  belongs_to :user
end

Lands controller新动作视图文件 - (app / views / lands / new.html.erb)

<%= form_for @land do |f| %>
  <p>Information</p>
  <%= f.label :Start %><br />
  <%= f.date_select :DateStart, :default => @DateStart, :order => [:month, :day, :year] %>

  <%= f.label :End %><br />
  <%= f.date_select :DateEnd,:default => @DateEnd,:order => [:month, :day, :year]%>
   <%= f.submit "Submit" %>
 <% end %>

Lands查看页面(app / views / lands / show.html.erb)

<p>Start : <%= @land.DateEnd %></p>
<p>End : <%= @land.DateEnd %></p>

我希望它应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.