The Mysterious Order-Dependent Checkbox State Array Controller Transmission Anomaly - ruby-on-rails

Here's all of the relevant view code:
<%= form_for(:object) do |form| %>
<p id="batch_ops">
<b>Objects</b>
<%= submit_tag "Do This" %>
<%= submit_tag "Do That",
:confirm => "Really do that?" %>
<p id="capture" float="left">
<%= button_to_function "Operation_1", \
"Element.remove('batch_input_area'); Element.show('get_op1_params')" %>
<%= text_field_tag "op1_save_path", "_op1_full_dir_path_" %>
<%= button_to_function "Operation_2", \
"Element.remove('batch_input_area'); Element.show('get_op2_params')" %>
<%= text_field_tag "op2_save_path", "_op2_full_dir_path_" %>
</p>
<div id="batch_input_area" style="display:none;">
</div>
</p>
<ul id="object_list">
<% #objects.each do |c| %>
<li id="current_object" style="horizontal-align: left" width="auto"><b>
<%= hidden_field_tag('seen_objects[]', c.id) %>
<%= check_box_tag 'chkd_objects[]', c.id, c.object_checked? %>
<%= c.object_address %>
<% if c.data1? %>
<%= c.data1 %>
<% else %>
<%= "_____________" %>
<% end %>
<% end %>
</ul>
<div id="get_op1_params" style="display:none;">
<%= form_tag(
:action => 'index',
:remote => true,
:html => {:id => 'op1_form'}
) do %>
Options: <%= text_field :op1params, :op1_params, :value => 'freq=30 run=1 stop=0' %>
<%= submit_tag 'Op1 Run', :confirm => 'Run Operation_1 test on checked objects?' %>
<%= submit_tag 'Op1 Status' %>
<%= submit_tag 'Op1 Stop', :confirm => 'Stop Operation_1 test on checked objects?' %>
<%= link_to "Cancel", {:action => "index"} %>
<% end %>
</div>
<div id="get_op2_params" style="display:none;">
<%= form_tag(
:action => 'index',
:remote => true,
:html => {:id => 'op2_form'}
) do %>
Options: <%= text_field :op2params, :op2_params, :value => 'freq=30 run=1 stop=0' %>
<%= submit_tag 'Op2 Run', :confirm => 'Run Operation_2 test on checked objects?' %>
<%= submit_tag 'Op2 Status' %>
<%= submit_tag 'Op2 Stop', :confirm => 'Stop Operation_2 test on checked objects?' %>
<%= link_to "Cancel", {:action => "index"} %>
<% end %>
</div>
Now here's the twist: Over on the controller, the Operation_1 handler gets the checkbox presence array seen_objects[] and the activated elements of the checkbox state array chkd_objects[] while the Operation_2 handler does not. If I reverse the order of the get_op1_params and get_op2_params form_tag divs shown last above then the Operation_2 handler gets these checkbox arrays but the Operation_1 handler does not.
My question is simply: Why?
I've long since lost count of the different things I've tried to get around this, which included additional hidden_field_tags within the form_tags. Ultimately I gave up and used a far less elegant submit_tag with a text_field_tag in place of the Operation_2 button_to_function and its associated form_tag but still do not understand why I could not make both of these operations work using button_to_function as illustrated above.
If someone can get this to work, please post your solution here. Any suggestion short of a working solution I've probably already seen and tried.

Related

how to Edit a post in rails and display said post in the text_field

