Rails:HTML.erb页面不呈现,服务器说,它呈现

时间:2014-09-07 16:36:33

标签: jquery ruby-on-rails ajax ruby-on-rails-4

可以帮我识别并解决为什么我无法看到我的us_show.html.erb模板吗?这是我在控制器中的功能:

def update_info

    respond_to do |format|

        if @traveler_info.update_attributes(traveler_info_params)
            flash[:notice] = 'Provided information has been submitted successfully.'
            # @traveler_info = TravelerInfo.where(user_id:current_user.id).first
            if true#@traveler_info.country == 'US'
                format.html { render :action => :us_show }
                format.js{ render :action => :us_show }
            else
                format.html {  render :action => :non_us_show}
                format.js{ render :action => :non_us_show }
            end
            # render :index, status: :ok, location: @contact_info
            # format.json { render action: 'edit', status: :created, location:    @traveler_info }
            format.js { render action: 'edit', status: :created, location: :us_show }
        else
            flash[:error] = "Provided information couldn't be saved. Please try again"
            format.html { render :action => :edit }
            format.json { render :json => @trav_info.errors.full_messages.join("</br>"), :status => :bad_request }
        end
    end     
end

这是我的服务器日志

Started PUT "/traveler/passport/1" for 127.0.0.1 at 2014-09-07 22:02:12 +0530
Processing by Traveler::PassportController#update as JS
Parameters: {"utf8"=>"✓", "traveler_info"=>{"country"=>"US", "passport_status"=>"1"},   "commit"=>"save changes", "id"=>"1"}
User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
(0.4ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" =  "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'traveler')  AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 1]]
TravelerInfo Load (0.3ms)  SELECT  "traveler_infos".* FROM "traveler_infos"  WHERE "traveler_infos"."user_id" = $1 LIMIT 1  [["user_id", 1]]
TravelerInfo Load (0.6ms)  SELECT  "traveler_infos".* FROM "traveler_infos"  WHERE "traveler_infos"."user_id" = 1  ORDER BY "traveler_infos"."id" ASC LIMIT 1
(0.1ms)  BEGIN
SQL (0.3ms)  UPDATE "traveler_infos" SET "passport_status" = $1, "updated_at" = $2 WHERE "traveler_infos"."id" = 1  [["passport_status", 1], ["updated_at", "2014-09-07 16:32:12.948454"]]
(30.9ms)  COMMIT
Rendered traveler/passport/us_show.html.erb (0.1ms)
Completed 200 OK in 49ms (Views: 4.7ms | ActiveRecord: 33.1ms)

它说它已成功呈现,但事实并非如此。

1 个答案:

答案 0 :(得分:2)

如果您查看日志,则说

  

通过Traveler :: PassportController#update处理为JS

现在在你的update方法中,这行被执行,因为格式是JS

format.js{ render :action => :us_show }

现在让我们再看一下你的日志。你有

  

呈现旅行者/护照/ us_show.html.erb(0.1ms)

注意它是.html而不是.js所以在你的us_show动作中你不是在处理一个js请求。所以,虽然您的模板正在呈现 ,但由于它是一个js请求,所以服务器不知道如何处理它。

<强>修正

您最好渲染部分而不是动作。将模板中的代码分离为部分代码,然后按js渲染。 您可以这样做:

format.js{} #this will make rails look for a file called update_info.js.erb in app/views/controller_name directory

#update_info.js.erb
("#some_parent_id").html(<%=j render "your_partial_name" %>);

您应该结帐Working with JavaScript in Rails