I am new to rails. I can't understand what the error is and how to fix it.
Error is param is missing or the value is empty: students
form_for helper`
<%= form_for :students, :url=>{:action=>'create'} do |f| %>
Strong parameters
params.require(:students).permit(:s_name,:batch,:roll_no,:branch,:gender,:date_of_birth,:contact_no_1,:contact_no_2,:email,:spi_1,:spi_2,:spi_3,:spi_4,:spi_5,:spi_6,:spi_7,:cpi_7,:percent_10,:percent_12)
end
If you will inspect the params you'll find that your parameters are not in this form
{students: {'s_name': 'xyz', 'batch': 'cse'}}
It means the previous answer which was explained by #puneet is the best way to make CRUD's Try that way.
Try below code:
students controller
def new
#student = Student.new
end
def create
#student = Student.new(student_params)
#student.save
end
def student_params
params.require(:students).permit(:s_name,:batch,:roll_no,:branch,:gender,:date_of_birth,:contact_no_1,:contact_no_2,:email,:spi_1,:spi_2,:spi_3,:spi_4,:spi_5,:spi_6,:spi_7,:cpi_7,:percent_10,:percent_12)
end
view
<% form_for #student do |f| %>
...
<% end %>
Related
I am trying to drill down some searches in my app using time intervals....posts in the last day, last week, last month. I am trying to be DRY and write a search controller method that will handle this for me. I am having probelms passing in the time variable. See the form below. #search_term is the search term and hidden becasue for this form as an example, all the client sees is a link saying "last 24 hours"
<%= form_tag time_searches_path do %>
<%= hidden_field_tag :search_task, #search_term %>
<%= hidden_field_tag :time, "24" %>
<%= hidden_field_tag :Klass, Micropost %>
<%= submit_tag "Last 24 hours", class: 'Criteria'%>
<% end %>
Then I have a helper method that is included in my search controller.....
def get_time_interval time
if time == "24"
#time_variable = 1.day.ago
elsif time == "week"
#time_variable = 1.week.ago
elsif time == "month"
#time_variable = 1.month.ago
end
end
Then I add the time_variable into my controller action
def search
model = params[:Klass]
klass = model.constantize
time = params[:time]
get_time_interval time
#search = Sunspot.search [klass] do
fulltext params[:search_task]
with(:created_at).greater_than(#time_variable)
end
end
Clearly this is wrong. I assuming i need some form of metaprogramming. I am new to rails to don't really know how to proceed.
Error I get is
undefined method `gsub' for nil:nilclass
And it highlights the line with the time variable in the controller
I have rails 4 app with an Events model that I want to make searchable on a variety of fields using the Ransack gem.
my controller action:
def index
#search = Event.ransack(params[:q])
#events = #search.result(distinct: true)
end
my search form in index.html.erb:
<%= search_form_for #search do |f| %>
<%= f.text_field :name_cont, class: "radius-left expand", placeholder: "Event Name" %>
<% end %>
However, in loading the page, I get the following error:
ActionView::Template::Error (undefined method events_path' for #<#:0x007fc08b3fa838>): 1: Events 2: 3: <%= search_form_for #search do |f| %> 4: <%= f.text_field :name_cont, class: "radius-left expand", placeholder: "Event Name" %> 5: <% end %> 6: app/views/events/index.html.erb:3:in_app_views_events_index_html_erb___2056451196739971413_70232473324180'
when I rake routes, I get:
events_show GET /events/show(.:format) events#show
events_index GET /events/index(.:format) events#index
so i assume if I could get this form to point to events_index_path, it would work as expected.
what do I need to do to specify the correct path for the form?
Tbh i am not sure what i did to get this working. here is my controller index action now, which includes a distance/location search feature:
def index
if params[:within].present? && (params[:within].to_i > 0)
#search = Event.near(params[:location],params[:within]).search(params[:q])
else
#search = Event.search(params[:q])
end
#events = #search.result
end
I'm using form for and the name of the form in the controller is appearing as "#", but I expect it to appear as :edge.
Here's my controller:
def new
#some_stuff
#edge = Edge.new
#some_stuff
end
def create
#edge = Edge.new(edge_params)
#edge.save
end
def edge_params
params.require(:edge).permit(:location_1_id, :location_2_id)
end
View:
<%= form_for( :edge, :url => {:action => 'create'}) do |f| %>
<ul>
<li>
<%= f.label :location_1_id %>
<%= collection_select(#edge, :location_1_id, #location, :id, :record_as_string) %>
</li>
<%= submit_tag(t(:create_edge)) %>
</ul>
<% end %>
Param req:
{"utf8"=>"✓", "authenticity_token"=>"blah",
"#"=>{"location_1_id"=>"91", "location_2_id"=>"92"},
"commit"=>"Create Edge", "action"=>"create",
"controller"=>"admin/edges", "floor_map_floor_id"=>"1"}
So the name of the parameter should be :edge but it's an object that I can't access.
Can anybody tell me what I'm missing?
Any help is appreciated.
You should be using the form block on your collection set like so
<%= f.collection_select(:location_1_id, #location, :id, :record_as_string) %>
(notice that you are calling the collection_select on the block variable f, and NOT passing in the #edge as the first argument).
In addition, because you are creating the object in new (#edge = Edge.new), you should just be using in your form, like so
<%= form_for( #edge, :url => {:action => 'create'}) do |f| %>
(although using :edge wasn't the cause of your problems, I suspect it was because you were using :edge and #edge in the same form. You need to be consistent, use one or the other)
If Project has_many Genre through: GenresProject
and params are:
def project_params
params.fetch(:project, {}).permit(genres_projects_attributes: [:id, {genre_id: []})
end
and my submit form is:
<%= form_for #project do |f| %>
<%= f.fields_for :genres_projects_attributes do |ff| %>
<%= ff.select :genre_id, Genre.order(:id).collect{|g| [g.name, g.id]}, {}, { multiple: true } %>
<% end %>
<%= f.submit 'Update'%>
<% end %>
and my params look like this:
"project"=>{ ... "genres_projects_attributes"=>{"genre_id"=>["3", "5"]}} ... }
Should
project.update(project_params)
Automatically iterate through the genre_id array and create the appropriate GenresProject records?
If you don't have GenreProject record, then you will not get an id from the params.
Secondly, it will not automatically create the new Genre record or update one for you, I think. You should handle creating new record in the update action yourself. For example:
params[:project][:genre_id].each do |id|
unless GenreProject.find(id)
# create new record here
GenreProject.create
end
# other updating operations
end
Hope it works for you
I have the following:
<% #users.each do |user| %>
<% if user.profile %>
<%= link_to user do %>
<h2><%= user.profile.first_name %> <%= user.profile.last_name %></h2>
<% end %>
<% end %>
<% end %>
The above code works fine. What this code does is that it will output the first and last names of every user. These names are clickable and will take me to that user's page. My main issue is with the 3rd line. The issue I am having is that I am trying to get rid of the link underline, but I am unsure as to how to pass a class into it. Below is my attempt. My class "no-text-dec" is just one line of "text-decoration: none;"
<%= link_to (user, class: "no-text-dec") do %>
I'm new to Rails, but I understand that link_to has a body, url options, and then html options in that specific order, but how can I make it work in this case? The above line makes my application is crash, but it's the only thing I can think of that makes sense. I'm assuming it's because I am not giving it its body argument, but I'm not sure what that would be.
This should work fine if user contains url/path correct
<%= link_to(user, class: 'some_class') %> do
<span>Delete</span>
<% end %>
The space after method in sending argument in helper method link_to is crashing your application
you can give a try at irb
def test(a,b)
puts a; puts b;
end
test ("Ad","Cd")
It should throw an error