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

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

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

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

我缺少的是我的表单处于循环中。因此,当我调用form_for@事件时,它看不到循环之外的内容。我让函数取事件的变量。

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

相关内容

  • 没有找到相关文章

最新更新