I'm trying to pass a project ID from its show page to an employee_projects form where I can display the projects name. The error I'm getting is
undefined local variable or method `project_id' for #<EmployeeProjectsController:0xb8bdd58>.
employee_project controller:
# GET /employee_projects/new
def new
puts params[project_id]
#project = Project.find(params[:project_id])
session[:project_id] = #project.id
#employee_project = EmployeeProject.new
end
employee_projects form
Assign worker for this project <%= project.projectName%><br>
<div class="field">
<%= f.label :employee_id %><br>
<%= f.collection_select :employee_id, Employee.all, :id, :empLastName, :prompt => "Select worker" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Projects show
<%= button_to "Assign!", new_employee_project_path(project_id: #project.id) , class: "btn btn-primary", :method => :get %>
Does anyone have any ideas on how to fix this?
In your controller try changing puts params[project_id] to puts params[:project_id].
params is a Hash so you must refer to its keys with symbols, that is :project_id. project_id is just a variable that has no value assigned to it, hence the error undefined local variable or method 'project_id' for #.
UPDATE ("Couldn't find Project with 'id'=" fix)
The button in projects' show view is not sending the parameter project_id even though you are passing it to the new_employee_project_path helper (check your url string and you will see it ending at ? with no parameters). To fix it, i recommend using a regular form instead of button_to helper.
So, instead of this line:
<%= button_to "Assign!", new_employee_project_path(project_id: #project.id) , class: "btn btn-primary", :method => :get %>
Try using this form:
<form action="<%= new_employee_project_path %>" method="get">
<input type="hidden" name="project_id" value="<%= #project.id %>">
<input type="submit" value="Assign!" class="btn btn-primary">
</form>
Related
I want to build a filter with the help of scope in ruby on rails.
In my controller, I have the lines:
def index
#carpools = Carpool.paginate(page: params[:page], per_page: 5)
#carpools = #carpools.filter_by_locationFrom(params[:carpool_from]) if params[:carpool_from].present?
end
And in my model, I have the lines:
scope :filter_by_locationFrom, -> (locationFrom) { where locationFrom: locationFrom }
And in my view (index.html.erb), I want to have a form to type the value of locationFrom, so that the controller will know the params: locationFrom exists and only select the specified data (with the locationFrom value)from the database.
I have tried several form format but they all got some issues. How can I make such form?
Here is the form I tried:
<%= form_with(model: #carpool, method: "POST") do |f| %>
<div class="form-group row">
<%= f.label :locationFrom, class: "col-2 col-form-label text-light" %>
<div class="col-10">
<%= f.text_field :carpool_from, class: "form-control shadow rounded", placeholder: "From" %>
</div>
</div>
<div class="form-group row justify-content-center">
<%= f.submit("filter", class: "btn btn-outline-light btn-lg") %>
</div>
<% end %>
This gives 400 bad request, saying that:
ActionController::ParameterMissing (param is missing or the value is empty: carpool):
app/controllers/carpools_controller.rb:20:in `create'
But my form is inside 'index' page, I don't know why it refers to 'create'
Thanks in advance!
In the controller review_queue I have a custom action that posts a result to a target URL, I want to build a form for this action. I am not going to save any of the fields to the DB I am just going to pass them in the params to the post_review action.
def post_review
RestClient::Request.execute(:method => :post,
:url => Rails.application.secrets['target_url'],
:content_type => :json,
:payload => #result_params.merge!(params[:reasons]).to_json,
:headers => HEADERS)
end
In the view I have a form that will be filled out and on submit it should send up the reasons when the form is submited, I am setting the review_queue_id and the status in the form, since these are static, but the reasons should come from the textarea
<%= form_for(:review_queue, url: { action: 'post_review', :review_queue_id => #review_queue.id, :status => 'accepted'} ) do |f| %>
<div class='form-group'>
<label for='comment'>Please give a reason? (required)</label>
<%= f.text_area(:reasons, placeholder: 'Your commentns ...', rows: 9, class: 'form-control') %>
</div>
<div class='modal-footer'>
<%= f.submit 'Approve', class: 'btn btn-success btn-decission btn-modal-left-side' %>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
<% end %>
error message:
NoMethodError - undefined method `reasons' for #<ReviewQueueApplication:0x007fa7ff7832d8>:
It seems as if rails is assuming the MVC architecture here, and assuming I want to pass the reasons to the review_queue model. there is no reasons column so it's dropping a no method error. Is there a way of specifying that the form is 'temporary' and only getting as far as the controller?
This seems like it should be a simple thing but there is some rails magic happening here.
NoMethodError - undefined method `reasons' for
ReviewQueueApplication:0x007fa7ff7832d8
form_for assumes that you are creating a form for a model object and expects the fields to be present in that specific model's table(in a normal situation).
You should be going with form_tag
<%= form_tag post_review_path, method: :get, :review_queue_id => #review_queue.id, :status => 'accepted'} ) do |f| %>
<div class='form-group'>
<label for='comment'>Please give a reason? (required)</label>
<%= text_area_tag(:reasons, placeholder: 'Your commentns ...', rows: 9, class: 'form-control') %>
</div>
<div class='modal-footer'>
<%= submit_tag 'Approve', class: 'btn btn-success btn-decission btn-modal-left-side' %>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
<% end %>
And in the controller access it like params[:reasons]. Also if you noticed, I've added method: :get to the form_tag as you don't want to save the info to DB
The rails helper form_for is used for forms for rails resources. You want to use the form_tag helper. Search for form_for and form_tag here for more information on these 2 methods.
I want to make a form in which the user can select a number between 1 and 30. I am trying to do something like this:
<%= f.select("#currency.neural_network", "prediction_days", [1..30]) %>
However, I am getting the below error.
Failure/Error: click_link 'Show'
ActionView::Template::Error:
undefined method `merge' for [1..30]:Array
The code for the entire form is:
<%= form_for #currency.neural_network do |f| %>
<%= f.label "Days" %><br />
<%= f.select("#currency.neural_network", "prediction_days", [1..30]) %>
<%= f.submit "Predict", class: "btn btn-primary" %>
<% end %>
What about :
<%= f.number_field(:days, in: 1..30, step: 1) %>
# => <input id="currency_days" max="30" min="1" name="currency[days]" step="1" type="number">
It basically returns you an html input tag of type "number" from 1 to 30 incremented 1 by 1. Is it what you are lookin for ?
Take a look at the documentation for select. Assuming you want prediction_days to be passed to your controller as a value between 1 and 30, and prediction_days is an attribute of a neural_network object, then the following should work for you:
<%= f.select(:prediction_days, options_for_select(1..30)) %>
Background
I am creating a search field in my header. This code in my view:
<% if signed_in? %>
<%=f orm_tag( "/search", method: "get", :class=>"navbar-form navbar-left", :role => "search") do %>
<div class="form-group">
<%=t ext_field_tag :q, nil, :class=>"form-control", :placeholder => 'Domain/IP Search' %>
</div>
<%=b utton_tag "Search", :type=>'submit', :class=> "btn btn-primary" do %>
<span class="glyphicon glyphicon-search"></span>
<% end %>
<% end %>
creates this output:
<form accept-charset="UTF-8" action="/search" class="navbar-form navbar-left" method="get" role="search">
<div style="display:none">
<input name="utf8" type="hidden" value="✓">
</div>
<div class="form-group">
<input class="form-control" id="q" name="q" placeholder="Domain/IP Search" type="text">
</div>
<button class="btn btn-primary" name="button" type="submit">
<span class="glyphicon glyphicon-search"></span>
</button>
</form>
Here is a picture:
The issue
The problem is when I search, I get this in my url:
/search?utf8=✓&q=test&button=
Ideally, I'd like it to be more like:
/search?q=test
tl;dr:
How do I get rid of the button parameter (and hopefully the utf one as well).
Extra info:
routes.rb
resources :search
search_controller.rb
class SearchController < ApplicationController
def index
#results = params[:q]
puts #results.to_s
end
end
search\index.html.erb
<%=#results.to_s %>
EDIT, answer
Adding this because while the accepted answer helped me get to the solution; it didn't have the exact code for my circumstance. I made these changes:
1) routes:
match '/search', to: 'search#index', via: 'post'
2) form in my header:
<%= form_tag search_path, method: "post", :class => "navbar-form navbar-left", :id=> "SearchForm", :role => "search" do %>
<div class="form-group">
<%= text_field_tag :q, nil, :class => "form-control", :placeholder => 'Domain/IP Search' %>
</div>
<%= button_tag "Search", :type=> 'submit', :class=> "btn btn-primary" do %>
<span class="glyphicon glyphicon-search"></span>
<% end %>
<% end %>
GET
The bottom line, as I wrote in another answer, is that you're using GET:
Essentially, when you submit a GET form, it appends the parameters to your URL, whilst a POST form will append the data to the request body (or somewhere hidden).
The difference is just that the GET request expects a response to be returned immediately, and so you have to pass the parameters in the URL to ensure the server knows how to construct it. The best example being if you use a GET request to load a page.
The POST method is used primarily used for forms, where a response is not expected immediately; thus allowing you to keep the params hidden in the request.
Fix
We've actually created a basic search feature here (the actual search doesn't work for some reason, but the live search does)
The way we did it was as follows:
match 'search(/:search)', :to => 'products#search', :as => :search, via: [:get, :post]
This will firstly allow you to access the search action by typing domain.com/search/your-query-here
In terms of submitting via a form, you'll be able to use JQuery to amend the URL with the input from the text field:
#app/views/elements/_nav.html.erb
<%= form_tag search_path, :method => :post, :id => "SearchForm" do %>
<%= text_field_tag :search, params[:search], placeholder: 'Search your favourite products or brands', :autocomplete => :off, :id => 'SearchSearch' %>
<%= image_submit_tag('nav_bar/search.png', title: 'Search', class: 'search_submit', data: { "placement" => "bottom" }) %>
<% end %>
#app/assets/javascripts/application.js
//Search Submit
$(document).ready(function() {
$('#SearchForm').submit(function(e) {
e.preventDefault();
if( $('#SearchSearch').val().length > 0 ) {
var search_params = '/' + $('#SearchSearch').val().toLowerCase();
}else{
var search_params = ''
}
window.location.href = $(this).attr('action') + search_params
});
});
If you are not doing with the Utf8 parameter but, Rails is & but its needed. It's to correct some issues in IE's parameter encoding, This parameter was added to forms in order to force Internet Explorer 5, 6, 7 and 8 to encode its parameters as unicode.Its fixes encodeing issue of IE.
Please refer following link:
What is the _snowman param in Ruby on Rails 3 forms for?
This parameter is a feature of rails.
It was previously the snowman.
It helps IE to really use utf-8.
Avoid using form_tag and it works:
<form action="<%= search_path %>" method="get" >
<%= text_field_tag 'query' %>
<%= submit_tag "Search", :name => nil%>
</form>
Now style your form as you want
The -Form_html.erb, the routes.rb and the create method in the controller are as below. But on form submission, it gives nil class error the moment I use params[:mail_setting]
routes.rb
-----------
resources :mail_settings
the _form.html.erb
---------
<%= form_tag '/mail_settings' do %>
<div class="fieldBlock">
<%= label_tag :name %> <%= text_field_tag :name%> </div>
<div class="fieldBlock">
<%= label_tag :id%> <%= text_field_tag :id%> </div>
<div class="actions fieldBlock">
<%= submit_tag "Update Settings ", :class => "btn-large btn-success" %>
</div>
<% end %>
The create method in controller:
def create
#mail_setting = MailSetting.find_by_user_id_and_name(current_user.id, params[:mail_setting][:name])
if ! #mail_setting.blank?
#mail_setting.update_attributes(params[:mail_setting])
else
#mail_setting = MailSetting.new(params[:mail_setting])
#mail_setting.save
render action: "index"
end
end
Try using params[:mailsetting] without the underscore. The model is named MailSetting, so I assume that would be right. The NoMethod error means that there is no generated helper method by the name of mail_setting, which, since you already made the model, is probably because it's spelled wrong.
I would recommend not using underscores/dashes when naming your resources anyways since it just confuses the grammar.