Passing parameter in a Ruby function from HTML - html

I have two files. .rb (with Ruby code) and .erb(HTML file with some ruby script). I am calling a Ruby function in .rb from .erb.
.erb
Click here
.rb
def showProducts(param)
//Some code
end
I am able to call a function without passing parameters to it. But as and when I pass parameters to function and then call it, I receive an error. I know this is the incorrect way to call a parametrized function from .erb. What is the correct way of calling a parameterized function from HTML?

If you add in another key/value pair to the hash in url_for
<%= url_for :action => :showProducts, :product => "toaster" %>
Your URL should go from, say, http://localhost:3000/showProducts to http://localhost:3000/showProducts?product=toaster
Here, we're adding parameters to the GET request. To access these parameters in the controller we use the params hash. For example to get the product (toaster):
params[:product] #=> "toaster"

I found the solution to my problem :
<a href="<%= url_for :action => :showProducts, :id=> 'Hello' %>">
.rb function:
def showProducts(param)
//Some code
end

I found the solution
def showProducts
#params['product']
end

Related

cannot able to pass data from express to ejs?

i tried to pass a data from js file(sample.js) to ejs using res.send("filename.ejs",data) by converting a object into JSON where JSON data is displaying on console, but while trying to pass it showing an error
TypeError: Cannot create property '_locals' on string
can some one help with this and tell me how to call them in ejs file
res.send() is used for send data there is no need to point ejs file
res.send takes array as parameter (res.send([body])) and you can get it in ejs like {{ data }}
for example
NODEJS
res.send({message: 'hello'})
filename.ejs
<div>{{ message }}</div> or <%= JSON.stringify(message) %>
also as Express 5x does not support res.send() method you can use
res.status(200).send({message: 'hello})
(you did not admit you express version)
Note that you should not use the .ejs extension in res.render, despite other answers to your question suggesting that you do so.
When you call res.render('myView'), ejs looks for a template called myView.ejs in a folder called views (which is set as the default folder to use by ejs)
for example:
res.render('myView.ejs',{
data:data,
foo:'foo'
});
ejs will look for a view called myView.ejs.ejs (or it might just fail alltogether).
To access the data on your template, you would do the following:
app.js:
app.get('/somePathHere', function(req, res) {
res.render('myView',{
data:data,
foo:'foo'
});
});
myView.ejs:
<% data.forEach(function(item) { %>
//do something
<% }); %>
<%= foo %>
Note that when accessing a variable, you use the <%= varNameHere %>, and when writing any type of function, you would omit the =.
You're sending data to your view so update your code with this and try
res.render("filename",{
data:"hello"
});
Or you can pass whole data
res.render("filename",{data:data});
And in your ejs file access it like this
<div> <%= data %> <div>

how to extend elixir phoenixframework html form helper

I tryed to extend Phoenix.HTML.Form module of the phoenixframework. My intension is to wrap the html form helper text_input to create a text input field for a timex date value to use it with bootstrap-datepicker.
I'm new to elixir but I read about protocols to extend elixir modules. So I tryed:
defprotocol Phoenix.HTML.Form.Extension do
def timex_date_input(form, field, opts \\ [])
end
defimpl Phoenix.HTML.Form.Extension, for: Phoenix.HTML.Form do
def timex_date_input(%{model: model, params: params}, field, opts \\ []) do
# my logic goes here
form = %{model: model, params: params}
text_input(form, field, opts)
end
end
But, it doesn't work, because: "function text_input/3 undefined". What would be the right solution?
You need to import the Phoenix.HTML.Form module to be able to use text_input - you will find that this is already imported into your views (and your templates since they are functions in your views) in your web.ex file.
If you wish to add a new form function, you can simply define the function (there is no need for protocols - these are often used as a way to extend libraries - phoenix_ecto is a great example of this):
defmodule MyApp.FormHelpers
def timex_date_input(form, field, opts \\ []) do
# my logic goes here
form = %{model: model, params: params}
Phoenix.HTML.Form.text_input(form, field, opts)
end
Then you can either import this into your view (import MyApp.FormHelpers), or use the full function name in your template:
<%= timex_date_input(f, :date, ...) %>

Rails actions and jbuilder, why they have to be as difficult?

Rails drive me crazy. I'm trying to respond to with an action with JSON.
My goal is to let be the JSON the only format for a response to a URL.
Let's see some code.
The Model is a Devise user, with some added field.
The Controller is my UsersController that has this action
# /app/controllers/users_controller.rb
def static
render json: current_user
end
I got also this jbuilder view
# /app/views/users/static.json.jbuilder
json.content format_content(#user.content)
json.author do
json.name #user.name
json.email_address #user.email
end
if current_user.admin?
json.someValue "foo"
end
this View doesn't do some interesting stuff, but It's just a try.
Anyway I'll never get the static.json.jbuildercontent. I always get all Devise user's content as a JSON.
Am I doing something wrong? (or better: where I done the epic fail?)
Anyway found the solution:
# /config/route.rb
get 'my-static-json' => 'mycontroller#static', defaults: {format: :json}
# /app/controllers/mycontrollers_controller.rb
def my-static-json
end
# /app/views/mycontrollers/my-static-json.json.jbuolder
json.content "some static content"
this is only an example but gives have all the information that I needed

Rails controller rendering HTML as plain text, instead of a form

On my site I want a form to be rendered based on what type of data the user is inputing. When I call the controller method in my view, it's outputting tons of HTML where the form should be as plain text starting from DOCTYPE to . I'm using a post form that was previous in place statically to try it out.
Controller:
def feed_form(form_type)
form_type = %w{type_1 type_2}.include?(form_type) ? form_type : 'post'
render "_#{form_type}_form"
end
And the view calling the action:
= feed_form 'post'
And the form:
.feed-form
%h3 News Feed
= form_for(#post) do |f|
= render 'shared/error_messages', object: f.object
.field
= f.text_area :content, placeholder: "Make your new post here..."
= f.submit "Post", class: "post-button"
You are don't supposed to call controller methods from a view.
You must prepare all the data for view rendering (e.g. all required models) in controller method, and put it into #variables.
And then in view you write all your html using already prepared #variables, you can call helpers methods from a view, but not controller's.
Try to put your def feed_form(form_type) code into a helper.
P.S.: and read something about MVC architecture.

Rails routing - how to add .html extension to the routing rule?

I have this rule:
match '*urlnames' => 'home#searching_names'
The URL address looks like website.com/john.html.
The problem is, that in the log I see
Parameters: {"urlnames"=>"john"}
without the .html extension. Text extension is important, I would need to test it in the controller.
I tried to add to the routing rule this part:
match '*urlnames' => 'home#searching_names', :defaults => { :format => "html" }
But still the same, in the log is
Parameters: {"urlnames"=>"john"}
How can I catch the extension in the controller?
You have access to the requested format via request.parameters[:format] or (as a MIME type) via request.format.
However, you can also use a respond_to block:
def show
file = params[:urlnames]
respond_to do |format|
format.html { ... }
format.txt { ... }
end
end
where ... is code to render some text, or send some data or a file.
If you're just trying to show some static files, just place them in the public dir, and bypass Rails entirely.