argument in form cannot contain nil or be empty Rails 4 - ruby-on-rails-4.1

I have this First argument in form cannot contain nil or be empty issue with the form code of mine
i have defined in the controller as
class AdminController < ApplicationController
def new
#dashboard_user = DashboardUser.new
end
def create
#dashboard_user = DashboardUser.new(dashboard_params)
#dashboard_user.save!
end
def user_creation
end
private
def dashboard_params
params.require(:dashboard_user).permit(:user_name, :password, :last_name, :first_name, :middle_name , :phone)
end
end
and for the form
<%= form_for #dashboard_user, url: "/admin/user_creation", method: "post" do |f| %>
but i am getting this error is there anywhere i did a mistake or i overlooked some basic things?
P.S i am redirecting to the same page after the form has been submitted
routes.rb
Rails.application.routes.draw do
resources :admin, :only => [:create, :new] do
post 'user_creation', on: :collection
get 'user_creation', on: :collection
end
end
rake route
Prefix Verb URI Pattern Controller#Action
user_creation_admin_index POST /admin/user_creation(.:format) admin#user_creation
GET /admin/user_creation(.:format) admin#user_creation
admin_index POST /admin(.:format) admin#create
new_admin GET /admin/new(.:format) admin#new
root GET / admin#new

okay i got the answer i need to use the controller action that corresponds to the view that contains my form .. my form is user_creation so i need to create a function
def user_creation to make that work
def create
#dashboard_user = DashboardUser.new(dashboard_params)
#dashboard_user.save!
end
def user_creation
end
my view name is user_creation so changing the controller as below solved my issue
def create
end
def user_creation
#dashboard_user = DashboardUser.new(dashboard_params)
#dashboard_user.save!
end

Related

Turbo_stream work from controller, but not from view

I'm working on a rails 7 app with hotwire.
I have a form modal, when subimited, turbo should remove html from modal and show flash message.
When i put my turbo code in my controller i works:
# emails_controller.rb
def forward
service = Email::Forward.new(
recipients: params[:recipients].split(','),
sender: current_user.email,
html: params[:html_to_forward]
).call
if service.errors.empty?
flash.now[:success] = t('.success')
else
flash.now[:error] = t('.error')
end
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.update('forward_modal', ''),
turbo_stream.update('flash', partial: 'shared/flash', locals: { flash: flash })
]
end
end
But when i try to put the same code in a forward.turbo_stream.slim view, it do nothing, and i have no error message.
# views/emails/forward.turbo_stream.rb
= turbo_stream.update('forward_modal', '')
= turbo_stream.update('flash', partial: 'shared/flash', locals: { flash: flash })
Any idea about what is happening ?
Maybe a typo... but your code references the file as "views/emails/forward.turbo_stream.rb", but should probably be "views/emails/forward.turbo_stream.slim"? Or perhaps you have both files and have the good code only in the bad file? I've made similar mistakes with "action.html.slim" vs. "action.turbo_stream.slim". Taking a while to get used to the new naming convention.

ASP MVC5 OnActionExecuting RedirectToRouteResult causes infinite loop

I have a filter attribute that checks for a route parameter and if it doesn't exits I apply some logic to check if the current request needs to be redirected to another route.
My code looks like this
Public Overrides Sub OnActionExecuting(filterContext As ActionExecutingContext)
If Not filterContext.RouteData.Values.ContainsKey("SearchKey") Then
strSearchKey = strFrom.ToString.Substring(strFrom.ToString.IndexOf("/") + 1)
strFrom = strFrom.ToString.Substring(0, strFrom.ToString.IndexOf("/") + 1)
If Not IsNullOrEmpty(strSearchKey) Then
Dim values As New Web.Routing.RouteValueDictionary
values.Add("SearchKey", strSearchKey)
values.Add("From", strFrom)
values.Add("controller", "SearchResults")
values.Add("action", "Category")
filterContext.Result = New RedirectToRouteResult("SearchCategory", values)
'filterContext.Result.ExecuteResult(filterContext)
End If
End If
End Sub
This is what I'm doing to get it to redirect but the problem is that it causes an infinite loop and I can't find the reason why it just keeps hitting this same route which is the one I'm redirecting from over and over.

pass request parameter value to class function and return value in classic asp

