2014-06-09 175 views
0

我沒有錯誤,但當我點擊「確定」(提交按鈕)什麼都沒有發生,我的數據庫根本沒有改變, 即時通訊按我的應用程序,如我按下一個按鈕,然後所有進一步的頁面已經被加載,並且我使用ajax和jquery在它們之間導航,並且在這些頁面之一中實現我的表單。這可能是因爲這個?當我在 「OK」 點擊(提交按鈕)它真的不控制檯上的反應:S我不能保存提交數據庫

我的窗體上的部分: _impediments_risks.html.erb

<div class="item-list"> 
    <%@solution = Solution.new(params[:solution])%> 
    <% for i in (0..session[:project_impediments].size-1) %> 
     <div class="item"> 
     <% impediment = session[:project_impediments][i][1] %> 
     <div class="title"> 
      <span class="indicator"><%= impediment.priority.name %></span> 
      <div class="description"><%= impediment.subject %></div> 
     </div> 
     <div class="content"> 
      <span class="title"><%= I18n.t 'text.solution' %></span> 
      <ul> 
      <% sol = ApplicationHelper.apply_filter(ApplicationHelper.Hash_DB(Solution.where('user_id IS NULL or user_id=?',session[:user])), ApplicationHelper.stem_phrase(impediment.subject.downcase)) %> 
       <% sol.each do |s| %> 
       <li><%= s %></li> 
       <% end %> 
       <li><b>NEW SOLUTION</b></li> 
       <li> 
        <%= form_for(@solution) do |f| %> 
        <%= render 'shared/error_messages' %> 
        <%= f.label :text %> 
        <%= f.text_field :text %> 
        <%= f.label :keywords %> 
        <%= f.text_field :keywords %> 
        <%= f.submit "Ok", class: "btn btn-large btn-primary" %> 
        <% end %> 
       </li> 
      </ul> 
     </div> 
     </div> 
    <% end %> 
</div> 

我CONTROLER solutions_controller.rb

class SolutionsController < ApplicationController 

    def new 
    @solution = Solution.new 
    end 

    def create 
    @solution = Solution.new(solution_params) 
    if @solution.save 
     #flash.now[:success] = "New Solution created!" 
    else 
     #Something 
    end 
    end 

    private 

    def solution_params 
    params.require(:solution).permit(:text, :keywords) 
    end 
end 

我的模型: solution.rb

class Solution < ActiveRecord::Base 
    attr_accessible :text, :keywords, :contar , :user_id 

    before_save { |solution| solution.keywords = solution.keywords.downcase } 

    default_scope -> { order('contar DESC') } 
    validates :text, presence: true, uniqueness: { case_sensitive: false } 

    VALID_KEYS_REGEX = /\A([a-zA-Z]{3,})+(&|\|[a-zA-Z]{3,}+)*\z/i 
    validates :keywords, presence:true, format: { with: VALID_KEYS_REGEX } 
end 
+0

發表你的'提交'form'時登錄information'。 – Pavan

+0

也嘗試給這個'<%​​= form_for(@solution,:url => {:action =>「create」,:controller =>'solutions'})do | f | %>' – Pavan

+1

如果你使用強參數,你不應該有attr_accessible 另外,如果你試圖消滅關鍵字,你應該有'self.keywords = keywords.downcase' –

回答

0

看起來您的Solution對象沒有被保存,大概是因爲它沒有通過驗證。通常情況下,您的表單中會出現一些錯誤消息,表示失敗的@solution對象,但是您從不會看到這些消息,因爲每次都會覆蓋@solution。

什麼應該發生的是:

a)提出一個新 - 新的動作,使新@solution對象,並呈現它採用form_for @solution

B)保存一個新 頁面 - 創建操作使用params [:solution]創建一個新的@solution對象。如果不能保存,則會顯示出使用form_for @solution的相同頁面。此頁面通常會顯示@solution的錯誤。

當@Pavan說「嘗試給出這樣的<%= form_for(@solution, ...」時,他假設每一個渲染頁面的動作都會定義@solution。這是標準的方式。

此外,form_for將自動發送到創建或更新操作,具體取決於它是否是新對象。所以,你不應該像使用url選項那樣強制它創建。

我會改變你的代碼是這樣的:

#controller 
def create 
    @solution = Solution.new(params[:solution]) 
    if @solution.save 
    flash.now[:success] = "New Solution created!" 
    #redirect wherever you go after the object is saved 
    else 
    render :template => <your page with the form partial> 
    end 
end 

def update 
    @solution = Solution.find(params[:id]) 
    @solution.attributes = params[:solution] 
    if @solution.save 
    flash.now[:success] = "Solution saved!" 
    #redirect wherever you go after the object is saved 
    else 
    render :template => <your page with the form partial> 
    end 
end  

#view 
... 
<%= form_for(@solution) do |f| %> 
... 
+0

嗨,我試過這個,但它給了我一個錯誤ActionView :: Template :: Error(對於NilClass:Class的未定義方法'model_name') 感謝您試圖幫助 – luishermenegildo

+0

這是因爲@solution在您未被定義正在調用'form_for @ solution'。它是否在您打電話的行動中定義? –

+0

好吧,我已經定義了它,錯誤消失了,但似乎沒有反應,當我按下「確定」。即時通訊做我的應用程序,如我按下一個按鈕,然後進一步進入的所有頁面已經加載,我使用Ajax和jQuery在他們之間導航,並在我的形式之一,在這些頁面。這可能是因爲這個?當我點擊「確定」(提交按鈕)它真的不會在控制檯上反應:S – luishermenegildo