Select options with new lines issue - html

I have the following rails code (it uses simple form):
<%= f.input :color, collection: custom_colors.split(/\n/).reject(&:empty?) %>
custom_colors comes from a text area where users are asked to enter entries seperated by new lines.
The issue: This displays correctly, however, when a user goes to edit, the color is not saved and the select is empty. I'm using a typical rails scaffold.
The issue (continued): The reason I believe this is not working, is that when saved, the color looks like blue\r\n. However, when I do:
<%= field.options.split(/\n/).reject(&:empty?) %> it returns blue\r, which means the input won't match, as blue\r\n clearly does not equal blue\r
How do I fix this?

Try this in your form.
<%= f.input :color, collection: custom_colors.split(/\n/).map(&:strip).reject(&:empty?) %>

Related

Ruby on Rails pass button value to next page

I am trying to design a webpage that contains both a radio button field and a list of links. When one of the links is clicked, I would like to pass the associated value of the button to a controller. So far I have the following code:
In my views page:
<div id="radio_buttons">
<%= radio_button_tag 'radioGroup', "1" %> 1
<%= radio_button_tag 'radioGroup', "2" %> 2
<%= radio_button_tag 'radioGroup', "4" %> 4
</div>
...
<div class = "text">
<%= link_to "mylink", search_path(:radioGroup => #radioGroup)%>
</div>
In my Controller:
#radioGroup = params[:radioGroup]
When I add the line 'puts #radioGroup' to my controller, it prints nothing. I have been able to pass different values to a new web page upon a link click (i.e those contained in a text field) but for some reason I can't figure out how to do the same thing with buttons. Does anyone know how to accomplish this?
Thanks!
If you want to submit multiple values to another page you should use an HTML form for it. Check this page for more related info: http://guides.rubyonrails.org/form_helpers.html.

Using one '_form.html.erb' partial but have different elements appear for certain views

Is it possible to display certain elements within a partial for a particular view in Rails? For example I'd like the submit button's text to change depending on the view: so if I'm in the new.html.erb I'd like the submit button to appear as, <%= f.submit 'Create Account' %> and <%= f.submit 'Update Account' %> for edit.html.erb. The unconventional way would be to manually add the custom code into the each view but is there a clever way to do this in my _form.html.erb partial?
First of all, I would recommend putting it into the new and edit views. However, you can switch off of params[:action] if you want to. As in
<%= f.submit(((params[:action] == 'new') ? 'Create' : 'Update') + ' Account') %>
Use simple_form with i18n for that. SimpleForm make it automatically.
Example:
<%= simple_form_for(#message) do |f| %>
<%= f.error_notification %>
<%= f.input :title %>
<%= f.input :description %>
<%= f.button :submit, class: "btn btn-primary" %>
<% end %>
I second kdeisz's answer if your intention is to use a single partial. The line he wrote will not be necessary if you use two separate views - You can just use different names on the same button in each view without any need for conditional logic.
To answer your supplemental questions: There is a tradeoff here between future changeability and DRY code. If your new and edit needs will start to differ significantly, you will have a lot of bloated, difficult-to-change conditional logic in your partial if you use it to render major features.
If you keep the views separated, this may repeat a significant amount of code, but it will also make the individual pages easier to change; the functions of each view will be tailored very specifically to the needs of each HTTP verb.
The answer isn't to conform completely to REST or to DRY "just because", but to ask yourself what will result in more work down the road. If your new and edit pages will be basically the same but for a few very minor features, the single partial (DRY) is more practical. If you see them diverging significantly in the future, keep them separated into two views (less DRY but more changeable).
Params. Each request made to Rails will automatically include an action and a controller based on the route the user requests; for example, navigating to /foo/bar might trigger action bar for controller foo, depending on how you've set up config/routes.rb. Rails fills in params[:action] and params[:controller] with these automatically. A good explanation of how this works, and how to access path and request params, can be found here.

Render Partial Help and Breakdown

When a person uses:
<%= render user.posts %>
what is happening behind the code that is tells it to search for the partial _post.html.erb and know to pass the a specified user inside the call?
I have seen many example where a partial is called from outside the index, such as:
<%= render partial: "posts", locals: {post: post} %>
But that is different from the example above
render user.posts detects a model/s as a parameter, and searches for /posts/_post.html.erb partial, see here, section 3.4.5.
render :partial => 'xxx/yyy' searches xxx/yyy file in views path app/views, i.e. app/views/xxx/yyy, you can see in which places these views are searched by looking at your rails server console.
Figured out what I was trying to achieve. I knew I was missing some simple.
Inside my show view:
<% #user.posts.each do |post| %>
<%= post.title %>
<% end %>

Rails page has wrong html

I have a link to a page in the same folder as this page on my rails site:
<%= link_to 'Special Access', 'followers/special_access' %>
However, when I go to this page it shows a different page on that url.
<p id="notice"><%= notice %></p>
<div id="sent">
<p>Your request has been sent</p>
<%= link_to 'Home', followers_path %>
</div>
I tried deleting the page that the html is from, but first of all I need that page and it also gives me an error.
I edited the controller to contain:
def special_access
format.html { redirect_to followers/special_access }
format.json { render :json => #post }
end
instead of
def show
but that still didn't solve the problem.
How do I get the right html to show up on the right page?
If you do not define a route for special_access, rails will just assume the the special_acces part in the path is the :id of the route for the show page (as the urls look like followers/:id).
So first off, in your routes.rb, find the resources :followers and replace with the following:
resources :followers do
collection do
get :special_access
end
end
And now you should best always use the rails path helpers, so your link would become
<% link_to 'Special Access', special_access_followers_path %>
Here I was assuming the special access is on the collection of the followers, if it should be on a specific follower (which seems more logical to me, but I have no idea of course), you should write
resources :followers do
member do
get :special_access
end
end
And your link would become
<% link_to 'Special Access', special_access_followers_path(#follower) %>
I am not quite sure what you want to do in your controller action, I hope you just want to render an html page (because redirecting to the same url seems silly, and your syntax is wrong there too).
Hope this helps.

using html tags within ruby on rails if statement

i have a method in my application controller that checks whether a user is in the correct group to access certain functions.
My problem is that when a user edits their own account settings i dont want them to be able to edit their group, unless they are part of the transport group. Currently i have this
<%= if logged_in_as_transport? %>
<div class="field">
<%= f.label :user_type %><br />
<%= f.select :user_type, [['Transport','1'],['Staff','2']] %>
</div>
<% end %>
My method works fine as it is used to check the permissions on the page. It would be ideal if the drop down box was not visible for other groups but just being disabled would work too.
the above code creates a SyntaxError "unexpected tRPAREN"
');#output_buffer.append= ( if logged_in_as_transport? );#output_buffer.safe_concat('
if anyone can help that would be great.
<%= if logged_in_as_transport? %>
should be
<% if logged_in_as_transport? %>
an if statement's conditional doesn't have a return value to output to your view's HTML (which is what your'e saying you want to have happen by using <%= instead of <%)