Rails link_to update attribute from controller action -



Rails link_to update attribute from controller action -

i want able have link when pressed updates 1 attribute based controller action. how able this? have create separate update route? new @ please bear me.

controller

def completed @meeting = meeting.find(params[:id]) if @meeting.day_of_meeting == @meeting.days @meeting.update_attribute(:day_of_meeting, '1') redirect_to :back else @meeting.increment! redirect_to :back end end

model

def increment! update_attribute(:day_of_meeting, day_of_meeting + 1) end

view

<%= link_to meetings_completed_path(@meeting), remote: true %>

routes

resources :meetings 'meetings/completed' end

there several issues code

since updating record need include method: :put in link , need modify routes file include appropriate route.

in routes file add

#routes.rb resources :meetings fellow member set 'completed' end end

in view

<%= link_to "mark completed", completed_meeting_path(@meeting), remote: true, method: :put %>

since have remote:true in link rails seek render javascript template , since not rendering javascript template template missing error

in controller

def completed @meeting = meeting.find(params[:id]) respond_to |format| # if ... # update meeting record , render js template format.js {render :status => :ok} # render completed.js.erb within 'app/views/meetings/' directory # else # else format.js {render :action => "different_action"} # render different_action.js.erb within 'app/views/meetings/' directory end end

now need write basic javascript in js templates update view.

note : if don't want render utilize format.js { render :nothing => true }. if want utilize redirect_to calls instead of rendering js template remove remote: true form link .

ruby-on-rails controller update-attributes

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -