2017-07-16 68 views
-1

我是一個Ruby on Rails編碼的新手,我試圖從視圖中獲取一些信息並更新視圖。但我遇到過這個問題。form_tag生成不同的路徑

首先,我在我的應用程序中有組註釋,用戶和訂閱模型。組注與用戶之間存在多對多關係,因此我使用訂閱來保留group_notes_id和user_id。現在在我的group_notes表明觀點,我有這樣的代碼:

<%= form_tag addUserToGroupNotes_group_notes_path do %> 
    <%= text_field_tag :eMail %> 
    <%= submit_tag "Add User to group note" %> 
<% end %> 

在我的路線文件,我有這樣的:

Rails.application.routes.draw do 

    resources :group_notes 

    resources :group_notes do 
    post :addUserToGroupNotes, :on=>:collection 
    end 

    resources :items 

    resources :personal_notes do 
     member do 
     patch :complete 
     end 
     resources :items 
    end 

    devise_for :users 

    resources :items 
    root 'personal_notes#index' 

end 

我希望我的表單標籤調用addUserToGroupNotes方法在我的組注意控制器。我創建了一個路由路徑,但是當我點擊提交按鈕,它是這樣的:?

  • group_notes/2 UTF8 =✓& authenticity_token = 9zN5NHaPEtXAREwkhQY2iHrNJ%2FA

,而不是這樣的:

  • group_notes/addUserToGroupNotes

我Group_Notes控制器代碼

class GroupNotesController < ApplicationController 
    before_action :find_group_note, only: [:show,:edit,:update,:destroy] 

    def index 
     if user_signed_in? 
      @group_notes = current_user.group_notes.order("created_at DESC") 
     end 
    end 


    def addUserToGroupNotes 
     eMail = params['eMail'] 
     user = User.where(:email => eMail).take 
     @groupNoteID = session[:currentGroupNote] 
     record = Subscription.where(:user_id => user.id, :group_note_id => @groupNoteID).take 

     if user.nil? || !record.nil? 
      redirect_to group_notes_path 
     else 
      @newSubscription = Subscription.new(:user_id => user.id, :group_note_id => @groupNoteID) 
      @newSubscription.save 
      redirect_to group_note_path(@groupNoteID) 
     end 
    end 


    def show 
     @currentGroupNote = params[:id] 
     session[:currentGroupNote] = @currentGroupNote 
     currentSubscription = Subscription.where(:group_note_id => @currentGroupNote) 
     @users = [] 

     currentSubscription.each do |subscription| 
      user = User.where(:id => subscription.user_id) 
      @users += user if user 
     end 

     @items = GroupItem.where(:group_note_id => @currentGroupNote) 
    end 

    def addItem 
     item = params['item'] 
     @groupNoteID = session[:currentGroupNote] 
     @newItem = Item.new(:description => item, :group_note_id => @groupNoteID) 
     @newItem.save 
     redirect_to group_note_path(@groupNoteID) 
    end 

    def new 
     @group_note = current_user.group_notes.build 
    end 

    def create 
     @group_note = current_user.group_notes.build(group_note_params) 
     user = User.find(current_user) 
     groupNote = @group_note 
     groupNote.users << user #Adding user to group 

     if @group_note.save 
      redirect_to root_path 
     else 
      render 'new' 
     end 
    end 

    def edit 
    end 

    def update 
     if @group_note.update(group_note_params) 
      redirect_to group_note_path(@group_note) 
     else 
      render 'edit' 
     end 
    end 

    def destroy 
     @group_note.destroy 
     redirect_to root_path 
    end 


    private 
     def group_note_params 
      params.require(:group_note).permit(:title, :description, :noteType) 
     end 

     def find_group_note 
      @group_note = GroupNote.find(params[:id]) 
     end 
end 

那麼背後有什麼問題呢?感謝您的答案。

+0

您使用的是哪種版本的導軌?我只是將你的代碼複製並粘貼到我的應用程序,它工作得很好。請提供整個routes.rb文件 – Ptr

+0

我的Rails版本是:5.0.4,還用完整的路由文件代碼編輯了我的問題。我使用設計和我的用戶模型創建與設計安裝。這可能是問題嗎? –

+0

奇怪。它應該工作得很好。特別奇怪的是這個** 2 **在'group_notes/2'中。它看起來像你有某種方式重寫這個方法什麼的。生成的html中是否有'method ='post''?我想我還需要你的控制器代碼,或者如果你有我的代碼在github或bitbucket上的公共回購只是給我鏈接,我會看看它。 – Ptr

回答

0

很多事情是格格不入:

  1. 指定請求方法post

    form_tag addUserToGroupNotes_group_notes_path, method: :post ....

  2. 修復路由名稱使用Rails的慣例:

    post :add_user_to_notes, :on=>:collection

  3. 解決您的控制器方法名稱:

    def add_user_to_notes end

這應該把你在正確的軌道上。