RoR novice here, appreciate any help.
I have the following error:
undefined method `to_i' for :funding_level:Symbol
Am trying to pass both funding_level and investment_id parameters from link_to into the new method in my controller.
From my understanding, the 100 value gets passed as a Symbol and not an Object, causing problems when I then try to assign it to #funding_level, an integer variable.
Thanks!
HTML:
<div id="investment-status">
<%= link_to "$100", new_project_funding_path(funding_level: 100,investment_id: #project.id), class: 'btn' %>
</div>
Controller:
def new
#investment = Project.find(params[:investment_id])
#funding_offered = :funding_level
#project_funding = current_user.project_fundings.new(investment: #investment, funding_offered: #funding_offered)
end
Shouldn't be this instead ?
#funding_offered = params[:funding_level]
Related
I am trying to use .html_safe in the below description where I receive the error as Undefined method for Nokogiri HTML document.
blogs_controller.rb
#blog = Blog.find(19)
#description = Nokogiri::HTML.parse(#blog.description)
#description.search('a.fr-file').each do |desc|
desc['href']= File.join(ActionController::Base.asset_host, desc['href'])
end
show.html.erb
<p><%= #description.html_safe %></p>
Kindly advise.
html_safe is a Rails method defined on String but not on Nokogiri::HTML.
I would try to translate the Nokogiri document into a HTML first:
<p><%= #description.to_html.html_safe %></p>
I don't understand Hanami, I've created Message model, and I want to pull from DB message by params[:id]. The way we do it in Rails #message = Message.find(params[:id].
I read documentation, and what I did after. My Controller (for show):
def call(params)
#message = MessageRepository.find(params[:id])
end
And my erb:
<%= #message.title %>
But it gives me error:
NoMethodError: undefined method `title' for nil:NilClass
What I did wrong?
At the controller call expose :message, then you can use it in the view or in the template as local variable (without the #).
I am doing a tutorial from Stuck.io on implementing a search field on my admin model, so someone can search for a specific admin. the form is appearing, however when I try to use the search I am getting an error field and I don't know where I've gone wrong.
The Exact error I am getting is:
NameError at /admins
undefined local variable or method `params' for #<Class:0x007f91e39df950>
My custom Devise Admin Controller: (to create an index and show page)
class AdminController < ApplicationController
before_action :authenticate_admin!
def index
#admins = Admin.all
# Search Query
#admins = Admin.search(params[:search])
end
def show
#admin = Admin.find_by_admin_ident(params[:id])
end
end
Routes for my custom Admin controller:
admins GET /admins(.:format) admin#index
admin GET /admins/:id(.:format) admin#show
My Admin Model: Search method
class Admin < ActiveRecord::Base
# Search Functionality
def self.search(search)
if search
#admins = Admin.where(["name LIKE ?","%#{params[:search]}%"])
else
all
end
end
end
And finally my Search form:
<div class="col-md-4">
<div class="info-block">
<%= form_tag admins_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], :class => 'form-control', :id => 'admin-search-field' %>
<center><%= submit_tag "Search Users", :class => 'btn btn-default btn-sm', :id => 'search-btn' %></center>
<% end %>
</div>
</div>
Thanks in advance for your input! Please let me know if you need anything more!
You cannot access the params hash inside the model. This is only accessible from controllers.
Instead of:
#admins = Admin.where(["name LIKE ?","%#{params[:search]}%"])
Use:
#admins = Admin.where(["name LIKE ?","%#{search}%"])
And then in the controller, pass the value as you are doing already:
Admin.search(params[:search])
I am trying to use a form_tag on a Rails application that, when submitted, call an instance of a class on the controller.
View
form_tag '/order', method: :get
select_tag 'type', options_for_select(#options_for_type, #type)
text_field_tag 'numbers', nil, placeholder: "100"
submit_tag "Get Info"
controller
def order
#order = Order.new(params_here).fetch_info
#type = params[:type] || "type1"
#options_for_type = [["type1", "type1"], ["type2", "type2"]]
...
end
This class sends some API requests (JSON).
Now problem is, I am getting an error from the API that some parameters are missing, which is expected since I haven't submitted the form with all the params, but it seems the class is called automatically when the page is loaded. I would have expected the class to be called only when the submit button is pressed?
Can you make the form_tag sort of a block? Like:
form_tag '/order', method: :get do
# All form elements
end
Maybe displaying your route here can also help.
I'm getting the error in the title. I am not sure how exactly to write the routes, controller, and index.
I am trying to create a 'Refresh' button in an index.html.erb view shown below:
...
<td><%= link_to 'Refresh', refreshProfile_affinity_path(a), method: :put %></td>
...
It is using this route in routes.rb:
resources :affinities
put 'affinities/refreshProfile/:id' => 'affinities#refreshProfile'
It is trying to access the following method in the affinities_controller.erb:
...
def refreshProfile
#affinity = Affinity.find(params[:id])
new_profile_affinity = User.find(#affinity.user_A_id).profile_affinity_with(User.find(#affinity.user_B_id))
if #affinity.update_attributes(:integer, new_profile_affinity)
redirect_to #affinity
end
end
...
What could be wrong? Any help is greatly appreciated!
You need to add an as: to your route, this is what specifies the prefix for the helper method and is why you're currently getting an undefined method error e.g.
put 'affinities/refreshProfile/:id' => 'affinities#refreshProfile',
as: 'refreshProfile_affinity'
refreshProfile_affinity_path would then work, or you could give it a shorter, snappier name e.g. as: 'refreshProfile' and then use refreshProfile_path.