Select tag rails - ruby-on-rails

I have a view where I need to show a list of pages (I have a model called Page) and then i need the id of the selected page
I have this code in the view
مهمة جديدة
: <%= form_for :task, :url => {:action=>"create", :controller=>"tasks"}, :html => {:class :
=> "nifty_form"} do |f| %>
<% if #task.errors.any? %>
<div id="error_explanation">
<h2>: عذرا لم نستطع استكمال طلبك</h2>
<ul>
<% #task.errors.full_messages.each do |msg| %>
<li style="color:red;"><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "اسم المهمة" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label "وصف المهمة" %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<button><%= link_to 'العودة الى قائمة المهام', project_tasks_path %></button>
<%= f.submit "مهمة جديدة"%>
</div>
<%= select_tag(:page, options_for_select(['a',1])) %>
<% end %>
#pageslist is a 2d array of the form [['pagename', 1], ..]
The question is how can i access the page_id of the selected page in the controller ?
I tried params[:p_id] but it is not working any help please?
This is what I have in the controller:
#task = Project.find(params[:project_id]).tasks.new(params[:task])
#task.update_attributes({:page_id => params[:p_id]})

Assuming that #pagelist is in a format as you described, then in your view:
<%= select_tag(:page, options_for_select( #pagelist )) %>
You had missed it in your example, and provided only 1d array for options_for_select.
With such prepared view your create action will receive page id in:
params[:page]
it's because you had passed :page as a first argument for select_tag.

Related

setting a select tag on Ruby on Rails

<%= form_tag("/posts/create") do %>
<div class="form">
<div class="form-body">
<% #post.errors.full_messages.each do |message| %>
<div class="form-error">
<%= message %>
</div>
<% end %>
<div class="continent">
<label>continent<br>
<%= #post.continent %>
<%= select :continent,
[["Africa","AF"],["North America","NA"],["Oceania","OC"],["Asia","AS"],["Europe","EU"],["South America","SA"]],
:prompt => "Select" %>
</label>
</div>
<div class="country">
<label>country<br>
<%= #post.country %>
<%= select :country,
[["Japan","JP"],["China","CH"]],
:prompt => "Select" %>
</label>
</div>
<div class="title">
<label>title<br>
<textarea name="title"><%= #post.title %></textarea>
</label>
</div>
<input type="submit" value="POST">
</div>
</div>
<% end %>
I want to set a select tag for choosing user's continent and country in the form
But It doesn't work well
I've tried some way to solve this problem.
And I just got the shape which is like "Select tag" but it's only included "prompt"
Please give me some advice.
FormOptionsHelper`s select takes these args
select(object, method, choices = nil, options = {}, html_options = {}, &block)
Try explicit putting options like this:
select :post, :country, [["Japan","JP"],["China","CH"]], { :prompt => "Select" }, { other_html_options }
If you are in a form context make this
<%= form_tag("/posts/create") do |form| %>
<%= form.select :country, [["Japan","JP"],["China","CH"]], { :prompt => "Select" }, { other_html_options } %>
<% end %>
First start off by creating the correct routes:
resources :posts
In Rails you create a resource by sending a POST request to the collection path ('/posts').
Then create the form with form_for(#post) or form_with(model: #post) (Rails 5.1+) and bind the model instance to the form builder.
<%= form_for(#post) do |f| %>
<div class="form">
<div class="form-body">
<% f.object.errors.full_messages.each do |message| %>
<div class="form-error">
<%= message %>
</div>
<% end %>
<div class="continent">
<%= f.label :continent do %>
<%= f.select :continent,
[["Africa","AF"],["North America","NA"],["Oceania","OC"],["Asia","AS"],["Europe","EU"],["South America","SA"]],
prompt: "Select" %>
<% end %>
</div>
<div class="country">
<%= f.label :country do %>
<%= f.select :country,
[["Japan","JP"],["China","CH"]],
:prompt => "Select" %>
<% end %>
</div>
<div class="title">
<%= f.label :title do %>
<%= f.text_area_field :title %>
</div>
<%= f.submit %>
</div>
</div>
<% end %>
This lets you reuse the same form for updating the resource. Also when you use the input helpers rails will properly name the inputs so that you can whitelist them with:
params.require(:post).permit(:continent, :country, :title)

How do I use select_year with form_for?

I have a form as follows:
<%= form_for(#car, :html => {:class => 'form-horizontal' }) do |f| %>
<% if #car.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#car.errors.count, "error") %> prohibited this car from being saved</h2>
<ul>
<% #car.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :model_year %><br />
<% %>
<% end %>
</div>
I want to have a collection select for the model year starting 100 years back and going to a year in the future. I have tried
<%= f.select_year :model_year%>
but it says that select_year is not a valid method. Tried
<%= f.date :model_year, :discard_month => true %>
also to no avail. Any thoughts?
select_year is a helper that generates a full select tag with options. Since you're using form_for instead of form_tag, you'll want to use a helper that can be called on a form builder object.
<%= f.select :model_year, (Time.zone.now.year - 100)..(Time.zone.now.year + 1) %>
Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_year

Rails: How to make a form post to another controller action

I know you are usually supposed to use the linking between new/create and edit/update in rails, but I have a case where I need something else. Is there anyway I can achieve this same connection?
I have a form for a model and I want it to post the data (similar to how a new view, post to the create action).
Here is my form
<div id="new-job">
<%= form_for(#job) do |f| %>
<% if #job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% #job.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit "create job", id: "submit-job", class: "button small radius" %>
<%= link_to "go back", jobs_path, class: "button small radius secondary" %>
<div id="new-job-errors"> </div>
</div>
<% end %>
</div>
Use the :url option.
= form_for #job, :url => company_path, :html => { :method => :post/:put }

Setting a value sent from a link_to a partial in another view

So i have this link from a partial in a view:
<td><%= link_to 'Inscribir Ejemplares en la Carrera', home_carreras_path(#race, :meeting_id => meeting.id) %></td>
That link sends me to another view where two other partials are rendered.
The browser address looks like this:
http://0.0.0.0:3000/home/carreras?meeting_id=6
So you see that the value i sent from the link is there, and i want to pass that value, that number to one of the partials rendered in that view.
i haven't found how to do that, i searched a lot before asking. So please i hope someone here can help me. Thank you.
EDIT: ADDING MORE INFO
Controller:
class HomeController < ApplicationController
def index
render :layout => 'principal'
end
def carreras
#meeting_id = params[:meeting_id]
render :layout => 'principal'
end
end
Not much action there :/
The Partial where the hidden field is:
<%= form_for(race) do |f| %>
<ul>
<% race.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.hidden_field :meeting_id %><br />
</div>
<div class="field">
<%= f.label :carrera_num %><br />
<%= f.text_field :carrera_num %>
</div>
<div class="field">
<%= f.label :caballo_num %><br />
<%= f.text_field :caballo_num %>
</div>
<div class="field">
<%= f.label :caballo_nombre %><br />
<%= f.text_field :caballo_nombre %>
</div>
<div class="field">
<%= f.label :distancia %><br />
<%= f.text_field :distancia %>
</div>
<BR>
<div class="actions">
<%= f.submit "Inscribir Ejemplar" %>
</div>
<% end %>
If your controller actions for carreras, you can set an instance variable for the meeting_id.
def carreras
#meeting_id = params[:meeting_id]
# rest of controller action
end
#meeting_id will then be available to you throughout your rendered action.
You can supply your hidden_field tag with an options hash to set the value.
<%= hidden_field :meeting_id, value: #meeting_id %>

Rendering one models 'edit form' in anothers 'index view'

I have two models, Teams and Players. On the teams index page I have a list of players that aren't assigned to a team. I'm trying to make a button so that I can click on one of the players with no team and have the 'edit form' of this player show up on the team index page.
This is my current team#index:
= link_to 'New Team', new_team_path
= link_to 'New Player', new_player_path
#teamLists
- #teams.each do |team|
.team
.teamtitle
.teamname
= link_to truncate(team.name, length: 18), edit_team_path(team)
.teammoney
= number_to_currency(team.adjust_money, precision: 0)
%table
%tr.tableheading
%th.namecolumn Player
%th.poscolumn Pos
%th.pricecolumn $
-team.players.each do |player|
%tr
%td.namecolumn= player.name
%td.poscolumn= player.position
%td.pricecolumn= player.price
-(1..(10-team.players.length)).each do |x|
%tr
%td ---
=render template: 'players/edit'
=render 'players/playerlist'
and this is my player#edit
%h1 Nominated Player
= render 'players/form'
= link_to 'Show', #player
= link_to 'Back', players_path
and the players/form
<%= form_for(#player) do |f| %>
<% if #player.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#player.errors.count, "error") %> prohibited this player from being saved:</h2>
<ul>
<% #player.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :position %><br />
<%= f.text_field :position %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.number_field :price %>
</div>
<div class="field">
<%= f.label :team_id %><br />
<%= f.select :team_id, Team.all.map { |team| [team.name, team.id] }, { :include_blank => true } %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
At the moment I get this error 'undefined method `model_name' for NilClass:Class' I think its because the form doesn't have access to #player which is defined in the players edit action. Is there a way I can get this to work somehow?
You can reference any partial from another view page, and that's fine. However, like in your case, if that partial you require needs some instance variables (like #player) you'll have to either: A) declare it in the controller of Teams, or B) pass it in to the partial.
So for A), in your Teams controller for action index, just add #player = Player.new or whatever you need it to be.
For B), do:
render :partial => "my_partial", :locals => {:player => Player.new}

Resources