ruby - Rails 4 - routing actions for contact form -



ruby - Rails 4 - routing actions for contact form -

i have 2 actions in controller:

def study @user = user.find_by_slug(params[:slug]) end def reportform @user = user.find_by_slug(params[:slug]) thread.new mail service = ... end @message = 'thanks!' end

and in routes:

# user study form "/user/:slug/report", to: "users#report" # grab study form , action post "/user/:slug/report", to: 'users#reportform'

and view:

<form method="post" action="/user/<%= @user.slug %>/reportform"> ...

but problem is, when send form, action reportform not called , instead of refresh current page form.

what's wrong here?

thank guys.

form helpers

the first thing that's wrong you're not using form helpers rails provides - problem because you'll end niggly little problems 1 you're receiving:

#config/routes.rb resources :users :report #-> domain.com/users/:id/report post :reportform #-> domain.com/users/:id/reportform end #view <%= form_tag user_reportform_path(@user) %> ... <% end %>

routes

the sec issue have routes

you've set next routes:

get "/user/:slug/report", to: "users#report" post "/user/:slug/report", to: 'users#reportform'

this means you've got send request domain.com/user/user_slug/report. form sends url reportform...

you should see routes above solution problem

but more importantly, should read on nested resources:

#config/routes.rb resources :users match :report, action: "reportform", via: [:get, :post] #-> domain.com/users/:id/report end

slug

finally, you're trying utilize params[:slug] in controller

with resourceful routes should using in rails, you'll passing params[:id] of time. should not issue (what contained in params[:id] can anything).

i highly recommend looking @ gem called friendly_id, makes including slugs in application lot simpler:

#app/models/user.rb class user < activerecord::base extend friendlyid friendly_id :name, use: [:slugged, :finders] end

this allow call:

#app/controllers/users_controller.rb class userscontroller < applicationcontroller def reportform user.find params[:id] #-> utilize either `id` or `slug` end end

ruby-on-rails ruby post get routes

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 -