2012-09-12 63 views
0

我試圖通過按下頁面上的按鈕用戶從數據庫中獲取隨機項將顯示,但將具有一個屬性編輯。我遇到的問題是,我不知道如何傳遞一個隨機ID(在數據庫中有效),以便在我的部分中呈現,但仍然能夠更新特定屬性。從標題Rails語法錯誤 - 不能找到它的原因

問題:

SyntaxError in Tasks#main 
    Showing /home/bogdan/ex/bored/app/views/tasks/main.html.erb where line #1 raised: 
/home/bogdan/ex/bored/app/views/tasks/main.html.erb:1: syntax error, unexpected ',', expecting ')' 
...uffer.append= form_for (:task, :url => {:action =>'rand_tas... 
...        ^
/home/bogdan/ex/bored/app/views/tasks/main.html.erb:1: syntax error, unexpected ')', expecting keyword_end 
...url => {:action =>'rand_task'}) do |f|@output_buffer.safe_co... 
...        ^
../app/views/tasks/main.html.erb:8: syntax error, unexpected keyword_ensure, expecting $end 
Extracted source (around line #1): 
1: <%= form_for (:task, :url => {:action =>'rand_task'}) do |f|%> 
2:  <%= render(:partial => "rand_show", :locals => {:f => f}) %></p> 
3: <%end%> 
4: 
Trace of template inclusion: app/views/tasks/main.html.erb 

我試圖在rand_task輸出對象傳遞給_rand_show.html.erb和afterwartds調用一個新的可編輯的形式只爲ATTR我被intressted在id

形式:

<%= form_for(@task = Task.new) do |f| %> 
    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name, :disabled=>true %> 
    </div> 
    <div class="field"> 
    <%= f.label :category %><br /> 
    <%= f.text_field :category, :disabled=>true %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 
隨機

方法:

def rand_task 
    if params[:id] == 'random' 
     @task = Task.order('RANDOM()').first 
    else 
     @task = Task.order('RANDOM()').first // if not i get a nill related error. 
    end 
    end 

請幫助我爲什麼會得到上述錯誤?我正在考慮錯誤的概念嗎?

回答

2
<%= form_for (:task, :url => {:action =>'rand_task'}) do |f| %> 
      ^

問題是form_for(和之間的空間中。如果你刪除它應該工作的空間。