0

在一个页面上,我列出了任务。我想为该任务添加一个表格作为完成该任务的更新。我有以下代码作为form_for:

<%= form_for :event, :action => :update, :id => event.id do |f| %>
  <%= f.check_box :complete %>
  <%= f.submit 'Complete %>
<% end %>

使用此代码,我希望它使用 url 中的 id 编辑事件并完成事件。它的作用是创建一个新事件。有任何想法吗?

事件控制器:

    class EventsController < ApplicationController
  layout 'events', :except => 'edit'
  # GET /events
  # GET /events.json
  def index
    @events = Event.order("events.initials ASC, events.priroty ASC")
    @archived = CompleteEvent.order("complete_events.created_at ASC")


    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @events }
    end
  end

  # GET /events/1
  # GET /events/1.json
  def show
    @event = Event.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @event }
    end
  end

  # GET /events/new
  # GET /events/new.json
  def new
    @event = Event.new
    @users = ['BG', 'BD', 'MB', 'AF', 'RA', 'JM']
    @name = ['Brad Garrison', 'Brian Davis', 'Matt Broach', 'Alan McFarland', 'Russell Anderson', 'Jason Milam']

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @event }
    end
  end

  # GET /events/1/edit
  def edit
    @event = Event.find(params[:id])
    @users = ['BG', 'BD', 'MB', 'AF', 'RA', 'JM']
    @name = ['Brad Garrison', 'Brian Davis', 'Matt Broach', 'Alan McFarland', 'Russell Anderson', 'Jason Milam']

  end

  # POST /events
  # POST /events.json
  def create
    @event = Event.new(params[:event])

    respond_to do |format|
      if @event.save
        user = @event.name 
        Notifier.task_created(user).deliver
        format.html { redirect_to @event, notice: 'Event was successfully created.' }
        format.json { render json: @event, status: :created, location: @event }
      else
        format.html { render action: "new" }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /events/1
  # PUT /events/1.json
  def update
    @event = Event.find(params[:id])
    @save = @event.update_attributes(params[:event])
      @complete = CompleteEvent.new(:initials => @event.initials, :name => @event.name, :event_name => @event.event_name, :complete => @event.complete, :event_description => @event.event_description, :comment => @event.comment)

    respond_to do |format|
      if @event.update_attributes(params[:event])
        if @event.complete == true
          user = @event.name
          @save
              @complete
              @complete.save
              @event.destroy
          format.html { redirect_to :controller => :events, :action => :index}
          format.json { head :no_content }
            elsif @event.complete == false          
          Notifier.task_updated(user).deliver
          format.html { redirect_to :controller => :events, :action => :index}
          format.json { head :no_content }
            end
      end
    end
  end

  # DELETE /events/1
  # DELETE /events/1.json
  def destroy
    @event = Event.find(params[:id])
    @event.destroy

    respond_to do |format|
      format.html { redirect_to events_url }
      format.json { head :no_content }
    end
  end


end
4

2 回答 2

0

我缺少的是我的表单处于循环状态。因此,当我调用 form_for @event 时,它在该循环之外看不到。我让函数接受事件变量。

<% @events.each do |event| %>
  ......
  ......
  <% form_for event do |f| %>
     .....
  <% end %>
于 2013-02-10T01:59:49.973 回答
0

在edit.html.erb(或在edit.html.erb中呈现的部分)使用:

<%= form_for @event do |f| %>

通常不需要指定动作,因为它是从 URI 路径推断出来的,而且 Rails 知道模型对象的 id 是 @event.id。

于 2013-02-06T14:28:00.440 回答