试图在Rails上用ruby编辑文章

时间:2019-03-21 04:55:32

标签: html ruby-on-rails ruby

我对红宝石一无所知,并且认为在修改数据库中名为“约会”的文章的值时,我只是缺少了一些东西。我将列出相关的代码片段。

我有一个管理页面,该页面列出了每个用户的预定约会,每个链接旁边都有一个编辑链接,可将我带到正确的约会/ id#/编辑页面。视图中的管理页面:

<div align="center">
    <div class="col-md-6">
      <div class="card">
        <h5 class="card-header">All Scheduled Appointments</h5>
        <div class="card-body">
          <%@appointments.each do | appt |%>
            <%="Appointment for #{appt.owner_email} on #{appt.date} in the #{appt.time}"%> <%= link_to 'Edit', edit_appointment_path(appt) %>
            </br>
          <%end%>
        </div>
      </div>
    </div>
  </div>

约会_controller.rb文件:

class AppointmentsController < ApplicationController
    def edit
        @appointment = Appointment.find(params[:id])
    end
end

编辑页面仅包含管理员使用提交按钮选择所需日期/时间的选项。我希望管理员选择这些值以更新用户先前将其设置为的值。点击提交只会刷新页面,但似乎无济于事。视图中的edit.html.erb文件:

<h1 align="center">Edit Existing Appointment</h1>

<form action="edit" method="refresh">
<div class="container-fluid">
  <div class="row">
    <div class="col-md-2">
      <div class="form-group">
        <label for="desiredDate">Desired Date</label>
        <input type="date" class="form-control" id="desiredDate" aria-describedby="desiredDate" placeholder="" name="desiredDate">
      </div>
    </div>
    <div class="col-md-3">
      <div class="form-group">
        <label for="desiredTime">Desired Time</label>
        <select class="form-control" id="desiredTime" name="desiredTime">
          <option>Morning</option>
          <option>Afternoon</option>
          <option>Evening</option>
        </select>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-4 offset-md-4 text-center">
       <%= submit_tag "Submit", class:"btn btn-primary"%>
    </div>
  </div>
</form>

我的routes.rb代码是否有帮助:

Rails.application.routes.draw do
  devise_for :users
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'welcome#index'

  get '/services', to: 'welcome#services'
  get '/about_us', to: 'welcome#about_us'
  get '/schedule', to: 'welcome#schedule'
  get '/new_car', to: 'welcome#new_car'
  get '/my_profile', to: 'welcome#my_profile'
  get '/admin', to: 'welcome#admin'

  devise_scope :user do
    get '/sign_up', to: 'devise/registrations#new'
    get '/login', to: 'devise/sessions#new'
    get '/logout', to: 'devise/sessions#destroy'
  end

  get '/appointments/edit', to: 'appointments#edit'
  resources :cars
  resources :appointments
end

任何建议将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试解决此问题:<form action="edit" method="refresh">。将操作更改为"<%=appointment_path(params[:id])%>",将方法更改为“发布”。

答案 1 :(得分:0)

那么首先要做的是:为什么不使用Rails表单助手?您将使代码更整洁并且更易于调试。

也就是说,您的表单需要指向正确的控制器方法(在您的情况下为update)。无论如何,我会尝试这样的事情:

<div class="container-fluid">
  <%= form_for @appointment do |f| %>
    <div class="row">
      <div class="col-md-2">
        <div class="form-group">
          <%= f.label :desiredDate %>
          <%= f.date_field :desiredDate %>
        </div>
      </div>
      <div class="col-md-3">
        <div class="form-group">
          <%= f.label :desiredTime %>
          <%= f.select :desiredTime, ["Morning", "Afternoon", "Evening"], {}, class: "form-control"
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-md-4 offset-md-4 text-center">
         <%= f.submit "Submit", class:"btn btn-primary"%>
      </div>
    </div>
  </form>
</div>

我已将容器移到了表单之外,因为它应该是您所做的编辑页面的包装。