开发者

Using Redirection in New Action in Rails 3

开发者 https://www.devze.com 2023-03-15 13:18 出处:网络
I am trying to construct my first new action in rails, thanks to the help of people here I have now got the vast majority working, but due to my mis-understanding of exactly how things work theaction

I am trying to construct my first new action in rails, thanks to the help of people here I have now got the vast majority working, but due to my mis-understanding of exactly how things work the action is not performing the desired action.

I have a database called Items, which contains :id, :external_url and :click_count My goal is to have a开发者_运维问答 link which when clicked on, the user is directed to the external url and :click_count is incremented by 1.

As it stands, I have the following: view

<%= link_to image_tag( item.picture.to_s + ".gif"), items_clickcountplusone_path(:id => item.id)%>

items_controller

def clickcountplusone
    clickeditem = Item.find(params[:id])
    redirect_to clickeditem.external_url if clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
end 

routes.rb

get 'items/:id' => 'items#clickcountplusone', :as => :items_clickcountplusone

Using this code, the page itself loads with all the links visible. However, when I click on a link I get directed to items/whateveridiclickedon NOT the external URL and furthermore the :click_count value does not increase from its initial value.

Am I doing this correctly? In particular is the routes line ok, it currently appears as though i am instructing it to be directed to a specific page on my site for the item, which was not my intention... Also, what is wrong with the if statement meaning the count doesn't increase?

Many thanks for your patience


The mapped route "items/:id" is already associated with the show action and have precedence.

Try associating it with another URL.

get 'items/:id/visit' => 'items#visit', :as => :items_visit
0

精彩评论

暂无评论...
验证码 换一张
取 消