link_to_unless_current fails when processing forms with error messages in it with restful routes

View: New views
2 Messages — Rating Filter:   Alert me  

link_to_unless_current fails when processing forms with error messages in it with restful routes

by Lp-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message




Hello,

does anyone know how to prevent the failing mechanism of
link_to_unless_current?

f.e.: I have my page navigation with

link_to_unless_current "new task", new_task_path

When I click on the link, i come to the new taks path form... And no
link is created -> ok. Then I put incorrect values in the form and
submit.

The TasksController processes the "create" action, the validation for
the ActiveRecord-model fails because of the incorrect data and the
controller renders the "new" action (and includes the error messages
for the model).

class TasksController < ApplicationController
    def create
        @task = Task.new(params[:task])

        if @task.save
            flash[:notice] = 'task was successfully created.'
            redirect_to(tasks_url)
          else
            render :action => "new"
        end
    end
end

But here the link gets created! -> Because of the difference between
the urls:

  link path = new_task_path

but

  posted path = tasks_path with :method => :post

Does anybody know how to cleanly solve this problem?

Thanks

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to_unless_current fails when processing forms with error messages in it with restful routes

by Lp-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


ou can do it with link_to_unless instead of link_to_unless_current:

link_to_unless(controller_name == 'tasks' &&
                 (action_name == 'new' || action_name == 'create'),
               new_task_path)


On 9 Jul., 16:24, Lp <lampesberger.pe...@...> wrote:

> Hello,
>
> does anyone know how to prevent the failing mechanism of
> link_to_unless_current?
>
> f.e.: I have my page navigation with
>
> link_to_unless_current "new task", new_task_path
>
> When I click on the link, i come to the new taks path form... And no
> link is created -> ok. Then I put incorrect values in the form and
> submit.
>
> The TasksController processes the "create" action, the validation for
> the ActiveRecord-model fails because of the incorrect data and the
> controller renders the "new" action (and includes the error messages
> for the model).
>
> class TasksController < ApplicationController
>     def create
>         @task = Task.new(params[:task])
>
>         if @task.save
>             flash[:notice] = 'task was successfully created.'
>             redirect_to(tasks_url)
>           else
>             render :action => "new"
>         end
>     end
> end
>
> But here the link gets created! -> Because of the difference between
> the urls:
>
>   link path = new_task_path
>
> but
>
>   posted path = tasks_path with :method => :post
>
> Does anybody know how to cleanly solve this problem?
>
> Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---