ruby on rails 3 - form_for update method -
on page have task listed out. want set form task update finish task. have next code form_for:
<%= form_for :event, :action => :update, :id => event.id |f| %> <%= f.check_box :complete %> <%= f.submit 'complete %> <% end %>
with code want edit event id in url , finish event. creates new event instead. ideas?
event controller:
class eventscontroller < applicationcontroller layout 'events', :except => 'edit' # /events # /events.json def index @events = event.order("events.initials asc, events.priroty asc") @archived = completeevent.order("complete_events.created_at asc") respond_to |format| format.html # index.html.erb format.json { render json: @events } end end # /events/1 # /events/1.json def show @event = event.find(params[:id]) respond_to |format| format.html # show.html.erb format.json { render json: @event } end end # /events/new # /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 |format| format.html # new.html.erb format.json { render json: @event } end end # /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 |format| if @event.save user = @event.name notifier.task_created(user).deliver format.html { redirect_to @event, notice: 'event 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 # set /events/1 # set /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 |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 |format| format.html { redirect_to events_url } format.json { head :no_content } end end end
in edit.html.erb (or partial rendered in edit.html.erb) use:
<%= form_for @event |f| %>
you not need specify action, since inferred uri path, , rails knows id model object @event.id.
ruby-on-rails-3 form-for
No comments:
Post a Comment