rails admin edit view path routes - ruby-on-rails

I've got an admin section setup, but am having trouble getting the "update" route to work.
Getting error when hitting "update" via the edit view:
"No action responded to 2."
For some reason the route is responding to the :id as the :action.
Parameters:
Parameters: {"commit"=>"Update", "action"=>"2", "_method"=>"put", "admin"=>{"ended_at(1i)"=>"2010", "ended_at(2i)"=>"8", "ended_at(3i)"=>"22"}, "id"=>"edit", "controller"=>"admin/subscriptions"}
The edit view uri:
/admin/subscriptions/2/edit
Edit view:
<% form_for :admin, #subscription, :html => {:method => :put} do |f| %>
<p>
<%= f.label :ended_at %><br />
<%= f.date_select :ended_at %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
Route:
map.namespace :admin do |admin|
admin.resources :subscriptions
end
I assume I need to do something differently in the form_for method or maybe the routes, but everything I've tried isn't working.
Thanks for the help!!

It should be this:
<% form_for [:admin, #subscription] do |f| %>
By putting :admin and #subscription in square-brackets, this makes it into an array which is passed as the first argument to form_for. The benefit of this is if #subscription is a pre-existing record (as-in, one found by find, not created with new) then Rails will know to set the form method to PUT.

This works:
<% form_for :admin, #subscription, :html => {:method => :put}, :url => { :action => "update" } do |f| %>
Seems verbose though. Any better ideas?

Try
- form_for :subscription, #subscription do |f|
We're using formtastic here.

Related

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) %>

Routing a resource to controller

How do you route a resource to its controller? I am using the resource in an edit page for a different model, so my actions are being routed to its model controller first.
This edit page requests from
class Grandstreamers::ResellersController < ApplicationController
def new
end etc...
I am trying to route the requests to here instead:
Grandstreamers::CertificatesController < ApplicationController
def new
end
def update
end etc...
This is my form under views/grandstreamers/resellers/edit.html.erb
<%= form_for #untrained, :url => certificates_update_path(#untrained) do |f| %>
<p> Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(#current_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Un-Train", class: "btn btn-large btn-primary" %>
<% end %>
<%= form_for #trained, :url => certificates_create_path(#trained) do |f| %>
<p> Non-Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(#non_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Train", class: "btn btn-large btn-primary" %>
<% end %>
My route is:
resources :certificates
Note that the
:url => certificates_create_path
is not correct and not working. Is there a way to specify this route in my routes.rb file or in my form? Thank you
EDIT
This is my resellers_controller edit() which is routes to first.
#trained = Certificate.new(params[:certificate])
#Trying to get to certificates_controller update. Then update the :attend to "No"
##untrained = Certificate.new(params[:certificate])
##untrained = Certificate.find(params[:id])
##untrained = Certificate.find_by_user_id(params[:id])
#untrained is not defined, I am not sure how to get it to just go to my certificate controller. For #trained I can define it since its not made yet and does not give me errors when it cant find a correct value.
My certificates controller which uses create() but cannot get to update()
def create
#trained = Certificate.new(params[:certificate])
if #trained.save
#trained.update_attributes(attend: "Yes")
end
redirect_to grandstreamers_resellers_path
end
def update
#untrained = Certificate.find(params[:id])
#untrained.update_attributes(attend: "No")
redirect_to grandstreamers_resellers_path
end
Major Issue
The instance variable #trained and #untrained need to be defined somehow in reseller_controller. What can I define them as to load the edit page?
Part Solution
I defined this is in my resellers_controller and it loads the edit page now.
#untrained = User.find(params[:id])
Now I get this error:
No route matches [PUT] "/certificates.1"
I believe the problem is you need to let routing know the full name to the controller.
From the Rails routing guide:
scope module: 'Grandstreamers' do
resources :certificates
end
Use these paths when creating the form:
<%= form_for #untrained, :url => certificate_path(#untrained), :method => :put do |f| %>
<%= form_for #trained, :url => certificates_path, :method => :post do |f| %>
use this his routes.rb file
namespace :grandstreamers do
resources :certificates
end

Rails simple form routing issue

I have a form in Rails
<div class="page-header">
<h3>Create Blah</h3>
</div>
<%= simple_form_for #blah do |f| %>
<%= f.input :id %>
<%= f.input :name %>
<%= f.input :pho %>
<%= f.input :fun %>
<%= f.submit :class => 'btn btn-primary' %>
<% end %>
<br>
When I click the submit button, where does the code attempt to go? Does it call the create method for blah_controller.rb? Because currently, I get a routing error
Routing Error
uninitialized constant BlahsController
Here is the BlahController#create method:
def create
authorize! :create, :blahs
#blah = Blah.new(params[:blah])
if #blah.save
redirect_to admin_blah_path(#blah), :notice => 'New blah created!'
else
render :new
end
end
In my rake routes, I have
admin_blahs GET /admin/blahs(.:format) admin/blahs#index
POST /admin/blahs(.:format) admin/blahs#create
new_admin_blah GET /admin/blahs/new(.:format) admin/blahs#new
edit_admin_blah GET /admin/blahs/:id/edit(.:format) admin/blahs#edit
admin_blah GET /admin/blahs/:id(.:format) admin/blahs#show
PUT /admin/blahs/:id(.:format) admin/blahs#update
DELETE /admin/blahs/:id(.:format) admin/blahs#destroy
It looks like your BlahsController is a namespaced controller, living under the Admin module (i.e., its fully-qualified name is Admin::BlahsController). If so, when constructing forms you must also provide the :admin namespace, using something like the following:
<%= simple_form_for [:admin, #blah] do |f| %>
See the Rails Guide to Form Helpers, under the "Dealing with Namespaces" section.

Editing a single form field with rails

I have a form field in a view that is separate from my initial form. I want the person using the application to be able to edit a single field without having to pull up the entire form. My code is as follows
<%= form_for :user, :url => {:controller => 'users', :action => 'update' } do |f| %>
<%= f.text_field :barcode %>
<%= submit_tag 'Register' %>
<% end %>
When trying to submit the changes to the specified form field I receive an error on the create method I believe. It redirects me to the user controller with the proper id but gives me the following error.
Unknown action
The action '1' could not be found for UsersController
I have tried changing the method from update to create, but then it brings up the blank form, I just want to be able to edit the specified field without having to re-create the form and get the error. Any ideas?
You are not passing the user object to the form.
Try also using the path helper generated by the routes:
<%= form_for #user, :url => user_path(#user) do |f| %>
<%= form_for(#user), :url => url_for(:controller => 'users', :action => 'update') do |f| %>
<%= f.text_field :barcode %>
<%= f.submit, 'Register' %>
<% end %>
It should work now...

How to use form_for helper in rails?

New to rails. I am using form_for helper in rails 2.3.5. The question is, when editing a record, I cannot use:
<% form_for :myrecord, :url => {:action => :update} %>
...
<% end %>
But I have to use:
<% form_for #myrecord, :url => {:action => :update} %>
... # the same as above
<% end %>
When I use the first code, it always return error saying undefined method. Shouldn't the first code and the second code serves the same when "myrecord" is the name of the instance passed from controller to view?
Thank you for any hints and answers.
form_for take a block so it's more like. If this record is same that resource, the :url option is not needed. FormHelper check if #myrecord is #new_record? or not. If new_record use POST instead use PUT.
<% form_for #myrecord do |f| %>
<%= f.text_field :field %>
<% end %>

Resources