link to - Rails 4 - Link_to action create with params -


i add link rails app allow users "like" resource (property) directly. click on link add db without asking user.

the like table: user_id, property_id

as can see, user_id , property_id have in link_to params.

routes.rb:

  resources :likes   resources :properties 

index:

<%= link_to "like property", new_like_path(current_user, property.id) %> #does not work idea 

controller:

 def new     @like = like.new   end def create     @like = like.new(like_params)      respond_to |format|       if @like.save         format.html { redirect_to @like, notice: 'like created.' }         format.json { render action: 'show', status: :created, location: @like }       else         format.html { render action: 'new' }         format.json { render json: @like.errors, status: :unprocessable_entity }       end     end   end 

so in properties#index , call create method add on property.

index:-

<%= link_to "like property", likes_path(:user_id => current_user.id, :property_id => property.id), :method => :post %> 

in controller:-

def create @like = like.new(:user_id => params[:user_id], :property_id => params[:property_id])  respond_to |format|   if @like.save     format.html { redirect_to properties_path, notice: 'like created.' }     format.json { render action: 'show', status: :created, location: @like }   else     format.html { render action: 'new' }     format.json { render json: @like.errors, status: :unprocessable_entity }   end end end 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -