No route matches [patch] "/happy/node/10003 - ruby-on-rails

routes
put '/happy/node/:node_id', to: 'nodes#happy', as: :happy
node controller
def happy
#node = Node.find(params[:node_id])
if #node.update_attributes(:node_status => "happy",
:location_id => params[:location_id],
:hostname => params[:hostname])
redirect_to node_url
end
end
view - form
<%= form_for(#node, url: happy_path(#node), method: :patch, do |f| %>
<%= f.label :location_id, "Location" %>
<%= collection_select :location_id, Location.order(:name), :id, :name, :prompt => "Select Location" %>
<%= f.submit "Save Changes" %>
I am trying to update the node form using a custom action. When i tried this it failed miserably. I will be extremely grateful for your help.

In your routes you are using put, so your method should also be put:
<%= form_for(#node, url: happy_path(#node), method: :put, do |f| %>

You'll need to add a patch route, if you want to continue using PATCH:
patch '/happy/node/:node_id', to: 'nodes#happy', as: :happy

Related

Change URL by filter

I am using friendly_id so that I can create such URLs:
/search/france/weekly/toyota-95
My routes:
scope to: 'search#show' do
get '/search/:car_country_id/:rental_type_id/:car_group_id', as: 'search_country'
end
At the search#show view I have a form:
<%= form_tag search_country_path, :method => :get do %>
<%= select_tag(:car_country_id, options_from_collection_for_select(CarCountry.all, :slug, proc {|c| c.name }, {:selected => #country}), class: "Select-control u-sizeFull") %>
<%= submit_tag t('shared.search'), class: 'btn btn-primary' %>
<% end %>
And search controller:
#country = CarCountry.friendly.find(params[:car_country_id])
So ok, my intention is to change the URL as:
/search/italy/weekly/toyota-95
But the thing is, Rails params always sending france as car_country_id when I select country from select tag and submit it.
So what should I do?
Currently, two car_country_id are sent to Rails server. You can rename one of them:
<%= select_tag(:new_car_country_id, options_from_collection_for_select(CarCountry.all, :slug, proc {|c| c.name }, {:selected => #country}), class: "Select-control u-sizeFull") %>
In your controller, you should check whether new_car_country_id exists. If it does, then redirect to the corresponding path.
Another way is to make sure that the two car_country_id are the same. You should change the form's submit path once select_tag is updated with JavaScript.

Rails nested routes

I am learning Rails and making a reddit clone to help me understand how routing works.
Each post has subreddit. I have the following in my routes file:
resources :subreddits do
resources :posts
end
This way I can create new posts with the following url:
/subreddits/:subreddit_id/posts/new(.:format)
Example:
http://localhost:3000/subreddits/1/posts/new
However I still need to specify in the new posts form what subreddit I want to post to belong to. Wheras what I want is a hidden field that sets the subreddit id to the correct one. The correct one being the one given in the URL.
This is what I currently have:
=simple_form_for #post, html: {multipart: true} do |f|
-if #post.errors.any?
#errors
%h2
=pluralize(#post.errors.count, "error")
prevented this pin from saving
%ul
-#post.errors.full_messages.each do |msg|
%li= msg
.form-group
=f.input :title, input_html: {class: 'form-control'}
.form-group
=f.input :content, input_html: {class: 'form-control'}
=f.hidden_field :subreddit_id, :value => #post.subreddit_id
=f.button :submit, class: "btn btn-primary"
I get the following error:
No route matches {:action=>"show", :controller=>"posts", :id=>33, :subreddit_id=>nil} missing required keys: [:subreddit_id]
I think this is because I am trying to access the subreddit id of a post that hasn't been created yet. How do I solve this? Am I going about it in the wrong direction?
In the new action of the posts_controller, you'll set the #subreddit instance variable
def new
#subreddit = Subreddit.find(params[:subreddit_id])
#post = Post.new
end
In the form, you'll need to change the first line
= simple_form_for [#subreddit, #post], html: {multipart: true} do |f|
:subreddit_id will now be in the url
Try this and check if it works
= f.input :title, :as => :hidden, :input_html => { :value => "some value" }

undefined method `contact_forms_path'

I am running into an error with a form I am trying to create:
ActionView::Template::Error (undefined method `contact_forms_path' for #<#<Class
The thing is I never created a contact_forms route, so I do not know why I am getting an undefined method for the contact_forms_path.
My route for the contact form is:
get "/contact_form/new", to: "contact_form#new"
My view for this form is new.html.erb within my contact_form directory
<%= simple_form_for #contact_form do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email, placeholder: 'example#email.com' %>
<%= f.input :address %>
<%= f.input :city %>
<%= f.input :state %>
<%= f.input :zip_code %>
<%= f.input :phone %>
<%= f.button :submit %>
<% end %>
My model is contact_form.rb and my controller is contact_form_controller.rb.
I could use a little direction. Any help is appreciated. I can pass along more info if needed.
Thanks!
Please try this:
add this to your routes:
post "/contact_form", to: "contact_form#create"
open up the terminal and run:
rake routes|grep contact_form
You should get something like this as a response:
contact_form_new GET /contact_form/new(.:format) contact_form#new
contact_form POST /contact_form(.:format) contact_form#create
This gives you the path for the route. Now you can specify that path in the simple_form_for:
<%= simple_form_for #contact_form, url: contact_form_path, method: :post do |f| %>
form_for assumes a default url for new Foo records as foos_path as explained here...
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
You need to specify the path that the contact_form will post to...
post "/contact_forms", to: "contact_forms#create", as: 'contact_forms"
That will be the route that receives the params when you submit the form.
Note that as a shorthand you could just specify in your routes.rb
resources :contact_forms, only: [:new, :create]
(This assumes that you will use the more conventional contact_forms#new ... convention-over-configuration is to use plural for controller names)

Rails - Silmple Form calls a specified action of my user controller

My problem is that when i try to call a specified method from a simple_form_for form it doesn't work.
Here is my code :
<%= simple_form_for #user, :url => {:action => :register_iban}, :html => { :method => :post } do |f| %>
<div class="col-md-8">
<%= f.input :first_name, :label => t('user-show.payment.form.first_name'), placeholder: "Prénom" %>
<%= f.input :last_name, :label => t('user-show.payment.form.last_name'), placeholder: "Nom" %>
<%= f.input :iban, :label => t('user-show.payment.form.iban'), placeholder: "IBAN" %>
<%= f.input :bic, :label => t('user-show.payment.form.bic'), placeholder: "BIC" %>
</div>
<div class="col-md-8 text-center">
<%= f.submit t('user-show.payment.title'), class: 'btn btn-danger' %>
</div>
<% end %>
So, as you can see, i try to call register_iban method from my user controller.
But when i do that, i have an error : No route matches {:action=>"register_iban", :controller=>"users", :id=>"5", :locale=>nil}
Everytime i create a new method in a controller, i have to create a route in the routes.rb file ? Here, i'd like to make this url : /users/5/register_iban (where "5" is the user id) call my method.
Sorry but i start in ruby and i'm pretty stuck :/
in your config/routes.rb try to add in the users resources
resources users do
member do
post :register_iban
end
end
No route matches {:action=>"register_iban", :controller=>"users", :id=>"5", :locale=>nil}
This error means you're trying to access a route which doesn't exist.
The routes are defined at config/routes.rb:
#config/routes.rb
resources :users do
post :register_iban
end
This will allow you to call the register_iban method in the users controller through your form.
You'll also want to make sure you're calling routes with the appropriate helpers:
<%= simple_form_for #user, url: user_register_iban(#user) %>

Do not post a form in a Rails root route

I have a controller with just one method:
index_controller.rb
def index
# some code
end
The form:
index.html.erb
<%= form_tag :class => "form-inline signup" do %>
<div class="form-group">
<%= text_field_tag :url, nil, :class => "form-control", :placeholder => "URL do tópico" %>
</div>
<%= submit_tag "Enviar", method: :post, :class => 'btn btn-theme' %>
<% end %>
And a simple root route:
root 'index#index'
post '/', to: 'index#index'
The problem is that when I load the root page, the form is posted automatically, when the preferable was to POST just on the button call.
What am I missing here?
You should move the code for the post out to another action that can handle that.
post '/', :to => "index#submit"
Then you can define a submit action within your IndexController to handle the form, and the index action won't run the form code anymore.

Resources