I am trying to build a page that will host the content of the post from the psql data base and allow user to edit the content of that post inside the text field
I am getting a no method error however message is the correct column in database
any help is very appreciated
<h1>Edit message</h1>
<%= form_with(modle: #post, local: true) do |form| %>
<p>
<%= form.label :message %><br>
<%= form.text_field :message, :value => form.message %>
</p>
<p>
<%= form.submit %>
</p>
<%end%>
<%= link_to 'Back', posts_url %>
<h1>Posts shown below</h1>
<p>To add a new post click link:
<%= link_to new_post_path do %>
New post
<% end %>
</p>
<p>---------------------------------------------</p>
<% #posts.reverse_each do |post| %>
<div id = "<%=post.id %>">
<p><%= post.message %></p>
<p><%= post.created_at.strftime(" Posted at %a %I:%M%p") %></p>
<p><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
:method => :delete %></td>
<p> <%= link_to 'Update', edit_post_path(post) %> </p>
<p>---------------------------------------------</p>
</div>
<% end %>
It should be form.object.message, make changes in this line
<%= form.text_field :message, :value => form.object.message %>
or as max said, just remove the value option all together, it will still work
<%= form.text_field :message %>
Give it a try!
<h1>Edit message</h1>
<%= form_with(model: #post, local: true) do |form| %>
<p>
<%= form.label :message %><br>
<%= form.text_field :message, :value => Post.find(params[:id]).message %>
</p>
<p>
<%= form.submit %>
</p>
<%end%>
<%= link_to 'Back', posts_path %>

Pre select form input based on params

I have a button on an index page that links to a new_assignment_path
<% #users.each do |user| %>
<tr>
<td><%= link_to user.name, user %></td>
<td><%= link_to 'Assign to Class', new_assignment_path, :class => 'btn btn-mini' %></td>
</tr>
<% end %>
And I want it so that when you click on it, it takes you to the new_assignment_path, and takes the dropdown select on that pages form.
<%= simple_form_for(#assignment) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :user_id, collection: User.all.collect, as: :select %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
And have the drop down automatically set to the user.id of whatever user the button was inside of.
<%= link_to 'Assign to Project', new_assignment_path(#assignment, :user_id => user.id), :class => 'btn btn-mini' %>
and
<%= link_to 'Assign to Project', new_assignment_path(:user_id => user.id), :class => 'btn btn-mini' %>
And neither of them worked.
What is the best way of doing this?
With your second option try setting the instance variable in the AssignmentsController.
#user_id = params[:user_id]
Then you need to specify the default in the form
<%= f.input :user_id, collection: User.all.collect, selected: #user_id %>
You don't need as: :select with Simple Form.

blank comment entry with rails

I'm trying to add ability to add comments to projects within a "todo" type app and have run into a problem.
I've created a project with comments before and never run into this problem, but basically rails is drawing an empty comment on the project page.
I've tried a few if statements without any luck, does anyone see my problem ?
<% #project.comments.each do |comment| %>
<div class="commentBlock"><strong><%= comment.posted_by %> says:</strong>
<%=raw comment.comment %>
<small><i class="icon-remove"></i> <%= link_to 'Delete', [comment.project, comment],:confirm => 'Are you sure?',:method => :delete %></small></div>
<% end %>
<h3>Leave a comment</h3>
<%= form_for([#project, #project.comments.build]) do |f| %>
<div class="field">
<%= f.hidden_field :posted_by, :value => current_user.username %>
</div>
<div class="field">
<%= f.label :comment %><br />
<%= f.text_area :comment, :class => "tinymce" %><%= tinymce %>
</div>
<p><%= f.submit :class => 'btn' %></p>
<% end %>
Answer was an error in my controller for Projects, referenced #comment like so:
#comment = #project.comments.build(params[:comment]) by accident!!
Changed to:
#comment = #project.comments
And all works as it should :P
thanks for your help, bad "end of the day" error right there :P

Rails how to update image? And how to do RESTful routing?

I am trying to update my photographer model. But how problems with Rails routing and paperclip wont replace image.
When I submit my form the url is: http://localhost:3000/admin/photographers/save.75 which gives an error. Couldn't find Photographer without an ID
And my image is not updated.
My form:
<%= simple_form_for #photographer, :url => save_admin_photographers_path(#photographer), :html => {:multipart => true, :method => :post} do |f| %>
<%= javascript_include_tag "tiny_mce/tiny_mce" %>
<%= javascript_include_tag "init_my_tiny_mce" %>
<%= f.input :name, :label => 'Name' %>
<%= f.text_area :text, :label => 'Text', :size => '12x12' %>
<%= f.file_field :image, :label => 'Image' %>
<% if #photographer %>
<% if #photographer.image %>
<p class="ok">
<label for="dd">image ok</label>
<%= image_tag("http://s3-eu-west-1.amazonaws.com/kjacobsen/photographer/image/#{#photographer.id}/#{#photographer["image"]}", :size => "39x22") %>
</p>
<p>
<label for="sdd"> </label>
<%= link_to("remove", {:action => "remove_image", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
<label for="dd"></label>
No image uploaded for movie
</p>
<% end %>
<br />
<% end %>
<br />
<%= f.file_field :flv, :label => 'Upload FLV' %>
<br />
<% if #photographer %>
<% if #photographer.flv %>
<p class="ok">
<label for="dd">flv: <%= #photographer["flv"] %></label>
<%= link_to("remove", {:action => "remove_flv", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
No flv uploaded
<br /><br />
</p>
<% end %>
<% end %>
<br />
<%= f.file_field :quicktime, :label => 'Upload Quicktime' %>
<br />
<% if #photographer %>
<% if #photographer.quicktime %>
<p class="ok">
<label for="dd">quicktime: <%= #photographer["quicktime"] %></label>
<%= link_to("remove", {:action => "remove_quicktime", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
<label for="dd"></label>
No quicktime uploaded
<br />
</p>
<% end %>
<% end %>
<%= f.button :submit, :value => 'Create movie' %>
<% end %>
My update controller:
def save
#photographer = Photographer.find(params[:id])
#photographer.update_attributes(params[:photographer])
if !#photographer.save
flash[:notice] = " "
render_action 'edit'
else
flash[:notice] = "update ok"
redirect_to :action => 'edit', :id => #photographer.id
end
end
My routes:
namespace :admin do
resources :photographers do
collection do
post :save
end
end
end
What you're doing is basically an update action. The update route is automatically created when you do resources :photographers, you can verify this by typing rake routes in the terminal.
You should rename your controller action from save to update and remove the custom route:
namespace :admin do
resources :photographers
end
Then use the update route in your form:
:url => admin_photographer_path(#photographer)
You should also change your html method, the update action uses PUT:
:method => :put
Hope this helps.

ruby on rails select not defaulting to current value

I'm currently using a select as follows (within a form):
<% form_for :search, :url => search_path, :html => {:method => :get} do |f| %>
<%= select('search', :type, options_for_select(['Artist', 'Track'])) %>
<%= f.text_field :query %>
<% end %>
This works, but when I perform a search, it defaults back to artist even if the user selected Track before searching. How can I correct this?
Automatically selecting the current value works for radio buttons:
<% form_for #search, :url => search_path, :html => {:method => :get} do |f| %>
<p class="radio_button">
<%= f.label :type_track, 'Search tracks' %>
<%= f.radio_button :type, 'Track' %>
</p>
<p class="radio_button">
<%= f.label :type_artist, 'Search artists' %>
<%= f.radio_button :type, 'Artist' %>
</p>
<p class="text_field">
<%= f.label :query, 'Search query' %>
<%= f.text_field :query, :class => 'auto_focus' %>
</p>
<p class="submit">
<%= submit_tag 'Go' %>
</p>
<% end %>
Any help getting this to work will be greatly appreciated!
You must use the select form builder:
<%= f.select(:type, [["text1", "value1"], ["text2", "value2"], ...]) %>

Resources