當「的link_to」,而不使用塊它的工作完美:的Rails:用的link_to做塊不起作用
<%= link_to "Ololo", {controller: "posts", action: "upvote", id: post.id}, method: :put, remote: true, class:"nav-link" %>
但是當我嘗試使用它的塊錯誤結束:
<%= link_to {controller: "posts", action: "upvote", id: post.id}, method: :put, remote: true, class:"nav-link" do %>
<%= image_tag('icons/candy.svg', alt: "Candies", class:"rounded-circle icon-nav") %>
<%= post.get_upvotes.size %>
<% end %>
這裏是錯誤消息:
SyntaxError (/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected ':', expecting '}'
...r.append= link_to {controller: controller_name, action: "up...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected ',', expecting '}'
...troller_name, action: "upvote", id: entity.id}, method: :put...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected tLABEL
...pvote", id: entity.id}, method: :put, class:"nav-link", remo...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected ',', expecting keyword_end
...method: :put, class:"nav-link", remote: true do @output_buff...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:49: syntax error, unexpected keyword_ensure, expecting end-of-input):
所以現在看來似乎不理解字面哈希,但是,我太新手找出WH在錯誤的...任何幫助表示讚賞!
P.S .:我不能刪除文字哈希,因爲link_to搞亂了CSS類(增加類作爲地址的一部分)。我也有,因爲控制器名稱是由變量(在我的例子是降低了代碼的可讀性)
UPDATE代表使用較早的說法風格替代了類似「upvote_post_path」:
的routes.rb
Rails.application.routes.draw do
root to: "home#index"
resources :posts do
member do
put "upvote", to: "posts#upvote"
end
end
end
您可以傳遞路由,而不是傳遞控制器,操作和id。你能添加你的路線嗎? –
是的,當然!我會更新這個問題。 –