2011-11-08 105 views
0

我正在關注教程http://fuelyourcoding.com/getting-started-with-jquery-mobile-rails-3/將簡單的腳手架Rails 3應用程序中的視圖轉換爲jquery移動前端。Rails jquery移動路由/渲染問題

創建新記錄後,我將其傳遞到show view,並實際查看show view的結果,因爲在該記錄的兩個新創建的字段中顯示,但在瀏覽器中的URL爲http://localhost:3000/currencies。當我查看源代碼時,源代碼實際上是索引視圖,而不是在瀏覽器中顯示的顯示視圖,這非常奇怪。任何想法爲什麼發生這種情況?

的Gemfile:

source 'http://rubygems.org' 

gem 'rails', '3.0.10' 

# Bundle edge Rails instead: 
# gem 'rails', :git => 'git://github.com/rails/rails.git' 

gem 'sqlite3' 
gem 'jquery-rails' 

途徑:

Mycurrency::Application.routes.draw do 
    resources :currencies 

    #match ':name' => 'Currencies#show', :as => 'currency_name' 

    root :to => 'currencies#index' 

控制器:

class CurrenciesController < ApplicationController 
    # GET /currencies 
    # GET /currencies.xml 
    def index 
    @currencies = Currency.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @currencies } 
    end 
    end 

    # GET /currencies/1 
    # GET /currencies/1.xml 
    def show 
    # @currency = Currency.find(params[:id]) 

    if params[:name] 
     if Currency.where(:name => params[:name]).first != nil 
     @currency = Currency.where(:name => params[:name]).first 
     else 
     redirect_to root_path 
     end  
    else 
     @currency = Currency.find(params[:id]) 
    end 

    # respond_to do |format| 
    # format.html # show.html.erb 
    # format.xml { render :xml => @currency } 
    # end 
    end 

    # GET /currencies/new 
    # GET /currencies/new.xml 
    def new 
    @currency = Currency.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @currency } 
    end 
    end 

    # GET /currencies/1/edit 
    def edit 
    @currency = Currency.find(params[:id]) 
    end 

    # POST /currencies 
    # POST /currencies.xml 
    def create 
    @currency = Currency.new(params[:currency]) 

    respond_to do |format| 
     if @currency.save 
     format.html { redirect_to(@currency, :notice => 'Currency was successfully created.') } 
     format.xml { render :xml => @currency, :status => :created, :location => @currency } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @currency.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /currencies/1 
    # PUT /currencies/1.xml 
    def update 
    @currency = Currency.find(params[:id]) 

    respond_to do |format| 
     if @currency.update_attributes(params[:currency]) 
     format.html { redirect_to(@currency, :notice => 'Currency was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @currency.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /currencies/1 
    # DELETE /currencies/1.xml 
    def destroy 
    @currency = Currency.find(params[:id]) 
    @currency.destroy 

    respond_to do |format| 
     format.html { redirect_to(currencies_url) } 
     format.xml { head :ok } 
    end 
    end 
end 

application.html.erb:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Mycurrency</title> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.css" /> 
    <%= javascript_include_tag :defaults %> 
<script src="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.js"></script> 
    <%= csrf_meta_tag %> 
</head> 
<body> 
<div data-role="page"> 
    <%= yield %> 
</div> 

</body> 
</html> 

回答

0

jQuery Mobile通過AJAX加載頁面,將它們添加到DOM,然後用所有jQuery Mobile樣式增強它們。由於這種通過AJAX加載頁面的方法,頁面的來源不會隨着用戶在網站周圍導航而改變。

要查看當前頁面的源代碼,您需要刷新網頁。

我建議你閱讀了AJAX導航的jQuery Mobile的文檔:http://jquerymobile.com/demos/1.0rc2/docs/pages/page-navmodel.html

+0

謝謝賈斯珀的洞察力。我遇到的問題與此處解決的問題相同http://forum.jquery.com/topic/restful-resources-rails-3-and-jquery-mobile。 – ponzicoder

+0

有沒有人想出了這個解決方法?當然,我們不能成爲Rails 3/JQM應用程序中唯一面臨此問題的人。 – jn29098

0

添加到您的佈局。它強制瀏覽器緩存更新URL。

<div data-role="page" id="home" data-url="<%= request.path %>">