2013-06-28 63 views
1

我試圖保存插入到窗體中的數據,當我按保存&繼續它再次進入'新',並且我試圖改變控制器中的錯誤操作並且它重定向到應用程序的任何頁面,但它是不是假動作在所有響應,並在瀏覽器的網址更改爲:窗體不保存到數據庫

0.0.0.0:3000/students/15/student_previous_data/new?utf8=%E2%9C%93&student_previous_data[student_id]=15&student_previous_data[institution]=institution&student_previous_data[course]=course&student_previous_data[year]=2010&student_previous_data[total_mark]=60&commit=Save+%26+Proceed 

對於今年屬性我使用的自舉日期選擇器,我使用此JQuery代碼

jQuery -> 
    $(".year").datepicker({ format: "yyyy", viewMode: "years", minViewMode: "years", autoclose: true }); 

這就是創建活動

 student_previous_data_index GET /student_previous_data/index(.:format)       student_previous_data#index 
     student_previous_data_show GET /student_previous_data/show(.:format)       student_previous_data#show 
      student_previous_data_new GET /student_previous_data/new(.:format)       student_previous_data#new 
     student_previous_data_edit GET /student_previous_data/edit(.:format)       student_previous_data#edit 
        guardians_index GET /guardians/index(.:format)          guardians#index 
        guardians_show GET /guardians/show(.:format)          guardians#show 
        guardians_edit GET /guardians/edit(.:format)          guardians#edit 
        students_index GET /students/index(.:format)          students#index 
         students_show GET /students/show(.:format)          students#show 
         students_edit GET /students/edit(.:format)          students#edit 
        article_comments GET /articles/:article_id/comments(.:format)      comments#index 
            POST /articles/:article_id/comments(.:format)      comments#create 
       new_article_comment GET /articles/:article_id/comments/new(.:format)     comments#new 
       edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format)    comments#edit 
        article_comment GET /articles/:article_id/comments/:id(.:format)     comments#show 
            PUT /articles/:article_id/comments/:id(.:format)     comments#update 
            DELETE /articles/:article_id/comments/:id(.:format)     comments#destroy 
          articles GET /articles(.:format)           articles#index 
            POST /articles(.:format)           articles#create 
         new_article GET /articles/new(.:format)          articles#new 
         edit_article GET /articles/:id/edit(.:format)         articles#edit 
          article GET /articles/:id(.:format)          articles#show 
            PUT /articles/:id(.:format)          articles#update 
            DELETE /articles/:id(.:format)          articles#destroy 
           users GET /users(.:format)            users#index 
            POST /users(.:format)            users#create 
          new_user GET /users/new(.:format)           users#new 
          edit_user GET /users/:id/edit(.:format)          users#edit 
           user GET /users/:id(.:format)           users#show 
            PUT /users/:id(.:format)           users#update 
            DELETE /users/:id(.:format)           users#destroy 
     student_student_previous_data GET /students/:student_id/student_previous_data(.:format)   student_previous_data#index 
            POST /students/:student_id/student_previous_data(.:format)   student_previous_data#create 
new_student_student_previous_datum GET /students/:student_id/student_previous_data/new(.:format)  student_previous_data#new 
edit_student_student_previous_datum GET /students/:student_id/student_previous_data/:id/edit(.:format) student_previous_data#edit 
    student_student_previous_datum GET /students/:student_id/student_previous_data/:id(.:format)  student_previous_data#show 
            PUT /students/:student_id/student_previous_data/:id(.:format)  student_previous_data#update 
            DELETE /students/:student_id/student_previous_data/:id(.:format)  student_previous_data#destroy 
        student_guardians GET /students/:student_id/guardians(.:format)      guardians#index 
            POST /students/:student_id/guardians(.:format)      guardians#create 
       new_student_guardian GET /students/:student_id/guardians/new(.:format)     guardians#new 
       edit_student_guardian GET /students/:student_id/guardians/:id/edit(.:format)    guardians#edit 
        student_guardian GET /students/:student_id/guardians/:id(.:format)     guardians#show 
            PUT /students/:student_id/guardians/:id(.:format)     guardians#update 
            DELETE /students/:student_id/guardians/:id(.:format)     guardians#destroy 
          students GET /students(.:format)           students#index 
            POST /students(.:format)           students#create 
         new_student GET /students/new(.:format)          students#new 
         edit_student GET /students/:id/edit(.:format)         students#edit 
          student GET /students/:id(.:format)          students#show 
            PUT /students/:id(.:format)          students#update 
            DELETE /students/:id(.:format)          students#destroy 
          sessions GET /sessions(.:format)           sessions#index 
            POST /sessions(.:format)           sessions#create 
         new_session GET /sessions/new(.:format)          sessions#new 
         edit_session GET /sessions/:id/edit(.:format)         sessions#edit 
          session GET /sessions/:id(.:format)          sessions#show 
            PUT /sessions/:id(.:format)          sessions#update 
            DELETE /sessions/:id(.:format)          sessions#destroy 
        user_dashboard GET /user/dashboard(.:format)          static_pages#home 
       student_admission1 GET /student/admission1(.:format)         students#new 
       student_admission2 GET /student/admission2(.:format)         guardians#new 
           root  /               sessions#new 

