form_for更新方法

时间:2013-02-05 14:10:59

标签: ruby-on-rails-3 form-for

在页面上我列出了任务。我想将该任务的表单作为更新来完成该任务。我有以下代码作为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

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

我缺少的是我的表格处于循环中。因此,当我调用form_for @event时,它无法看到该循环之外。我有函数接受事件变量。

<% @events.each do |event| %>
  ......
  ......
  <% form_for event do |f| %>
     .....
  <% end %>