Rails link_to用于尝试调用搜索操作的创建操作

时间:2015-05-20 15:03:23

标签: ruby-on-rails

我的Rails应用程序中有一个链接,应该将用户添加到旅行中。链接本身是这样的:

<%= link_to "Add to trip", {:url => trip_users_url(@trip.id, {:user_id => user.id}), :remote => true, :method => :post}, :class => 'button absolute' %>

trip_users_controller中的new和create方法如下:

def new
    body_attributes 'add-crew', 'main-feed'

    if params[:trips_user] && !params[:trips_user][:email].blank?
      authorize! :create, @trip.trips_users.new
      @trips_users = @trip.trips_users.find(:first, :include => :user, :conditions => {"users.email" => params[:trips_user][:email]})
      if @trips_users.blank?
        name = params[:trips_user][:email]
        if !User.name_like(name).blank?
          @users = User.name_like(name)
        else
          @users = User.email_like(name)
        end
        @trips_users = []
        @users.each do |user|
          @trips_users << @trip.trips_users.new(:user => user)
        end
        @invitation = @trip.invitations.find_by_email(params[:trips_user][:email])
        @invitation ||= @trip.invitations.new(:email => params[:trips_user][:email]) if params[:trips_user][:email].match(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)
      end
    end
  end

def create
    @trip = Trip.find(params[:trip_id])
    @user = User.find(params[:user_id])
    authorize! :create, TripsUser.new(:user => @user, :trip => @trip)
    unless @trip.users.include? @user
      @trip.users << @user
      add_news(:added_user, @trip, {:user_id => @user.id})
      flash[:notice] = "#{@user.name} was added to the trip."
    end
  end

然而,当我点击链接时,似乎是出于某种原因试图调用搜索操作并给出了这个错误:

NoMethodError in TripsUsersController#search

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

它传递的参数是:

{"method"=>"post",
 "remote"=>"true",
 "url"=>"http://localhost:3000/trips/18/users?user_id=11",
 "trip_id"=>"18-Testing"}

所以它清楚地知道这次旅行,并且需要user_id ......

有什么想法吗?

**编辑**

以下是路线的相关部分:

map.resources :trips, :member => {:share => :get, :map => :get, :favourite => [:post, :get]}, :collection => {:find_boat => :get} do |trips|
    trips.resources :photos
    trips.resources :videos
    trips.resources :users, :controller => "trips_users", :collection => {:search => [:get, :post]}
    trips.resources :paths
    trips.resources :posts
    trips.resources :invitations
  end

0 个答案:

没有答案