In my ruby on rails application, I want to get controls value into controllers page:
below is view page:
<%= form_for :managerviewresume, :url => { :method => :post, :action => :managerviewresume }) do |f| %>
<table class="table" width="100%">
<tr>
<td><%= #selection %></td> //Here I am checking radio button value
<td>
<label style="font-size:small;">Selected?</label>
<label class="radio inline" style="font-size: small">
</br>
<%= f.radio_button :select, "Yes", :id => "rb_select1" %>
Yes
</label>
<label class="radio inline" style="font-size: small">
<%= f.radio_button :select, "No", :id => "rb_select2" %>
No
</label>
</td>
</tr>
<tr>
<td>
<%= f.submit "Save", { :class => "stylbutton" } %>
</td>
</tr>
</table>
<% end %>
below is controllers page:
class ManagersController < ApplicationController
def managerviewresume
#selection = params[:select]
render "managerviewresumes"
end
end
In the controller's page I am getting below error at this line render 'managerviewresumes' :
Missing template managers/managerviewresumes, application/managerviewresumes with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "C:/Sites/VideoResume/app/views"
below is the route:
match '/managerviewresumes', to: 'managers#managerviewresume', via: 'get'
Kindly suggest what should I do get radio button value from view page into controller page.
waiting for reply.
Thanks.
Render
Firstly, you don't need to use render to render the same view as your action name
When using a Rails controller, you can call this without issue:
#app/controllers/your_controller.rb
Class YourController < ApplicationController
def your_action
#-> will automatically render the "your_action" view
end
end
So I would remove your reference to the render action (as it is an unnecessary step). This will not resolve the issue directly, but should ensure your application is more convention over configuration
--
Routes
Secondly, you may need to look at resourceful routing
In Rails' routing structure, you are able to call resources :controller to generate a series of RESTful routes:
I understand you likely want to keep using your action, but for the sake of correctness, can I recommend you look into your config/routes.rb file & ensure you're using as many resource-based routes as possible:
#config/routes.rb
resources :managers do
collection do
post :managerviewresume
end
end
--
Form
Finally, I think your form needs to be improved
You're using form_for, which is mainly for ActiveRecord objects (if you want to create new record etc. It seems you'll be better suited to using form_tag instead:
<%= form_tag managers_managerviewresume_path do %>
<table class="table" width="100%">
<tr>
<td><%= #selection %></td> //Here I am checking radio button value
<td>
<label style="font-size:small;">Selected?</label>
<label class="radio inline" style="font-size: small">
</br>
<%= radio_button_tag :select, "Yes", :id => "rb_select1" %>
Yes
</label>
<label class="radio inline" style="font-size: small">
<%= radio_button_tag :select, "No", :id => "rb_select2" %>
No
</label>
</td>
</tr>
<tr>
<td>
<%= submit_tag "Save", { :class => "stylbutton" } %>
</td>
</tr>
</table>
<% end %>
This syntax might need checking, but this will send the :select params as required, which is not what your current form will be doing.
This should be coupled with a views/managers/managerviewresume.html.erb file for Rails to load
Related
I'm looping through each instance of a built sub-tournament - and the problem that I'm having has to do with conditionally creating a collection_select box with data fetched via ajax. Here's the view - the line I want to insert code in is marked:
View
<% #tournament.sub_tournaments.each_with_index do |sub, i| %>
<%= f.fields_for :sub_tournaments, sub, :validate => false do |sub_form| %>
<div class="tab-content standings-row-<%= i %>" style="display:none">
<table>
<thead>
<tr>
<th> <h4>Standing</h4> </th>
<th class="standings-field-<%= i %>"></th>
<th></th>
</tr>
</thead>
<tbody>
<%= sub_form.fields_for :standings, :validate => false do |standings| %>
<tr>
<td>
<%= f.hidden_field :_destroy %><%= f.text_field :standing, :class => "standing", readonly: true, :type => "" %>
</td>
<td class="standings-ajax-<%= i %>">**INSERT HERE**</td>
<td><span class="remove">Remove</span></td>
</tr>
<% end %>
</tbody>
</table>
<div class="add-item">
<%= link_to_add_standings_fields(sub_form, :standings) %>
</div>
</div>
<% end %>
<% end %>
I thought about doing the conditional check (it depends upon whether the game selected is a team game or a single-player game) in the controller, but it seems to make more sense as a method (or a helper?). At the moment I have it in Standing.rb (below) - but I'm getting a no method "collection_select" error - so probably form helpers aren't available in models, which seems reasonable. So how could I do this?
Standing.rb
def team_or_player(game)
if Game::TEAM_GAMES.include?(game.name)
self.collection_select(:team_division_id, TeamDivision.where("game_id = ?", game.id),
:id, :name, {include_blank: true})
else
self.collection_select(:player_id, Player.where("game_id = ?", game.id),
:id, :handle, {include_blank: true})
end
end
And how can I pass the f to my AJAX call?
AJAX
$(".standings-ajax-<%= #tab_number %>").html("<%= ** ?? **.team_or_player(#standing, #game) %>");
You can call helper in model:
ActionController::Base.helpers.collection_select( ... )
So from what I can see you should change team_or_player() to class method and call it with:
Standings.team_or_player(#standing, #game)
or as instance
#standing.team_or_player(#standing, #game)
But that you should use self instead of passing #standing.
Me preference would be to put that logic directly in view or to helper.
Good Day, i have this form view/startseites/index.html.erb and i specified a methode in my people_controller, she doesnt go. I prepared some ghost code for understanding. I want to give the entries from dropdowns with the button_to tag to the controller action checkValid. There i want to validate the entries against the database. I have to read and write from and to the table. I hope its clearly enough.
class PeopleController < ApplicationController
def checkValid
#trainerName = params[:trainer_name]
#sportlerName = params[:sportler_name]
#trainerPID = params[:trainer_pid]
#sportlerPID = params[:sportler_pid]
#checks if sportlerID is null
#person = Person.find(params[:sportler_pid])
id = Person.sportler_id
if id = nil then
Person.sportler_id = params[:trainerPID]
else
puts "Sportler can have only one Trainer!"
end
end
...
view/startseites/index.html.erb: this code doesnt go
this should send the drop down selection to the controller action checkValid(). how can i use parameters?
<%=button_to( "Zuordnung erstellen", :action => "checkValid", :controller =>"people" %>**
<table>
<tr>
<td colspan="3">
Jeder Sportler kann ein Trainer haben. </br>
</td>
</tr>
<tr>
<td>Trainer</td>
<td>
<%= collection_select(:trainer, :trainer_id, Trainer.all, :id, :name) %>
</td>
<td>
<%= link_to 'Neuer Trainer', new_person_path(:type => "Trainer") %>
</td>
<tr>
<tr>
<td>Sportler</td>
<td>
<%= collection_select(:sportler, :sportler_id, Sportler.all, :id, :name) %>
</td>
<td>
<%= link_to 'Neuer Sportler', new_person_path(:type => "Sportler") %>
</td>
<tr>
<tr>
<td></td>
<td></td>
<td>
**<%=button_to( "Zuordnung erstellen", :action => "checkValid", :controller => "people") %>**
</td>
<tr>
</table>
i added this line to my routes
match '/people/checkValid', :controller => 'people', :action => 'checkValid'
but: No route matches {:controller=>"people/checkValid", :method=>:checkValid}
no i think it goes but
Template is missing
Missing template people/checkValid, application/checkValid with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "C:/Users/jord/testmood/app/views"
The Missing template error refers to a missing view. You should have check_valid.html.erb view file in app/views/people/ directory.
Also, run rake routes on the command line anywhere within your app's directory. You will receive a list routes that are generated by your routes.rb file. You can double-check there if people#checkValid exists.
Btw, you might want to change checkValid to check_valid so you follow the naming convention for actions in Rails.
I have a simple select form. When a year is selected from the form I would like the results to be returned to the same page but below the form. Can someone explain how to do this? Here is the form page (index.html.erb)
<%= form_tag("lookup/show", :method => "get") do %>
<%= label_tag(:q, "Pub Year :") %>
<%= collection_select(:lookup, :pubyear, #pubyears, :pubyear, :pubyear) %>
<%= submit_tag("Find") %>
<% end %>
Here is the show method from the Lookup controller
def show
#lookuprows = Lookup.return_lookup_row(params[:lookup][pubyear])
respond_to do |format|
format.html
end
end
Here is the show.html.erb page that the results currently go to
<tbody class="lkuptbody">
<% #lookuprows.each do |lkup| %>
<tr class="lkuprow">
<td><input type="text" class="lkupcode" value=<%= lkup.codetype %> /></td>
<td><input type="text" class="lkupdesc" value=<%= lkup.codedesc %> /></td>
<td><input type="text" class="lkuprmks" value=<%= lkup.rermark %> /></td>
</tr>
</tbody>
I understand that I will have to make a partial _show.html.erb, but how do I reference that from the form page (index.html.erb)?
Thanks
If you want the results to appear on the same page but below the form, then the form should send the results to the index action, not to the show action:
<%= form_tag("lookup", :method => "get") do %>
<%= label_tag(:q, "Pub Year :") %>
<%= collection_select(:lookup, :pubyear, #pubyears, :pubyear, :pubyear) %>
<%= submit_tag("Find") %>
<% end %>
and in your LookupController:
def index
#lookuprows = Lookup.return_lookup_row(params[:lookup][pubyear]) unless params[:lookup].nil?
...
end
Then just append the table HTML in your show page below the form (in index.html.erb) wrapped in an if block to filter out the case where #lookuprows is nil:
<% if #lookuprows %>
<tbody class="lkuptbody">
<% #lookuprows.each do |lkup| %>
<tr class="lkuprow">
<td><input type="text" class="lkupcode" value=<%= lkup.codetype %> /></td>
<td><input type="text" class="lkupdesc" value=<%= lkup.codedesc %> /></td>
<td><input type="text" class="lkuprmks" value=<%= lkup.rermark %> /></td>
</tr>
<% end %>
</tbody>
<% end %>
This will show the results in #lookuprows as a table if there are any, if not it will not show the table.
You may want to put that table HTML in a separate partial to clean up the view, but that is not essential to the problem you asked.
Hope that helps.
You want to avoid reloading the page and put it under the form? Here's how to do it with ajax :
Index.html.erb.
<%= form_tag("lookup/show", :method => "get", :data => {:remote => true, :type => "html"}, :id => "lookup_form") do %>
<%= collection_select(:lookup, :pubyear, #pubyears, :pubyear, :pubyear, id: 'lookup_show_select') %>
<%# Add More fields here %>
<%= submit_tag "Submit" %>
<% end %>
<div id='lookup_show_result'></div>
In app/assets/lookup.js.coffee. If you don't use coffeescript, it's easy to translate to JS, just ask.
$ ->
$(document).on 'ajax:success', '#lookup_form', (evt, data) ->
$('#lookup_show_result').html(data)
Controller unchanged, just process the request according to all parameters
This is a minimal untested version, tell me if you're stuck
On a side note, I'm starting to develop a gem that will extend UJS implementation of Rails with jQuery here to make standard update/delete etc easier with ajax:
jQuery UJS extended
I'm creating a simple project tagging application using the Act_As_Taggable_On gem.
Adding projects, and adding tags (in my case 'types' / 'type_list') to each project works great. Where I'm stuck is how to remove individual tags using Act_As_Taggable_On. What I would like is to be able to click the 'x' text link next to each tag (see link) to remove the tag from that projects type_list.
I've searched the documentation and found a method along the lines of:
project.type_list.remove("your tag")
But what I need help with is how to call the remove method on the specific tag, especially since the whole thing is being iterated with .each do
My controller and model code is pretty minimal and standard - based on Act_As_Taggable_On docs. Here is my view code for generating the layout above:
<h1><%= #title %></h1>
<div class="column-left">
<% #projects.each do |project| %>
<div class="p_wrapper">
<table>
<tr>
<td><div class="project p_name"><%= project.name %></div></td>
<td><div class="p_link"><%= link_to 'Edit', edit_project_path(project) %></div></td>
<td><div class="p_link"><%= link_to 'Nuke', project, :confirm => 'Are you sure?', :method => :delete %></div></td>
</tr>
</table>
<table>
<tr>
<td>
<% project.type_list.each do |tag|%>
<div class="p_tag">
<%= tag %>
<%= link_to "x", # %> <!-- THIS IS THE PART I'M STUCK ON -->
</div>
<% end %>
</td>
</tr>
</table>
<table>
<tr>
<td>
<%= form_for(project) do |f| %>
<%= f.text_field :inject_tags %>
<%= f.submit "Add Tag" %>
<% end %>
</td>
</tr>
</table>
</div>
<% end %>
<br />
<%= link_to 'Add new project', new_project_path %>
</div>
Can anyone point me in the right direction? Am I implementing this correctly to be able to actually remove tags as described?
Thanks guys!
I would simply add a new method to your projects controller, like so:
def remove_tag
Project.find(params[:id]).type_list.remove(params[:tag])
end
And in your routes file
resources :projects do
member do
put 'remove_tag', :as => :remove_tag
end
end
And in your view
<%= link_to 'x', remove_tag_project_path(project), :tag => tag, :method => :put %>
Of course you should add some sanitation, but it should work this way...
Based on #Vapire's suggested code - finally worked out a working solution. Just some minor edits to the view, route, and controller. Let me know if you see anything dodgy in here - still trying to get a good grasp of Ruby/Rails so all suggestions/refactoring ideas welcome.
The updated test site is at project-list.heroku.com.
Updated projects controller to find current project, remove :tag passed from index view through route:
def remove_tag
#project = Project.find(params[:id])
#project.type_list.remove(params[:tag])
#project.save
redirect_to projects_path, :flash => { :success => "Updated - tag nuked."}
end
Updated route:
resources :projects
match 'projects/:id/remove_tag/:tag' => 'projects#remove_tag'
Updated the link_to 'x' code to pass :tag params through the updated route above:
<% project.type_list.each do |tag|%>
<div class="p_tag">
<%= tag %>
<%= link_to 'x', {:action => "remove_tag", :id => project.id, :tag => tag,
:controller => "projects"} %>
</div>
<% end %>
This is obviously new ground for me so would appreciate if you have a different / better way of handling this issue please let me know! Also, thanks for your help #Vapire!
I am new to ROR and am trying to pass two variables from two fields in a view to a controller to generate a dataset for an excel export. The excel export works, however all I get are headers and no data. I know it's because the parameters aren't being passed to the controller.
Here's the code. The SQL has been shortened...
In View
<table border="0" cellspacing="0" cellpadding="10">
<tr>
<td style="text-align: left; width:400px;"> Enter any part of a BU Manager Name, Subcontractor name, Subcontractor ID, PO Number, CRN or SCA name:</td>
<td style="text-align: left;">
<% form_tag :controller => 'subcontracts', :action => 'show_active_subcontracts_results', :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] , :id => 'search_field' %>
</p>
<td>
<p>
Expand Contract Period:
<%= select_tag 'c_end_date', options_for_select([["Current Subcontracts", "1"],["Past 5 Years", "2"], ["All", "3"]],{:mutiple => false}) %>
<%= submit_tag "Update", :name => nil %>
<%= link_to_function "Clear", "$('search_field').clear()" %>
<%= link_to 'Export To Excel',:action=>'active_subcontracts_to_excel', :format=> 'excel' %>
</p>
</td>
<% end %>
</td>
</tr>
</table>
What I want to do is pass the 'c_end_date' and 'search' data to the controller. Not sure if the link_to is the correct way.
Thank you for your help.
Your html is invalid: between the td above your form tag you're starting another td inside the form tag where the opening td hasn't been closed yet. This could be breaking your form. Try taking that td out entirely.