开发者

How can I use a method action like the 'update', 'create' or 'destroy' actions (from scaffold)?

开发者 https://www.devze.com 2023-01-31 11:57 出处:网络
I made these changes appropriately: Now in \'app/controllers/users_controller.rb\' I have this code: def reset

I made these changes appropriately:

Now in 'app/controllers/users_controller.rb' I have this code:

def reset
    respond_to do |format|
        format.html # reset.html.erb
    end
end

def send_reset
    respond_to do |format|
        if @user.errors.empty?
            ...
        else
            format.html { render :action => 'reset' }
        end
    end
end

Now in 'config/routes.rb' I have this code:

  resources :users do
    collection do
      get 'reset'
      post 'reset'
      get 'send_reset'
      post 'send_reset'
    end
  end

If I submit the form ('app/views/users/reset.html.erb') related to changes

<%= form_tag( send_reset_users_path, :method => :post ) do %>
    <%= text_field_tag :email %>
    <%= submit_tag("Send") %>
<% end %> 

the browser URL become '.../users/send_reset' but I want '.../users/reset' (I think the problem is around the 'format.html { render :action => 'reset' }' code that does not render correctly the action) or, maybe, the route.

In few words I aim to use the 'send_reset' action like I use, for example, the 'update', 'create' or 'destroy' actions (from scaffold), in my case that is without creating the 'app/views/users/send_reset.html.erb' file but just calling the action method to handl开发者_如何学Ce my issue. How can I make this?


The default Rails URL scheme is

:host/:controller/:action/:id

Rename your controller action from

def send_reset
end

to

def reset
end

and rename the views and routes to match this change.

However you are already using reset for get and send_reset for Post but you want them to be the same just do different things if you ask for the page or send a form POST.

def reset
   case request.method
   when :post
       # send them an email
       # redirect_to :somewhere_else
   else # :get, :put, :delete
       # render the view with the recovery form
   end
end
0

精彩评论

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