How to use session_path with simple_form? - ruby-on-rails

I'd like to adapt form_tag to simple_form
<%= form_tag sessions_path do %>
<%= label_tag :password %>
<%= password_field_tag :password %>
<% end %>
it works fine, I can log in normally.
<%= simple_form_for(sessions_path) do |f| %>
<%= f.error_notification %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
Here I get
No route matches [POST] "/sessions/new"
In my routes.rb I have following line
get 'login', :controller => 'sessions', :action => 'new'
Could someone help me fix it?

form_for (and therefore simple_form_for) assume that you have a model and are POSTing or PATCHing to its resource route.
But you don't have a model and that route is a GET route, so you need to pass in some options to simple_form to make it work.
This should do it:
simple_form_for(sessions_path), method: :get
But there's also this answer which should help (not the accepted one, the second one):
Does form_tag work with Simple_form?

Related

Rails Nested routes undefined method

I keep getting an
undefined method 'orders_path' for #<#<Class:0x007faefee50a88>:0x007faefee414e8>
when navigating to my new order path url /users/1/orders/new
Been stuck on this, not sure what the deal is.
Routes:
devise_for :users, :path => 'accounts'
resources :users, only: [:index, :show] do
resources :orders
end
root index:
<%= link_to "Create an Order", new_user_order_path(current_user.id) %>
form:
<%= form_for([#user, #order]) do |f| %>
<%= f.hidden_field :user_id %>
<div class="form-group">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: "form-control" %>
</div>
<% end %>
new:
<h1>New Order</h1>
<%= render 'form' %>
<%= link_to 'Back', user_orders_path(#user) %>
When you write this:
<%= form_for(#order) do |f| %>
Rails is looking for orders_path, but you don't really have such a route as you have defined your orders resource nested under the users resource. So, you have to pass both: #user and #order to the form like this:
<%= form_for([#user, #order]) do |f| %>
# your code goes here
<% end %>
If you write the form this way, then Rails will look for this path: user_orders_path which will match your defined route and will work and you won't get this error anymore:
undefined method 'orders_path' for #<#<Class:0x007faefee50a88>:0x007faefee414e8>
So, this will fix your current issue. But, there is another issue in your new.html.erb file where you currently have this:
<%= link_to 'Back', user_orders_path %>
You have to pass the #user as an argument to this method:
<%= link_to 'Back', user_orders_path(#user) %>
otherwise, it will fail again.
Hope this helps to solve your problems.
Since orders is nested on top of users, the form_for declaration will need to include both the user, and the order objects.
<%= form_for([#user, #order]) do |f| %>
Your new template will also need to have a reference to the given user.
<%= link_to 'Back', user_orders_path(#user) %>

How to implement a destroy confirmation form with simple_form?

I'm using Ruby on Rails 4 with simple_form. On my delete page, I would like to display a form confirming the user's decision to destroy the resource in question. Here is the code I am using:
<h2>Delete Page</h2>
<p>Are you sure you want to delete <%= #journalEntry.title %></p>
<%= simple_form_for(#journalEntry, :action => 'destroy') do |f| %>
<%= f.button :submit %>
<% end %>
However, this is getting processed by the update action instead (my server console shows that it is being sent as a PATCH request).
I also tried amending that code to the following, but with the same result:
<%= simple_form_for(#journalEntry, :url => {:action => 'destroy', :id => #journalEntry.id}) do |f| %>
<%= f.button :submit %>
<% end %>
Add :method => :delete option with simple_form_for :
<%= simple_form_for(#journalEntry, :method => :delete) %>
<%= f.button :submit %>
<% end %>

correct syntax for form_for url in rails

This is probably simple, I'm still coming to terms with rails syntax. What is the right syntax to pass the address_id in the url for form_for to a modified route?
This is the form - note the "address_id parameter"
<div class="one_fourth floatcenter">
<%= form_for address, :url => edit_address_path(:id => address.id), :method => :get do |f| %>
<%= content_tag(:button, :class => 'btn btn-inverse') do %> Edit Address
<% end %>
<% end %>
</div>
And this is the route I've configured:
get "edit_address/:id" => "member/addresses#edit"
Id is not being passed to the controller for some reason...
form_for address should be enough if address is a persisted object, but if it's not enough, then form_for address, url: edit_address_path(address) is what you want.
This is very simple. In place of url, you put your post method route:
<%= form_for(#post, url: super_posts_path) do |f| %>
...
<% end %>
You also call by action
<%= form_for #friend,:url=> { action: "create_friend"} do |f|%><br>
<%= f.label :u_from %>
<%= f.text_field :u_from %>
<%= f.label :u_to %>
<%= f.text_field :u_to %>
<%= f.submit%>
<% end %>

Rails: form_for routes won't display a good form

i'm new to Rails 3 and am stuck with something i think is simple to solve.
I was following the examples from Head First: Rails but came to the conclusion that they are using rails 2.
I made an html.erb file like this:
<h1>New user</h1>
<% form_for(#user, :url=>{:action=>'create'}) do |f| %>
<p>Fullname: <%= f.text_field :fullname%></p>
<p>Username: <%= f.text_field :username%></p>
<p>Email: <%= f.text_field :email%></p>
<p>Password: <%= f.text_field :password%></p>
<p><%= f.submit "Create" %></p>
<% end %>
and i want to show this form show up when i go to http://localhost:3000/users/new so i added a route like this:
resources :users
match "users/new" => "users#new"
match "users/create" => "users#create"
When i go to the address, it show's a web page with a H1-header: New User, but it doesn't show any of the other things.
What did i forget?
resources :users already contains
match "users/new" => "users#new"
match "users/create" => "users#create"
So you can safely remove them. Form doesn't display, because if you want to display result of excecution ruby code, than you should use following construction <%= %>. <% %> stands for only code execution, not for displaying.
So your new view should be like this
<h1>New user</h1>
<%= form_for(#user, :url=>{:action=>'create'}) do |f| %>
<p>Fullname: <%= f.text_field :fullname%></p>
<p>Username: <%= f.text_field :username%></p>
<p>Email: <%= f.text_field :email%></p>
<p>Password: <%= f.text_field :password%></p>
<p><%= f.submit "Create" %></p>
<% end %>
In rails 3 you need to use <%= form_for
form_for

creating a form to delete a resource

I have a subscriber resource (mailinglist) and want to make a unsubscribe form. I created a remove view with a form
<%= form_for(Subscriber.new, :action => :delete) do %>
email: <%= text_field_tag :mail %>
<%= submit_tag "Sign out" %>
<% end %>
I try to call the delete method of the controller but instead the edit action gets called.
The problem is that the RESTful routes to the destroy action needs an :id of the resource to be deleted and since you use Subscriber.new as a source for creating the form, it cannot create an appropriate url to post to.
You can go around this by using routes like this:
<% form_for(:subscriber, :url => subscriber_path("email"), :html => {:method => :delete}) do %>
email: <%= text_field_tag :mail %>
<%= submit_tag "Sign out" %>
<% end %>
Note that you have to edit subscriber_path to your own routing but by using "email" as an identifier you make sure that no faulthy :id is being passed to the controller and you can use the email to find the correct model to destroy as I think was what you wanted to do.
<%= form_for #subscriber, :method => :delete do %>
email: <%= text_field_tag :mail %>
<%= submit_tag "Sign out" %>
<% end %>
This should do the trick.

Resources