2014-09-28 102 views
1

我無法解決此問題。 當我嘗試使用「聊天室#新」方法,我得到這個錯誤,ActionController :: ParameterMissing
param丟失或值爲空ActionController :: ParameterMissing param丟失或值爲空

以下代碼是ChatroomController。

class ChatroomsController < ApplicationController  
    before_action :find_room_owner,only:[:index] 
    before_action :objects_for_index,only:[:index] 
     def index 
     #/users/:user_id/cart/items/chatroom 
     sign_in @user if signed_in? 
     if @seller && @buyer 
      flash[:success] = "U are owners of the chatroom" 
      @messages = Message.all #Messageのmodelを作成 
     else 
      flash[:error] = "U cant enter the chatroom." 
      redirect_to user_cart_items_url(@user,@cart,@items) #@user,@cart,@itemsをgetしろ 
     end 
     end 

     def new 
     @user = User.find(params[:user_id]) 
     @cart = Cart.find(params[:user_id]) 
     @item = Item.find(params[:item_id]) 
     @message = Message.new(message_params) 
     end 

     def create 
     @user = User.find(params[:user_id]) 
     @message = @user.messages.build 
     if @message.save 
      @message.update_attributes(user_id:@user.id) 
      redirect_to user_cart_chatroom_path(@user,@cart,@items) 
     else 
      flash[:error] = "could not create any message." 
      render 'new' 
     end 
     end 

    private 

     def message_params 
     params.require(:messages).permit(:id,:user_id,:messages) 
     #params{:message => :id,:user_id,:messages} 
     end 


    #before_aciton 
     def find_room_owner 
     @item = Item.find(params[:item_id]) 
     @buyer = User.find(params[:user_id]) 
     @seller = Product.find_by(user_id:@item.user_id) 
     end 

     def objects_for_index 
     @user = User.find(params[:user_id]) 
     @items = Item.all  
     end 

    end 

下面的代碼是Message#new的視圖。

<h1>Chatroom#new</h1> 
<p>Find me in app/views/chatroom/new.html.erb</p> 


<%= form_for @message,url:new_user_cart_item_chatroom_path(@user,@cart,@item) do |f| %> 
    <%= f.label :messages %> 
    <%= f.text_field :messages %> 

    <%= f.submit "new message", class:"btn btn-default" %> 
<% end %> 

下面的代碼是Message的遷移。

class CreateMessages < ActiveRecord::Migration 
    def change 
    create_table :messages do |t| 
     t.integer :user_id 
     t.string :messages 

     t.timestamps 
    end 
    end 
end 

下面的代碼是消息的模型。

class Message < ActiveRecord::Base 

    validates :messages,presence:true,length:{maximum:200} 
    belongs_to :user 

end 

下面的代碼是路線。

KaguShop::Application.routes.draw do 

    resources :users,only:[:show,:new,:create,:edit,:update,:destroy] do 
     collection do 
     get 'get_images',as:'get_images' 
     end 
    resources :products,only:[:show,:index,:new,:create,:edit,:update,:destroy] do 
     collection do 
     post 'add_item', as:'add_item' 
     get 'get_image',as:'get_image' 
     get 'get_images',as:'get_images' 
     end 
    end 
    end 

    resources :users,only:[:show,:new,:create,:edit,:update,:destroy] do 
    resource :cart,except:[:new,:show,:edit,:destroy,:update,:create] do 
     collection do 
     post 'purchase', as:'purchase' 
     #get 'show' , as: 'show' 
     post 'create' , as: 'create' 
     #多分routeにas的な感じでtemplateを提供するのが一番いい気がする 
     delete 'destroy',as:'destroy' 
     delete 'remove_item',as:'remove_item' 
     end 
     resources :items,only:[:show,:index] do 
      collection do 
      get 'get_images',as:'get_images' 
      end 
      resources :chatrooms,only:[:index,:create,:new] do 
      end 
     end 
    end 
    end 

    resources :sessions,only:[:new,:create,:destroy] 



    root 'products#index' 
    match '/signup', to:'users#new',via:'get' 
    match '/signin', to:'sessions#new', via:'get' 
    match '/signout', to:'sessions#destroy', via:'delete' 
    match '/contact', to:'nomal_pages#contact', via:'get' 

end 

回答

3

你應該叫Message.new沒有PARAMS因爲message_paramsnil在這個請求,這提高ActionController::ParameterMissing

class ChatroomsController < ApplicationController  
    #......... 
    def new 
     @user = User.find(params[:user_id]) 
     @cart = Cart.find(params[:user_id]) 
     @item = Item.find(params[:item_id]) 
     @message = Message.new 
    end 

    #........ 

    private 

    def message_params 
     params.require(:messages).permit(:id,:user_id,:messages) 
    end 
    #....... 
end 
+0

謝謝!
也許,在「ChatroomController#創建」
我應該從這更新,「@ message = @ user.messages.build」爲「@message = @ user.messages.build(message_params)」 – user3674902 2014-09-28 09:59:34

+0

@ user3674902是的,我認爲在'create' action'message_params'中可用。 – 2014-09-28 10:00:41

+0

Thx man;)我會稍後再試! – user3674902 2014-09-28 10:03:15

相關問題