I am 3 days new to classic asp after a few years of .NET.
I have a simple form (consisting of radio buttons) that collects information and saves it to the a database (think basic survey form).
I get the following error when submitting the form:
Microsoft VBScript runtime error '800a01a8'
Object required: ''
/project/surveypage.asp, line 83
I have the following code on the survey page (snippet)
<%
Dim oConstants
Set oConstants = new Constants
Dim d1NumberOfFF
d1NumberOfFF = oConstants.GetValue(Request.Form("d1NumberOfFF")) '<--- line 83
%>
And the following class (Constants.asp):
<%
Class Constants
'..........
function GetValue(item)
GetValue = 99
If NOT IsEmpty(item) Then
GetValue = item
End if
end function
End Class
%>
It seems basic enough but is acting like the function doesn't exist or that I can't pass the Form value? Beginning to think I've been too babied by .NET
You say you cant pass a querystring value, and you're using Request.Form("d1NumberOfFF") on line 83.
Request.Form only captures form variables (ie from a form using method="post"), Request.Querystring is the equivalent for a querystring value, or you could just use Request and it would collect either kind of variable

Accessing pagination info from liferay search-container in SpringMVC Controller

I am developing a Portlet with Liferay (using liferay-ui in the JSP) and SpringMVC.
I have the following code in my JSP:
<liferay-ui:search-container delta="5" emptyResultsMessage="no books!">
<%
List<Book> bookList = (List<Book>)request.getAttribute("bookList");
List<Book> bookListView = ListUtil.subList(bookList, searchContainer.getStart(), searchContainer.getEnd());
%>
<liferay-ui:search-container-results results="<%= bookListView %>" total="${numberOfBooks}">
</liferay-ui:search-container-results>
...
I'd really like to get rid of the Java Code Block in the JSP and have the bookListView as model attribute just like numberOfBooks in the above code.
However, I can't find a way to access searchContainer from the Spring Controller to get the start and end of the pagination...
Any ideas? Thx!
This might work for you:
SearchContainer<Book> searchContainer = new SearchContainer<Book>(renderRequest, renderResponse.createRenderURL(), null, "there are no books");
Or else,
You can get the parameters from request: delta=20 & cur=2
where cur is the current page which is requested and delta is the total number of items on a page.
With this you can calculate the start (0,20,40, ...) and end (19,39,59, ...) as does liferay's SearchContainer with this method:
private void _calculateStartAndEnd() {
_start = (_cur - 1) * _delta;
_end = _start + _delta;
_resultEnd = _end;
if (_resultEnd > _total) {
_resultEnd = _total;
}
}
Create a suitable SearchContainer in your controller and add it to your model. As Prakash K already said this SearchContainer could look like this:
SearchContainer<Book> searchContainer = new SearchContainer<Book>(renderRequest, renderResponse.createRenderURL(), null, "there are no books");
Because of the two parameter renderRequest and renderResponse you can not use the #ModelAttribute annotation to add the SearchContainer as a model attribute.
Then the JSP can be written like this:
<liferay-ui:search-container searchContainer="${model.searchContainer}" delta="${model.searchContainer.delta}" deltaParam="books_delta">
<liferay-ui:search-container-results results="${model.searchContainer.results}" total="${model.searchContainer.total}"/>
<liferay-ui:search-container-row
className="Book"
keyProperty="primaryKey"
modelVar="book">
...
</liferay-ui:search-container-row>
<liferay-ui:search-iterator searchContainer="${model.searchContainer}"/>
</liferay-ui:search-container>
The attribute deltaParam can be used to configure the used URL parameter

How could I render just the form from an autoform?

I'm in the context of a BrowserView. I want to render a form inside the page.
class CommandeView(BrowserView):
...
def render_myform(self):
livraison_form = LivraisonForm(self.context, self.request)
livraison_form.update()
return livraison_form.render()
class ILivraisonForm(interface.Interface):
livraison = schema.Choice(title=_(u"Mode de livraison"),
vocabulary=livraison_vocabulary)
class LivraisonForm(AutoExtensibleForm, form.EditForm):
schema = ILivraisonForm
class LivraisonFormAdapter(object):
component.adapts(ICommande)
interface.implements(ILivraisonForm)
def __init__(self, context):
self.context = context
self.livraison = self.context.livraison
I would like render_myform to render only the form but it return a HTML Plone page.
Any tips are welcomed.
Haven't checked this in-depth, but the form is probably wrapped by the plone FormWrapper class.
Look for a .form attribute on the LivraisonForm instance:
def render_myform(self):
livraison_form = LivraisonForm(self.context, self.request).form
livraison_form.update()
return livraison_form.render()
It may be you need to add an additional interface to the request for this to work (the wrapper normally does this for you. I'm a little low on time right now, so you get this untested:
from plone.z3cform import z2
class CommandeView(BrowserView):
def render_myform(self):
z2.switch_on(self) # Apply layer interface, set up locale, add `getURL` method
livraison_form = LivraisonForm(self.context, self.request).form
livraison_form.update()
return livraison_form.render()
The Plone Knowledgebase uses this same setup to show a form in a viewlet.

Resources