Saturday, 15 March 2014

ruby on rails - Routing Error: No route matches [PUT] "/goals/4/edit" -



ruby on rails - Routing Error: No route matches [PUT] "/goals/4/edit" -

i trying create button alter model record attribute false true. i'm using form_tag follows:

=form_tag edit_goal_path(goal), method: :post =hidden_field_tag :purchased, value: true =submit_tag "purchase"

it's haml, sense free post suggestions erb. i'm getting next error:

no route matches [post] "/goals/4/edit" rails.root: /home/ben/rails_projects/hartwig

however, have next route resources:

put /goals/:id(.:format) goals#update

my controller looks following:

def edit @goal = goal.find(params[:id]) end def update @goal = goal.find(params[:id]) if @goal.update_attributes(goal_params) redirect_to '/goals', notice: "update successful!" else render '/' end end def goal_params params.require(:goal).permit(:item, :description, :picture, :purchased) end

how work? or there improve way solve this?

another solution problem adding new method goal controller:

in goals_controller.rb

def purchase @goal.update_attribute(:purchased, true) end

and add together on top (just add together :purchase)

before_action :set_goal, only: [:show, :edit, :update, :destroy, :purchase]

in routes.rb alter

resources :goals fellow member post 'purchase' end end

to add together new post routes goals have purchase_goal_path can utilize in view this:

link_to 'purchase', purchase_goal_path(@goal), method: :post

ruby-on-rails routes put

No comments:

Post a Comment