2016-08-10 57 views
0

我的腳手架模型不適用於create操作。 我發現,如果我按提交按鈕,它與TalksController#index但它應該鏈接到​​Ruby on Rails /如何將腳手架鏈接到特定的動作?

我不知道如何解決它鏈接到創建行動。 _form.html.erb和new.html.erb

Create 2天前運行良好。

我發佈我的代碼。 我添加了一些代碼,除了代碼支架使自己

talks_controller.rb

class TalksController < ApplicationController 
    before_action :set_talk, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user!, except: [ :index, :show, :all ] 

    # GET /talks 
    # GET /talks.json 

    def all 
    @talks = Talk.all 
    @talks = Talk.order(created_at: :desc) 
    end 

    def index 
    @talks = Talk.all 
    @talks = Talk.order(created_at: :desc) 

    @whichboard = params[:whichboard] 
    @seq = params[:saveit] 
    @title = params[:savetitle] 
    end 

    # GET /talks/1 
    # GET /talks/1.json 
    def show 
    end 

    # GET /talks/new 
    def new 
    @talk = Talk.new 
    @seq = params[:seq] 
    @whichboard = params[:whichboard] 
    end 

    # GET /talks/1/edit 
    def edit 
    authorize_action_for @talk 
    end 

    # POST /talks 
    # POST /talks.json 
    def create 
    @talk = Talk.new(talk_params) 
    @talk.user_id = current_user 
    @talk.seq = params[:talk][:seq] 
    @talk.where = params[:talk][:where] 

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

    end 

    # PATCH/PUT /talks/1 
    # PATCH/PUT /talks/1.json 
    def update 
    authorize_action_for @talk 
    respond_to do |format| 
     if @talk.update(talk_params) 
     format.html { redirect_to @talk, notice: 'Talk was successfully updated.' } 
     format.json { render :show, status: :ok, location: @talk } 
     else 
     format.html { render :edit } 
     format.json { render json: @talk.errors, status: :unprocessable_entity } 
     end 
    end 

    end 


    # DELETE /talks/1 
    # DELETE /talks/1.json 
    def destroy 
    authorize_action_for @talk 
    @talk.destroy 
    respond_to do |format| 
     format.html { redirect_to talks_url, notice: 'Talk was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_talk 
     @talk = Talk.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def talk_params 
     params.require(:talk).permit(:content, :user_id, :seq, :where) 
    end 
end 

_form.html.erb

<%= simple_form_for(@talk) do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
    <%= f.input :content %> 
    <%#= f.association :user %> 
    <%= f.input :seq, :as => :hidden, :input_html => { :value => @seq } %> 
    <%= f.input :where, :as => :hidden, :input_html => { :value => @whichboard } %> 
    </div> 

    <div class="form-actions"> 
    <%= f.button :submit %> 
    </div> 
<% end %> 

new.html.erb

<div class="row"> 
    <div class='col-md-12'> 
    <p style='text-align:right;'> 
     created by <%= current_user.name %>, Current Time : <%= Time.now %>, board <%= @whichboard %> seq <%= @seq %> 
    </p> 
    </div> 
</div> 
<%= render 'form' %> 
<%= link_to 'Back', talks_path %> 

[第二編輯] routes.rb

Rails.application.routes.draw do 

    devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } 

    root 'cpu#index' 
    post ':controller(/:action(/:id(.:format)))' 
    get ':controller(/:action(/:id(.:format)))' 


    resources :talks 
end 
+0

你可以發表我們的形式和控制器請 – mrvncaragay

+0

好吧我會發布 –

+0

對不起,這是我第一次自己寫一個問題。 –

回答

0

調整你的routes.rb

Rails.application.routes.draw do 

    devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } 

    root 'cpu#index' 


    resources :talks 

    #do you really need these? 
    post ':controller(/:action(/:id(.:format)))' 
    get ':controller(/:action(/:id(.:format)))' 



end 

把默認/包羅萬象的路線上面的resources :talks。路線在第一場比賽中進行處理,所以如果你需要那些默認/超級選手,你可以把它們放在最下面。話雖如此,除非你有他們的要求,大多數現代rails應用程序默認情況下不需要它們,所以你可以將它們註釋掉/刪除它們作爲另一種選擇。

+0

解決了!我無法向所有寫信給我的問題的人表示感謝。謝謝你數千! –

0

索引和創建兩個引用相同的url或相同的路由方法,唯一的區別是方法,如果您使用該方法作爲後,然後,它會去創建,否則它會去索引。但是,如果您傳入要創建的對象的新實例,例如@customer = Customer.new,則可以隱式處理此操作,然後將@customer傳遞給form_for方法。

+0

我使用的方法作爲後,但它去索引,雖然它應該去創建.. –

+0

你可以發佈你的代碼的視圖,控制器和routes.rb配置 – Pragash

+0

我已經發布'新'視圖和控制器。我會張貼routes.rb –

0

利用的form_for方法,您可以指定要執行的動作或只是控制器和動作像這樣的網址:

<%= form_for @post, :url => {:controller => "your-controller-name", :action => "your-action-name"} do |f| %> 

而在你的行動(「你的動作名稱」)你可以調用你的參數,如:

post_id = params[:post][:id] 

希望能幫助你。

+0

我提到了你的建議。我適應了一點,因爲我的代碼使用了simple_form_for。我修正了「<%= simple_form_for @talk,url:'/ talks/create'do | f |%>」它鏈接到/會談/創建並運行良好,但我得到「行動'11'找不到爲TalksController「錯誤。('11'表示新添加的數據的id) –

+0

好吧,所以你只需要在/ config中添加你的routes.rb文件的動作路徑,那就沒關係,請參閱http://guides.rubyonrails.org /routing.html獲取更多信息。 – Tisamu

相關問題