I'm using a constraint to set a subdomain for pages in my app
get '/', to: 'referal#new', constraints: { subdomain: 'keystrategy' }
It brings me to keystrategy.[mypage]. This page only contains a few lines :
<%= form_for #referal, url: {action: "create", subdomain: false} do |f| %>
<%= f.text_field :referer %>
<input type="hidden" value="keystrategy">
<%= f.submit "Valider" %>
<% end %>
But when I try to load this page, I get the following error :
No route matches {:action=>"create", :controller=>"referal", :subdomain=>"keystrategy"}
What am I missing ? I thought the subdomain: false would prevent this
use this code:
<%= form_for #referal, url: referal_url(subdomain: false) do |f| %>
<%= f.text_field :referer %>
<input type="hidden" value="keystrategy">
<%= f.submit "Valider" %>
<% end %>
Ok, so I found out it was not about the form itself, but rather because of routing. When using a subdomain, every new route generated by
resources :whatever
assumes you're doing so in your current subdomain. Therefore, if subdomain.yousite.com points to a view including a form, the form will be handled by subdomain.yoursite.com/whatevers, as a POST request.
So, you have to make sure that this route includes your subdomain. In your routes.rb file, add these lines:
get '/', to: 'whatever#new', constraints: { subdomain: 'yoursubdomain' }
post '/whatevers', to: 'whatever#create', constraints: { subdomain: 'yoursubdomain' }
Your form should look like this :
<%= form_for #referal do |f| %>
<%= f.text_field :referer %>
<input name="referal-type" type="hidden" value="keystrategy">
<%= f.submit "Valider" %>
<% end %>
And you're all set.
Related
I'm new to RoR and I've stumbled upon a problem. I'm trying to approve or deny a rent, and have been successful in doing so using the HTML form.
<form action="<%= approve_rental_url(rental) %>" method="post">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<input type="submit" value="Approve">
</form>
This code works without a problem and I know that it isn't the rails way of doing it. I've tried to write it in the rails way and have encountered a problem which says No route matches [POST] "/cats/2".
#Ruby code
<%= form_for approve_rental_url(rental) do |f| %>
<%= f.submit "Approve" %>
<% end %>
Here is the rails routes
Prefix Verb URI Pattern Controller#Action
approve_rental POST /rentals/:id/approve(.:format) rentals#approve
deny_rental POST /rentals/:id/deny(.:format) rentals#deny
rentals POST /rentals(.:format) rentals#create
new_rental GET /rentals/new(.:format) rentals#new
cats GET /cats(.:format) cats#index
POST /cats(.:format) cats#create
new_cat GET /cats/new(.:format) cats#new
edit_cat GET /cats/:id/edit(.:format) cats#edit
cat GET /cats/:id(.:format) cats#show
PATCH /cats/:id(.:format) cats#update
PUT /cats/:id(.:format) cats#update
root GET / cats#index
When you use form_for, you have to pass record as argument like this:
<%= form_for :person do |f| %>
First name: <%= f.text_field :first_name %><br />
Last name : <%= f.text_field :last_name %><br />
Biography : <%= f.text_area :biography %><br />
Admin? : <%= f.check_box :admin %><br />
<%= f.submit %>
<% end %>
If you need to pass URL, you need to use form_tag instead of form_for:
form_tag('/posts/1', method: :put)
But these helpers are softly deprecated. Now there is form_with helper.
You can pass to it URL or record
form_with(model: nil, scope: nil, url: nil, format: nil, **options)
Actually, since this is such a common problem, in rails you can just write
link_to 'Approve', approve_rental_url(rental), class: 'btn btn-success', method: :post
or use the button_to helper
button_to 'Approve', approve_rental_url(rental), class: 'btn btn-success'
(which will POST by default)
and both will automatically inline a form to perform the POST.
Here the thing goes, I wanna write a login in a page in Ruby on Rails, when the url is localhost:3000/admin, I'll get the page which need to fill in. After the form has filled in, I submit the button and the form should be admitted to the create action with post method, however it still works with get method and new action.
What happens to it?
Here is the routes.rb file:
Prefix Verb URI Pattern Controller#Action
new_admin_users GET /admin/users/new(.:format) admin/users#new
admin_users POST /admin/users(.:format) admin/users#create
admin GET /admin(.:format) admin/users#new
POST /admin(.:format) admin/users#create
posts_home GET /posts/home(.:format) redirect(301, /posts)
posts_about GET /posts/about(.:format) redirect(301, /about)
Then here is the new action
<form class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<%= form_for :user,url:admin_users_path do |f| %>
<p>
<%= f.label :name%><br>
<%= f.text_field :name ,class:"form-control" %>
</p>
<p>
<%= f.label :pass%><br>
<%= f.text_field :pass,class:"form-control" %>
</p>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<p>
<%= f.submit'Sign in', :class=>"btn btn-lg btn-primary btn-block" %>
</p>
<%= link_to 'Back', posts_path %>
<%end%>
</form>
Here is the routes.rb:
Rails.application.routes.draw do
namespace :admin do
resources :users
get "", to:"users#new"
post "", to:"users#create"
end
get "/posts/home", to: redirect("/posts")
get "/posts/about", to: redirect("/about")
resources :posts do
resources :comments
end
root "posts#index"
get "/home",to: "posts#index"
get "/about",to: "posts#about"
end
If you have some throught, please tell me, thanks in advance!
You must have the actual User object built inside your controller. Try using that inside your form instead of symbol :user.
<%= form_for #user, url: admin_users_path do |f| %>
...
You can try explicitly specifying the method post
Instead of
<%= form_for :user,url:admin_users_path do |f| %>
Use this:
<%= form_for :user,url:admin_users_path, method: :post do |f| %>
Also check the created action in form created such way
Hope this Helps
I've followed this tuto to make a search form in my app.
It works well but it doesn't do exactly what I want
At the moment, I've to go to /search to see my result but I want that when a search is made, everywhere in my app, it's redirect on /search and the results are displayed.
My search controller :
def search
if params[:q].nil?
#idees = []
else
#idees = Idee.search params[:q]
end
end
My search form view
<%= form_for search_path, method: :get do |f| %>
<p>
<%= f.label "Search for" %>
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "Go", name: nil %>
</p>
<% end %>
And my route :
get '/search', to: 'search#search'
It has to be a problem with my route but I don't know how to do this
Update your form as:
<%= form_for :search, url: search_path, method: :get do |f| %>
You may need to update you search action too to support nested params.
Im new to rails, and I am not able to understand how I can get data from a submitted form.
This is my form registerduser.html.erb
<%= form_tag("/submitform", :method => "get") do %>
<%= label_tag(:q1, "id:") %>
<%= text_field_tag(:q1) %>
<%= submit_tag("Submit") %>
<% end %>
How do I get the submitted values in this customers controller action?
def submitform
#customers_values = params[:q1]
end
routes.rb
get "customers/submitform"
error:
No route matches [GET] "/submitform"
You set wrong form url. It should be:
<%= form_tag('/customers/submitform', method: :get) do %>
...
or:
<%= form_tag(controller: :customers, action: :submitform, method: :get) do %>
...
I'm trying to nest a form within another using submit_to_remote but it does a PUT instead of a POST. Can anyone explain what's wrong here?
The routes are RESTful:
map.resources :thing
map.resources :item
The view is like this:
<% form_for(#thing) do |f| %>
<% fields_for(Item.new) do |i| %>
<%= i.text_field :name %>
<%= submit_to_remote 'create', 'Create', :url => items_path, :method => "post" %>
<% end %>
<%= f.text_field :title %>
<%= f.submit 'Update' %>
<% end %>
To get around the problem I've been adding another method to the restful routes to do a create on a PUT but it's ugly and I want to know what the problem is.
The submit_to_remote comes out as:
<input name="create" onclick="new Ajax.Request('/items', {asynchronous:true, evalScripts:true, method:'post', parameters:Form.serialize(this.form) + '&authenticity_token=' + encodeURIComponent('blah')});" type="button" value="Create">
Thanks
How about using link_to_remote instead and style the link to like a 'button', or just leave it as a link would be fine to be honest. This way you can control the XmlRequest fully. Currently I think the method is being determined by your actual form that is being submitted by the JS, not the :method your setting in the helper call.