param丢失或值为空:event

时间:2015-12-04 22:36:22

标签: ruby-on-rails-4

不确定为什么:event param不会拉动当前事件。任何见解将不胜感激!

  

EventsController

class EventsController < ApplicationController
    before_filter :requireLogin

    def requireLogin
        if session[:user_id] == nil
            redirect_to "/"
        end
    end

    def index
       @events = Event.all
    end

   def main
       @event = Event.find(params[:id])
       @current_user ||= User.find(session[:user_id]) if session[:user_id]
       @creator = User.find(@event.createdByID)
    end

   def new
       @event = Event.new
            @current_user ||= User.find(session[:user_id]) if session[:user_id]
       @event.createdByID = @current_user.id
    end

   def coming
       @title = "coming"
       @event = Event.find([:id])
       @events = @event.coming.paginate(page: params[:page])
       render 'show'
    end

    def create
        @event = Event.new(event_params)
        @event.createdByID = Rails.application.config.currUserID
        if @event.save
            redirect_to '/event'
        else
            render 'new'
        end
    end

    def edit
        @event = Event.find(params[:id])
        if @event.update_attributes(event_params)
            redirect_to action: "show"
            # Handle a successful update.
            else
            render 'edit'
        end
    end

    def destroy
        @event = Event.find(params[:id]).destroy
        redirect_to(events_path)
    end

    private
    def event_params
        params.require(:event).permit(:event_name, :place, :time_of)
    end
end
  

修改视图

<!DOCTYPE html>
<div class="edit-event">
  <div class="container">

    <h2>Tell us about your dog!</h2>
    <div class="edit-area">
        <%= form_for(current_user) do |u| %>
        <div class="field">
            Event Name:
            <%= u.text_field :event_name %>
            Event Place:
            <%= u.text_field :place %>
            <%= u.submit "Submit"%>
        </div>
        <% end %>
    </div>

</div>

1 个答案:

答案 0 :(得分:0)

不确定你的意思是什么&#34;拉动当前事件&#34;但您需要在表单中使用@event

<%= form_for(@event) do |u| %>

- 的向上

删除编辑并将其替换为

  def edit
    @event = Event.find(params[:id])
  end


  def update
    @event = Event.find(params[:id])
    respond_to do |format|
      if @event.update(event_params)
        format.html { redirect_to @event }      
      else
        format.html { render :edit }
      end
    end
  end