重定向控制器Rails 4的新操作

时间:2015-01-25 17:37:07

标签: ruby-on-rails ruby-on-rails-4 ransack

我在为事件控制器执行新操作和显示操作时遇到问题。目前,我只能访问列出事件的索引。但是,当我尝试创建新事件甚至显示current_users事件时,我得到302重定向。你能帮我修复这个问题,让current_biz_user看到他或她创建的事件,同时还允许他们将新事件发布到索引上以供公众收视吗?谢谢。

这也是我的路线。

events GET    /events(.:format)                            events#index
                          POST   /events(.:format)                            events#create
                new_event GET    /events/new(.:format)                        events#new
                    event GET    /events/:id(.:format)                        events#show
                          PATCH  /events/:id(.:format)                        events#update
                          PUT    /events/:id(.:format)                        events#update



class EventsController < ApplicationController
      before_filter :set_search
      before_action :set_event, only: [:show, :update, :create, :new]
      respond_to :html, :json

      def index
        @events = Event.all.order("created_at DESC")
        @search = Event.search(params[:q])
        @events = @search.result
        @search.build_condition if @search.conditions.empty?
        @search.build_sort if @search.sorts.empty?
        respond_with(@events)
      end

      def show
        @event = Event.friendly.find(params[:id])
        @eventcreator = BizUser.find(@event.biz_user_id)
        respond_with(@event)
      end

      def new
        @event = Event.new
      end

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

      def create
        @event = Event.new(event_params)
        @event.biz_user = current_user

        respond_to do |format|
          if @event.save
            format.html { redirect_to @event, notice: 'Event was successfully created.' }
            format.json { render :show, status: :created, location: @event }
          else
            format.html { render :new }
            format.json { render json: @event.errors, status: :unprocessable_entity }
          end
        end
      end

      def update
        respond_to do |format|
          if @event.update(event_params)
            format.html { redirect_to @event, notice: 'Event was successfully updated.' }
            format.json { render :show, status: :ok, location: @event }
          else
            format.html { render :edit }
            format.json { render json: @event.errors, status: :unprocessable_entity }
          end
        end
      end

      private
        def set_event
          @event = Event.find(params[:id])

        end

        def set_search
          @q = Event.search(params[:q])
        end

        def event_params
          params.require(:event).permit(:title, :description, :city, :state, :from_time, :to_time, :date, :eventlogo, :manager, :managerdescription, :reservation, :eventtype, :theme, :capacity, :passname, :private, :alcohol, :ticketquantity, :options, :price, :category, :biz_user_id)
        end
    end

1 个答案:

答案 0 :(得分:0)

变化:

 before_action :set_event, only: [:show, :update, :create, :new]

对此:

  before_action :set_event, only: [:show, :update]

另外,请检查您的before_actions。我认为您不需要在所有控制器操作上应用set_search