Passing Parameters to Erb View

Passing parameters to erb view

just pass the :locals to the erb() in your routes:

get '/hello/:name' do
erb :hello, :locals => {:name => params[:name]}
end

and then just use it in the views/hello.erb:

Hello <%= name %>

(tested on sinatra 1.2.6)

Passing multiple param values from view to controller in ERB form

Remove your own <select> tag and try using rails way like this

 <%= select_tag :species, options_for_select(Species.all.collect{|sp| [sp.name, sp.id]}, params[:species]), {prompt: "Select Species"} %>

Perhaps like the comment above suggested. Fetch all the species details you need in an array in controller action like this

@species_names = Species.pluck(:name, :id) #as per suggestion give in comment below

OR

@species_names = Species.all.collect{|sp| [sp.name, sp.id]}

and use that like this in view

<%= select_tag :species, options_for_select(@species_names, params[:species]), {prompt: "Select Species"} %>

This will much better approach.

Pass variable between views (erb) using sinatra

The local variable is passed correctly to the partial view. So, in the partial view, it will be available as a normal local variable -- test. You should not use params to access them, the data it contains are different from the local variables available in a view.

So you should use it just like how you use a normal local variable :

view2.erb

<%= test %>

How to pass parameter in .js.erb render file and get

Set your message as an instance variable exactly as you would in a .html.erb view:

if @user.user_settings == nil
@msg = "Your account settings saved successfully"
else
@msg = "Something goes wrong. Please try again."
end

render "save_account_settings"

And in your js.erb view:

$('#saved_message').text('<%= @msg %>');

Note: params in the controller and the view will always be the parameters of the request. When you use :locals to pass variables to a view for rendering, they will be available as local variables in the view i.e. just msg not params[:msg]. The latter would be trying to get the msg parameter from the request i.e. ?msg=value-from-query

Ruby templates: How to pass variables into inlined ERB?

Got it!

I create a bindings class

class BindMe
def initialize(key,val)
@key=key
@val=val
end
def get_binding
return binding()
end
end

and pass an instance to ERB

dataHash.keys.each do |current|
key = current.to_s
val = dataHash[key]

# here, I pass the bindings instance to ERB
bindMe = BindMe.new(key,val)

result = template.result(bindMe.get_binding)

# unnecessary code goes here
end

The .erb template file looks like this:

Key: <%= @key %>

Rails - render a view in a view - pass params

The approach taken here should be to use nested forms, accepts_nested_attributes_for with allow_destroy: true and reject_if for the validations.

  • to destroy nested records, use accepts_nested_attributes_for :tags, allow_destroy: true and ensure that (besides :id) also :_destroy
    is added to strongparams
  • to use validations for nested fields, use
    accepts_nested_attributes_for :tags, allow_destroy: true,
    reject_if: :something_to_check

need to pass parameter from controller to .html.erb file

In general the way variables from the controller get passed to the views is by assigning them as an instance variable, which are variables defined with an @ before it, e.g @products. If you want the product variable to be passed along, it has to be set as @product, like so:

@product = @products.find ...

In this particular case, you are getting an error raised before your view even is called. The only line in your controller action is throwing this.

Make sure that @products is actually being set. It's not being set in the #description method, so is it being set in a before_filter here? Is @products able to call #find? In Rails we often only see a model calling this method, e.g Product.find(params[:id]).

Further more, I see that you're accessing your params hash with regular brackets instead of square brackets. In Ruby you use square brackets. This alone may be what's causing the error. Try changing that to:

@product = @products.find(params[:id])


Related Topics



Leave a reply



Submit