如何使形式去真正保存在:N在我的控制器

def create 
    @student_previous_data = StudentPreviousData.new(params[:student_previous_data]) 
    if @student_previous_data.save 
     flash[:success] = 'Record Saved Successfully.' 
     redirect_to '/user/dashboard' 
    else 
     flash.now[:error] = 'An error occurred please try again!' 
     render 'new' 
    end 
    end 

student_previous_data/new.html.erb

<h1>Admission</h1> 
<h4>Step 3 - Previous details</h4> 

<div class="row-fluid"> 
    <div class="span5 offset1 hero-unit"> 
    <%= bootstrap_form_for(@student_previous_data, :url => new_student_student_previous_datum_path(@student), 
          html: { class: 'form-horizontal' }, method: :get) do |f| %> 
     <% if @student_previous_data.errors.any? %> 
      <div id="error_explanation"> 
       <div class="alert alert-error"> 
       The form contains <%= pluralize(@student_previous_data.errors.count, 'error') %> 
       </div> 
       <ul> 
       <% @student_previous_data.errors.full_messages.each do |msg| %> 
        <li>* <%= msg %></li> 
       <% end %> 
       </ul> 
      </div> 
     <% end %> 


      <fieldset> 
       <div class="field"> 
       <%= f.hidden_field :student_id, value: @student.id %> 

       </div> 

       <h4>Previous Educational Details</h4> 
       <div class="field"> 
       <%= f.text_field :institution, label: 'Institution Name'%> 
       </div> 
       <div class="field"> 
       <%= f.text_field :course, label: 'Course'%> 
       </div> 
       <div class="field"> 
       <%= f.text_field :year , class: 'year', label: 'Year' %> 
       </div> 
       <div class="field"> 
       <%= f.text_field :total_mark, label: 'Total Mark'%> 
       </div> 
       <div class="actions"><%= f.submit 'Save & Proceed', class: 'btn btn-mini btn-primary' %></div> 
      </fieldset> 
     <% end %> 
     </div> 

運行Rake routes命令後數據庫?

+0

對於保存/繼續邏輯,您的視圖代碼的外觀如何? – lurker

+0

問題更新後,我添加了new.html.erb –

+0

您的網址有誤。運行耙路線,並找到正確的路線 – sunny1304

回答

1

正是在這個名字本身出了問題,控制器應該接受的名稱作爲singular但形式接受名字plural,我已經在控制器中創建一個新的controllermodel名爲'previous_detail'應該>>redirect_to new_student_previous_detail_path(@student)並在表單中應該是student_previous_details_path(@student)

1

您的表單url不正確。您的網址應類似於以下內容:

:url => student_student_previous_data_path(@student) 

您目前的網址

:url => new_student_student_previous_datum_path(@student) 

總是會直接將您重定向到新的行動。您永遠不會傳遞創建操作。

+0

我已經嘗試過這一點,現在再次嘗試確保,它不工作,因爲它會在重定向到新表單之前產生錯誤,這是我之前的錯誤,如果我使用了您的解決方案,它會再次發生http://stackoverflow.com/questions/17353929/cant-reach -to-the-next-form-ror-3 –

+0

你也有'method::get'。你需要發佈。 – aceofspades

+0

老兄,我已經嘗試過,但它引發了路由錯誤 沒有路由匹配{:action =>「show」,:controller =>「student_previous_data」,:student_id =>#<學生id:22,admission_no:「995」 ,class_roll_no:nil,admission_date:nil,first_name:「aaa」,middle_name:「bbb」,last_name:「ccc」... –

1

您正在使用GET方法,使用POST方法。

而且使用student_student_previous_data_path(@student)在URL

+0

不工作會引發路由錯誤沒有路由匹配{:action =>「show」,:controller =>「student_previous_data」,:student_id =>#<學生id:22,admission_no:「995」,class_roll_no:nil,admission_date:nil,名字:「aaa」,中間名:「bbb」,姓氏:「ccc」.. –

+0

它不應該訪問POST方法的顯示操作。你能否粘貼你的'routes.rb'文件? –

1

變化從

<%= bootstrap_form_for(@student_previous_data, :url => new_student_student_previous_datum_path(@student), 
         html: { class: 'form-horizontal' }, method: :get) do |f| %> 

您的form_for到

<%= bootstrap_form_for(@student_previous_data, :url => student_student_previous_data_path(@student), 
         html: { class: 'form-horizontal' }, method: :post) do |f| %> 

,它會工作。